FileDocCategorySizeDatePackage
Support_StringReader.javaAPI DocAndroid 1.5 API8288Wed May 06 22:41:06 BST 2009tests.support

Support_StringReader

public class Support_StringReader extends Reader

Fields Summary
private String
str
private int
markpos
private int
pos
private int
count
Constructors Summary
public Support_StringReader(String str)
Construct a StringReader on the String str. The size of the reader is set to the length() of the String and the Object to synchronize access through is set to str.

param
str the String to filter reads on.


                                                            
       
        super(str);
        this.str = str;
        this.count = str.length();
    
Methods Summary
public voidclose()
This method closes this StringReader. Once it is closed, you can no longer read from it. Only the first invocation of this method has any effect.

        synchronized (lock) {
            if (isOpen()) {
                str = null;
            }
        }
    
private booleanisOpen()
Answer a boolean indicating whether or not this StringReader is open.

        return str != null;
    
public voidmark(int readLimit)
Set a Mark position in this Reader. The parameter readLimit is ignored for StringReaders. Sending reset() will reposition the reader back to the marked position provided the mark has not been invalidated.

param
readlimit ignored for StringReaders.
exception
java.io.IOException If an error occurs attempting mark this StringReader.

        if (readLimit >= 0) {
            synchronized (lock) {
                if (isOpen()) {
                    markpos = pos;
                } else {
                    throw new IOException("StringReader is closed");
                }
            }
        } else {
            throw new IllegalArgumentException();
        }
    
public booleanmarkSupported()
Answers a boolean indicating whether or not this StringReader supports mark() and reset(). This method always returns true.

return
true if mark() and reset() are supported, false otherwise. This implementation always returns true.

        return true;
    
public intread()
Reads a single character from this StringReader and returns the result as an int. The 2 higher-order bytes are set to 0. If the end of reader was encountered then return -1.

return
the character read or -1 if end of reader.
exception
java.io.IOException If the StringReader is already closed.

        synchronized (lock) {
            if (isOpen()) {
                if (pos != count) {
                    return str.charAt(pos++);
                }
                return -1;
            }
            throw new IOException("StringReader is closed");
        }
    
public intread(char[] buf, int offset, int count)
Reads at most count characters from this StringReader and stores them at offset in the character array buf. Returns the number of characters actually read or -1 if the end of reader was encountered.

param
buf character array to store the read characters
param
offset offset in buf to store the read characters
param
count maximum number of characters to read
return
the number of characters read or -1 if end of reader.
exception
java.io.IOException If the StringReader is closed.

        // avoid int overflow
        if (0 <= offset && offset <= buf.length && 0 <= count
                && count <= buf.length - offset) {
            synchronized (lock) {
                if (isOpen()) {
                    if (pos == this.count) {
                        return -1;
                    }
                    int end = pos + count > this.count ? this.count : pos
                            + count;
                    str.getChars(pos, end, buf, offset);
                    int read = end - pos;
                    pos = end;
                    return read;
                }
                throw new IOException("StringReader is closed");
            }
        }
        throw new ArrayIndexOutOfBoundsException();
    
public booleanready()
Answers a boolean indicating whether or not this StringReader is ready to be read without blocking. If the result is true, the next read() will not block. If the result is false this Reader may or may not block when read() is sent. The implementation in StringReader always returns true even when it has been closed.

return
true if the receiver will not block when read() is called, false if unknown or blocking will occur.
exception
java.io.IOException If an IO error occurs.

        synchronized (lock) {
            if (isOpen()) {
                return true;
            }
            throw new IOException("StringReader is closed");
        }
    
public voidreset()
Reset this StringReader's position to the last mark() location. Invocations of read()/skip() will occur from this new location. If this Reader was not marked, the StringReader is reset to the beginning of the String.

exception
java.io.IOException If this StringReader has already been closed.

        synchronized (lock) {
            if (isOpen()) {
                pos = markpos != -1 ? markpos : 0;
            } else {
                throw new IOException("StringReader is closed");
            }
        }
    
public longskip(long count)
Skips count number of characters in this StringReader. Subsequent read()'s will not return these characters unless reset() is used.

param
count The number of characters to skip.
return
the number of characters actually skipped.
exception
java.io.IOException If this StringReader has already been closed.

        synchronized (lock) {
            if (isOpen()) {
                if (count <= 0) {
                    return 0;
                }
                long skipped = 0;
                if (count < this.count - pos) {
                    pos = pos + (int) count;
                    skipped = count;
                } else {
                    skipped = this.count - pos;
                    pos = this.count;
                }
                return skipped;
            }
            throw new IOException("StringReader is closed");
        }