FileDocCategorySizeDatePackage
MarkableInputStream.javaAPI DocphoneME MR2 API (J2ME)6056Wed May 02 18:00:28 BST 2007com.sun.kvem.midp.pim

MarkableInputStream

public class MarkableInputStream extends InputStream
InputStream that supports mark() with an infinite lookahead.

Fields Summary
private final InputStream
in
Input stream.
private ByteArrayOutputStream
baos
Internal buffered stream.
private byte[]
buffer
Current buffer.
private int
bufferIndex
Current index in buffer.
Constructors Summary
public MarkableInputStream(InputStream in)
Constructs a markable input stream.

param
in input data

        this.in = in;
    
Methods Summary
public voidclose()
Closes the input stream.

throws
IOException if any error occurs

        in.close();
        baos.close();
        baos = null;
        buffer = null;
    
public voidmark(int lookahead)
This implementation of mark() supports infinite lookahead.

param
lookahead The value of this parameter is ignored.

        baos = new ByteArrayOutputStream();
    
public booleanmarkSupported()
Checks if mark is supported.

return
true if mark is supported

        return true;
    
public intread(byte[] b)
Reads next block of bytes from the stream.

param
b buffer to hold data
return
number of bytes read
throws
IOException if an error occurs

        return read(b, 0, b.length);
    
public intread()
Reads a byte from the stream.

return
next byte from stream
throws
IOException if an error occurs

        if (buffer != null) {
            return readFromBuffer();
        } else {
            return readFromStream();
        }
    
public intread(byte[] b, int offset, int length)
Reads next block of bytes from the stream.

param
b buffer to hold data
param
offset in buffer for data read
param
length size of data to read
return
number of bytes read
throws
IOException if an error occurs

        if (buffer != null) {
            return readFromBuffer(b, offset, length);
        } else {
            return readFromStream(b, offset, length);
        }
    
private intreadFromBuffer(byte[] b, int offset, int length)
Reads next block of bytes from the internal buffer.

param
b buffer to hold data
param
offset in buffer for data read
param
length size of data to read
return
number of bytes read

        int bytesRead = -1;
        if (length <= buffer.length - bufferIndex) {
            System.arraycopy(buffer, bufferIndex, b, offset, length);
            bufferIndex += length;
            bytesRead = length;
        } else {
            int count = buffer.length - bufferIndex;
            System.arraycopy(buffer, bufferIndex, b, offset, count);
            buffer = null;
            bytesRead = count;
        }
        if (baos != null) {
            baos.write(b, offset, bytesRead);
        }
        return bytesRead;
    
private intreadFromBuffer()
Reads a byte from the internal buffer.

return
next byte from the buffer.

        int i = buffer[bufferIndex++];
        if (baos != null) {
            baos.write(i);
        }
        if (bufferIndex == buffer.length) {
            buffer = null;
        }
        return i;
    
private intreadFromStream(byte[] b, int offset, int length)
Reads next block of bytes from the stream.

param
b buffer to hold data
param
offset in buffer for data read
param
length size of data to read
return
number of bytes read
throws
IOException if an error occurs


        int i = in.read(b, offset, length);
        if (i != -1 && baos != null) {
            baos.write(b, offset, i);
        }
        return i;
    
private intreadFromStream()
Reads next value from the input stream.

return
next value from stream
throws
IOException if an error occurs

        int i = in.read();
        if (i != -1 && baos != null) {
            baos.write(i);
        }
        return i;
    
public voidreset()
Reset the line markers.

throws
IOException if an error occurs accessing the input stream

        if (baos == null) {
            throw new IOException("Cannot reset an unmarked stream");
        }
        if (baos.size() == 0) {
            // no data was read since the call to mark()
            baos = null;
        } else {
            buffer = baos.toByteArray();
            baos = null;
            bufferIndex = 0;
        }