net.jbeans.io
Class StringReader

java.lang.Object
  extended byjava.io.Reader
      extended bynet.jbeans.io.StringReader
All Implemented Interfaces:
java.util.Enumeration

public final class StringReader
extends java.io.Reader
implements java.util.Enumeration

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:

     this
     is
     a
     test
	   1
	   2
	   3.4 
 
or
     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 
 

Version:
$Revision: 1.1.1.1 $
Author:
Masahiro Takatsuka (masa@jbeans.net)
See Also:
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

StringReader

public StringReader(java.lang.String str,
                    java.lang.String delim,
                    java.lang.String quotes,
                    boolean returnTokens)
Constructs a M-string reader for the specified string. The set of delimiters is specified in the delim argument.

If the returnTokens flag is true, then the delimiter characters are also returned as tokens.

Parameters:
str - a string to be parsed.
delim - the delimiters.
quotes - quotation characters.// 1999-Oct-18
returnTokens - flag indicating whether to return the delimiters as tokens.

StringReader

public StringReader(java.lang.String str,
                    java.lang.String delim,
                    boolean returnTokens)
Constructs a M-string reader for the specified string. The set of delimiters is specified in the delim argument.

If the returnTokens flag is true, then the delimiter characters are also returned as tokens.

Parameters:
str - a string to be parsed.
delim - the delimiters.
returnTokens - flag indicating whether to return the delimiters as tokens.

StringReader

public StringReader(java.lang.String str,
                    java.lang.String delim)
Constructs a M-string reader for the specified string. The set of delimiters is specified in the delim argument.

Parameters:
str - a string to be parsed.
delim - the delimiters.

StringReader

public StringReader(java.lang.String str)
Constructs a M-string reader for the specified string. The following characters are used as the default delimiter set: "\t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.

Parameters:
str - a string to be parsed.
Method Detail

hasMoreTokens

public final boolean hasMoreTokens()
Returns true if there are more tokens available. If this method returns true, then a subsequent call to nextToken or readToken with no argument will successfully return a token.

Returns:
true if and only if there is at least one token in the string after the current position; false otherwise.

restOfTokens

public final java.lang.String restOfTokens()
Returns the rest of tokens in the string.

Returns:
the rest of tokens in the string.
Throws:
java.util.NoSuchElementException - if there are no more tokens.

nextToken

public final java.lang.String nextToken()
Returns the next token.

Returns:
the next token.
Throws:
java.util.NoSuchElementException - if there are no more tokens.

nextToken

public final java.lang.String nextToken(java.lang.String delim)
Returns the next token. First, delimiters specified by delim is set as the new delimiters. Then the next token in the string after the current position is returned. The new delimiter set remains the default after this call.

Parameters:
delim - the new delimiters.
Returns:
the next token, after switching to the new delimiter set.
Throws:
java.util.NoSuchElementException - if there are no more tokens.

hasMoreElements

public final boolean hasMoreElements()
Returns the same value as the hasMoreTokens method. It exists so that this class can implement the Enumeration interface.

Specified by:
hasMoreElements in interface java.util.Enumeration
Returns:
true if there are more tokens; false otherwise.
See Also:
hasMoreTokens()

nextElement

public final java.lang.Object nextElement()
Returns the same value as the nextToken method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration interface.

Specified by:
nextElement in interface java.util.Enumeration
Returns:
the next token in the string.
Throws:
java.util.NoSuchElementException - if there are no more tokens in this tokenizer's string.
See Also:
nextToken()

countTokens

public final int countTokens()
Calculates the number of tokens in the string. The current position is not advanced.

Returns:
the number of tokens remaining in the string using the current delimiter set.

reset

public final void reset(java.lang.String str,
                        java.lang.String delim,
                        boolean returnTokens)
Resets a M-string reader for the specified string. The set of delimiters is specified in the delim argument.

If the returnTokens flag is true, then the delimiter characters are also returned as tokens.

Parameters:
str - a string to be parsed.
delim - the delimiters.
returnTokens - flag indicating whether to return the delimiters as tokens.

reset

public final void reset(java.lang.String str,
                        java.lang.String delim)
Constructs a M-string reader for the specified string. The set of delimiters is specified in the delim argument.

Parameters:
str - a string to be parsed.
delim - the delimiters.

reset

public final void reset(java.lang.String str)
Constructs a M-string reader for the specified string. The following characters are used as the default delimiter set: "\t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character.

Parameters:
str - a string to be parsed.

read

public final int read(char[] cbuf,
                      int off,
                      int len)
Read characters into a portion of an array.

Parameters:
cbuf - Destination buffer
off - Offset at which to start writing characters
len - Maximum number of characters to read
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
java.io.IOException - If an I/O error occurs

read

public final int read()
Read a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.

Subclasses that intend to support efficient single-character input should override this method.

Returns:
The character read, as an integer in the range 0 to 16383 (0x00-0xffff), or -1 if the end of the stream has been reached

read

public final int read(char[] cbuf)
Read characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

Parameters:
cbuf - Destination buffer
Returns:
The number of bytes read, or -1 if the end of the stream has been reached

ready

public final boolean ready()
Tell whether this stream is ready to be read. String readers are always ready to be read.


markSupported

public final boolean markSupported()
Tell whether this stream supports the mark() operation, which it does.


mark

public final void mark(int readAheadLimit)
                throws java.io.IOException
Mark the present position in the stream. Subsequent calls to reset() will reposition the stream to this point.

Parameters:
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.
Throws:
java.io.IOException - If an I/O error occurs

reset

public final void reset()
                 throws java.io.IOException
Reset the stream to the most recent mark, or to the beginning of the string if it has never been marked.

Throws:
java.io.IOException - If an I/O error occurs

close

public final void close()
Close the stream.


readToken

public final java.lang.String readToken()
Read one token as a string.

Returns:
A String containing the string, or null if the end of the stream has been reached.

readToken

public final java.lang.String readToken(java.lang.String delim)
Returns the next token in this string tokenizer's string. First, the set of characters considered to be delimiters by this StringTokenizer object is changed to be the characters in the string delim. Then the next token in the string after the current position is returned. The current position is advanced beyond the recognized token. The new delimiter set remains the default after this call.

Parameters:
delim - the new delimiters.
Returns:
the next token, after switching to the new delimiter set.
Throws:
java.util.NoSuchElementException - if there are no more tokens in this tokenizer's string.

readBoolean

public final boolean readBoolean()
Reads a 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.

Returns:
the boolean value read.

readByte

public final byte readByte()
Reads a signed 8-bit value from this data input stream. This method reads a byte from the underlying input stream. If the byte read is b, where 0 <= b <= 255, then the result is:

This method blocks until either the byte is read, the end of the stream is detected, or an exception is thrown.

Returns:
the next byte of this input stream as a signed 8-bit byte.

readShort

public final short readShort()
Reads a signed 16-bit number from this data input stream. The method reads two bytes from the underlying input stream. If the two bytes read, in order, are b1 and b2, where each of the two values is between 0 and 255, inclusive, then the result is equal to:

This method blocks until the two bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next two bytes of this input stream, interpreted as a signed 16-bit number.

readChar

public final char readChar()
Reads a Unicode character from this data input stream. This method reads two bytes from the underlying input stream. If the bytes read, in order, are b1 and b2, where 0 <= b1, b1 <= 255, then the result is equal to:

This method blocks until either the two bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next two bytes of this input stream as a Unicode character.

readInt

public final int readInt()
Reads a signed 32-bit integer from this data input stream. This method reads four bytes from the underlying input stream. If the bytes read, in order, are b1, b2, b3, and b4, where 0 <= b1, b2, b3, b4 <= 255, then the result is equal to:

This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown.

Returns:
the next four bytes of this input stream, interpreted as an int.

readLong

public final long readLong()
                    throws java.io.IOException
Reads a signed 64-bit integer from this data input stream. This method reads eight bytes from the underlying input stream. If the bytes read, in order, are b1, b2, b3, b4, b5, b6, b7, and b8, where

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.

Returns:
the next eight bytes of this input stream, interpreted as a long.
Throws:
java.io.IOException

readFloat

public final float readFloat()
Reads a 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.

Returns:
the next four bytes of this input stream, interpreted as a float.

readDouble

public final double readDouble()
Reads a 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.

Returns:
the next eight bytes of this input stream, interpreted as a double.


Copyright © 2003 JBean project. All Rights Reserved.