|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.io.Reader net.jbeans.io.StringReader
The -string reader class allows an application to read tokens
as well as single characters from a string. The tokenization method
is the same one used by StringTokenizer
.
The MtStringReader
does not recognize identifiers, numbers,
quoted strings and skip comments.
Delimiters can be set at creation time as well as on a per-token basis.
The MtStringReader
behaves in one of two ways
(same as java.util.StringTokenizer), depending
on whether it was created with the returnTokens
flag
having the value true
or false
: see more detail
in java.util.StringTokenizer
.
The following is one example of the use of the string reader. The code:
MtStringReader sr = new MtStringReader("this is a test 1 2 3.4"); while (sr.hasMoreTokens()) { println(sr.nextToken()); }
prints the following output:
orthis is a test 1 2 3.4
MtStringReader sr = new MtStringReader("this is a test 1 2 3.4"); println(sr.nextToken()); println(sr.nextToken()); println(sr.nextToken()); println(sr.nextToken()); println(sr.readInt()); println(sr.readInt()); println(sr.readFloat());
prints the following output:
this is a test 1 2 3.4
Reader
,
Enumeration
Field Summary |
Fields inherited from class java.io.Reader |
lock |
Constructor Summary | |
StringReader(java.lang.String str)
Constructs a M-string reader for the specified string. |
|
StringReader(java.lang.String str,
java.lang.String delim)
Constructs a M-string reader for the specified string. |
|
StringReader(java.lang.String str,
java.lang.String delim,
boolean returnTokens)
Constructs a M-string reader for the specified string. |
|
StringReader(java.lang.String str,
java.lang.String delim,
java.lang.String quotes,
boolean returnTokens)
Constructs a M-string reader for the specified string. |
Method Summary | |
void |
close()
Close the stream. |
int |
countTokens()
Calculates the number of tokens in the string. |
boolean |
hasMoreElements()
Returns the same value as the hasMoreTokens
method. |
boolean |
hasMoreTokens()
Returns true if there are more tokens available. |
void |
mark(int readAheadLimit)
Mark the present position in the stream. |
boolean |
markSupported()
Tell whether this stream supports the mark() operation, which it does. |
java.lang.Object |
nextElement()
Returns the same value as the nextToken method,
except that its declared return value is Object rather than
String . |
java.lang.String |
nextToken()
Returns the next token. |
java.lang.String |
nextToken(java.lang.String delim)
Returns the next token. |
int |
read()
Read a single character. |
int |
read(char[] cbuf)
Read characters into an array. |
int |
read(char[] cbuf,
int off,
int len)
Read characters into a portion of an array. |
boolean |
readBoolean()
Reads a boolean from this data input stream. |
byte |
readByte()
Reads a signed 8-bit value from this data input stream. |
char |
readChar()
Reads a Unicode character from this data input stream. |
double |
readDouble()
Reads a double from this data input stream. |
float |
readFloat()
Reads a float from this data input stream. |
int |
readInt()
Reads a signed 32-bit integer from this data input stream. |
long |
readLong()
Reads a signed 64-bit integer from this data input stream. |
short |
readShort()
Reads a signed 16-bit number from this data input stream. |
java.lang.String |
readToken()
Read one token as a string. |
java.lang.String |
readToken(java.lang.String delim)
Returns the next token in this string tokenizer's string. |
boolean |
ready()
Tell whether this stream is ready to be read. |
void |
reset()
Reset the stream to the most recent mark, or to the beginning of the string if it has never been marked. |
void |
reset(java.lang.String str)
Constructs a M-string reader for the specified string. |
void |
reset(java.lang.String str,
java.lang.String delim)
Constructs a M-string reader for the specified string. |
void |
reset(java.lang.String str,
java.lang.String delim,
boolean returnTokens)
Resets a M-string reader for the specified string. |
java.lang.String |
restOfTokens()
Returns the rest of tokens in the string. |
Methods inherited from class java.io.Reader |
skip |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public StringReader(java.lang.String str, java.lang.String delim, java.lang.String quotes, boolean returnTokens)
delim
argument.
If the returnTokens
flag is true
, then
the delimiter characters are also returned as tokens.
str
- a string to be parsed.delim
- the delimiters.quotes
- quotation characters.// 1999-Oct-18returnTokens
- flag indicating whether to return the delimiters
as tokens.public StringReader(java.lang.String str, java.lang.String delim, boolean returnTokens)
delim
argument.
If the returnTokens
flag is true
, then
the delimiter characters are also returned as tokens.
str
- a string to be parsed.delim
- the delimiters.returnTokens
- flag indicating whether to return the delimiters
as tokens.public StringReader(java.lang.String str, java.lang.String delim)
delim
argument.
str
- a string to be parsed.delim
- the delimiters.public StringReader(java.lang.String str)
"\t\n\r\f"
: the space character, the tab
character, the newline character, the carriage-return character,
and the form-feed character.
str
- a string to be parsed.Method Detail |
public final boolean hasMoreTokens()
true
if and only if there is at least one token
in the string after the current position; false
otherwise.public final java.lang.String restOfTokens()
java.util.NoSuchElementException
- if there are no more tokens.public final java.lang.String nextToken()
java.util.NoSuchElementException
- if there are no more tokens.public final java.lang.String nextToken(java.lang.String delim)
delim
- the new delimiters.
java.util.NoSuchElementException
- if there are no more tokens.public final boolean hasMoreElements()
hasMoreTokens
method. It exists so that this class can implement the
Enumeration
interface.
hasMoreElements
in interface java.util.Enumeration
true
if there are more tokens;
false
otherwise.hasMoreTokens()
public final java.lang.Object nextElement()
nextToken
method,
except that its declared return value is Object
rather than
String
. It exists so that this class can implement the
Enumeration
interface.
nextElement
in interface java.util.Enumeration
java.util.NoSuchElementException
- if there are no more tokens in this
tokenizer's string.nextToken()
public final int countTokens()
public final void reset(java.lang.String str, java.lang.String delim, boolean returnTokens)
delim
argument.
If the returnTokens
flag is true
, then
the delimiter characters are also returned as tokens.
str
- a string to be parsed.delim
- the delimiters.returnTokens
- flag indicating whether to return the delimiters
as tokens.public final void reset(java.lang.String str, java.lang.String delim)
delim
argument.
str
- a string to be parsed.delim
- the delimiters.public final void reset(java.lang.String str)
"\t\n\r\f"
: the space character, the tab
character, the newline character, the carriage-return character,
and the form-feed character.
str
- a string to be parsed.public final int read(char[] cbuf, int off, int len)
cbuf
- Destination bufferoff
- Offset at which to start writing characterslen
- Maximum number of characters to read
java.io.IOException
- If an I/O error occurspublic final int read()
Subclasses that intend to support efficient single-character input should override this method.
public final int read(char[] cbuf)
cbuf
- Destination buffer
public final boolean ready()
public final boolean markSupported()
public final void mark(int readAheadLimit) throws java.io.IOException
readAheadLimit
- Limit on the number of characters that may be
read while still preserving the mark. Because
the stream's input comes from a string, there
is no actual limit, so this argument is ignored.
java.io.IOException
- If an I/O error occurspublic final void reset() throws java.io.IOException
java.io.IOException
- If an I/O error occurspublic final void close()
public final java.lang.String readToken()
public final java.lang.String readToken(java.lang.String delim)
delim
- the new delimiters.
java.util.NoSuchElementException
- if there are no more tokens in this
tokenizer's string.public final boolean readBoolean()
boolean
from this data input stream. This
method reads a single byte from the underlying input stream. A
value of 0
represents false
. Any other
value represents true
. This method blocks until
either the byte is read, the end of the stream is detected, or an
exception is thrown.
boolean
value read.public final byte readByte()
b
, where
0 <= b
<= 255, then the
result is:
(byte)(b)
This method blocks until either the byte is read, the end of the stream is detected, or an exception is thrown.
byte
.public final short readShort()
b1
and b2
,
where each of the two values is between 0
and
255
, inclusive, then the result is equal to:
(short)((b1 << 8) | b2)
This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.
public final char readChar()
b1
and b2
,
where 0 <= b1
,
b1
<= 255, then the result is equal to:
(char)((b1 << 8) | b2)
This method blocks until either the two bytes are read, the end of the stream is detected, or an exception is thrown.
public final int readInt()
b1
, b2
,
b3
, and b4
, where
0 <= b1
, b2
,
b3
, b4
<= 255, then the
result is equal to:
(b1 << 24) | (b2 << 16) + (b3 << 8) +b4
This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.
int
.public final long readLong() throws java.io.IOException
b1
, b2
,
b3
, b4
, b5
,
b6
, b7
, and b8
, where
0 <= b1, b2, b3, b4, b5, b6, b7, b8 <= 255,
then the result is equal to:
((long)b1 << 56) + ((long)b2 << 48) + ((long)b3 << 40) + ((long)b4 << 32) + ((long)b5 << 24) + (b6 << 16) + (b7 << 8) + b8
This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.
long
.
java.io.IOException
public final float readFloat()
float
from this data input stream. This
method reads an int
value as if by the
readInt
method and then converts that
int
to a float
using the
intBitsToFloat
method in class Float
.
This method blocks until the four bytes are read, the end of the
stream is detected, or an exception is thrown.
float
.public final double readDouble()
double
from this data input stream. This
method reads a long
value as if by the
readLong
method and then converts that
long
to a double
using the
longBitsToDouble
method in class Double
.
This method blocks until the eight bytes are read, the end of the stream is detected, or an exception is thrown.
double
.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |