com.devexperts.io
Class BufferedInput

java.lang.Object
  extended by java.io.InputStream
      extended by com.devexperts.io.BufferedInput
All Implemented Interfaces:
Closeable, DataInput, ObjectInput
Direct Known Subclasses:
BufferedInputPart, ByteArrayInput, StreamInput

public abstract class BufferedInput
extends InputStream
implements ObjectInput

An efficient buffered implementation of data input API. It is attributed with underlying byte buffer, position and limit:

The BufferedInput supports the notion of total position - an offset of the next byte to be read from the beginning of the stream. It is equal to the number of bytes read from the stream adjusted according to reposition methods like seek(long), skip(long), rewind(long) and other.

All BufferedInput implementations are required to support marking because proper marking plays a vital role in definition and implementation of certain reposition methods.


Field Summary
protected  byte[] buffer
           
protected static byte[] EMPTY_BYTE_ARRAY
           
protected  int limit
           
protected  long markLimit
           
protected  long markPosition
           
protected  int position
           
protected  long totalPositionBase
           
 
Constructor Summary
protected BufferedInput()
          Creates new instance with empty buffer.
 
Method Summary
 int available()
           
protected  void checkEOB()
          Throws IllegalStateException if expression (position !
protected  void checkRewind(long n)
          Throws appropriate exception if rewind for specified distance is impossible.
 void mark(int readLimit)
          Marks the current position in this input stream.
 void mark(long readLimit)
          Marks the current position in this input stream.
 boolean markSupported()
          All BufferedInput implementations support mark(int) and reset(), so this method always returns true.
protected  void needData()
          This method is invoked when input methods need more bytes to read data.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 boolean readBoolean()
           
 byte readByte()
           
 byte[] readByteArray()
          Reads an array of bytes in a compact encapsulation format.
 char readChar()
           
 int readCompactInt()
          Reads an int value in a compact format (see IOUtil).
 long readCompactLong()
          Reads a long value in a compact format (see IOUtil).
protected abstract  int readData()
          This method is invoked when input methods need more bytes to read data.
 double readDouble()
           
 float readFloat()
           
 void readFully(byte[] b)
           
 void readFully(byte[] b, int off, int len)
           
 int readInt()
           
 String readLine()
           
 long readLong()
           
 Object readObject()
           
 short readShort()
           
 int readUnsignedByte()
           
 int readUnsignedShort()
           
 String readUTF()
           
 int readUTFChar()
          Reads Unicode code point in a UTF-8 format.
 String readUTFString()
          Reads Unicode string in a UTF-8 format with compact encapsulation (see IOUtil).
 void reset()
          Repositions this stream to the position at the time the mark(int) method was last called on this input stream.
abstract  void rewind(long n)
          Rewinds specified number of bytes.
 void seek(long totalPosition)
          Repositions this buffered input to specified total position.
 long skip(long n)
           
 int skipBytes(int n)
           
protected  void throwEOFException()
          Throws EOFException when requested by needData() method.
 long totalPosition()
          Returns total position of this input stream.
 
Methods inherited from class java.io.InputStream
close
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.io.ObjectInput
close
 

Field Detail

EMPTY_BYTE_ARRAY

protected static final byte[] EMPTY_BYTE_ARRAY

buffer

protected byte[] buffer

position

protected int position

limit

protected int limit

totalPositionBase

protected long totalPositionBase

markPosition

protected long markPosition

markLimit

protected long markLimit
Constructor Detail

BufferedInput

protected BufferedInput()
Creates new instance with empty buffer.

Method Detail

needData

protected void needData()
                 throws IOException
This method is invoked when input methods need more bytes to read data. This method must ensure that expression (position < limit) is true or throw an exception. This method may block if needed. This method is allowed to update buffer, position, limit and totalPositionBase fields as needed.

This method shall throw an EOFException if the end of stream is reached.

Throws:
IOException - if an I/O error occurs or end of stream is reached

readData

protected abstract int readData()
                         throws IOException
This method is invoked when input methods need more bytes to read data. This method shall make an attempt to read some bytes into the buffer. This method returns number of bytes actually read or value -1 if no bytes can be read because the end of the stream has been reached. This method may block if needed. This method is allowed to update buffer, position, limit and totalPositionBase fields as needed.

This method shall never throw an EOFException.

Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached
Throws:
IOException - if an I/O error occurs

throwEOFException

protected void throwEOFException()
                          throws EOFException
Throws EOFException when requested by needData() method. This implementation throws same reused instance with truncated stack trace to avoid garbage.

Throws:
EOFException

checkEOB

protected final void checkEOB()
Throws IllegalStateException if expression (position != limit) is true.


read

public final int read()
               throws IOException
Specified by:
read in interface ObjectInput
Specified by:
read in class InputStream
Throws:
IOException

read

public final int read(byte[] b)
               throws IOException
Specified by:
read in interface ObjectInput
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Specified by:
read in interface ObjectInput
Overrides:
read in class InputStream
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Specified by:
skip in interface ObjectInput
Overrides:
skip in class InputStream
Throws:
IOException

available

public int available()
              throws IOException
Specified by:
available in interface ObjectInput
Overrides:
available in class InputStream
Throws:
IOException

readFully

public final void readFully(byte[] b)
                     throws IOException
Specified by:
readFully in interface DataInput
Throws:
IOException

readFully

public void readFully(byte[] b,
                      int off,
                      int len)
               throws IOException
Specified by:
readFully in interface DataInput
Throws:
IOException

skipBytes

public final int skipBytes(int n)
                    throws IOException
Specified by:
skipBytes in interface DataInput
Throws:
IOException

readBoolean

public final boolean readBoolean()
                          throws IOException
Specified by:
readBoolean in interface DataInput
Throws:
IOException

readByte

public final byte readByte()
                    throws IOException
Specified by:
readByte in interface DataInput
Throws:
IOException

readUnsignedByte

public final int readUnsignedByte()
                           throws IOException
Specified by:
readUnsignedByte in interface DataInput
Throws:
IOException

readShort

public final short readShort()
                      throws IOException
Specified by:
readShort in interface DataInput
Throws:
IOException

readUnsignedShort

public final int readUnsignedShort()
                            throws IOException
Specified by:
readUnsignedShort in interface DataInput
Throws:
IOException

readChar

public final char readChar()
                    throws IOException
Specified by:
readChar in interface DataInput
Throws:
IOException

readInt

public final int readInt()
                  throws IOException
Specified by:
readInt in interface DataInput
Throws:
IOException

readLong

public final long readLong()
                    throws IOException
Specified by:
readLong in interface DataInput
Throws:
IOException

readFloat

public final float readFloat()
                      throws IOException
Specified by:
readFloat in interface DataInput
Throws:
IOException

readDouble

public final double readDouble()
                        throws IOException
Specified by:
readDouble in interface DataInput
Throws:
IOException

readLine

public final String readLine()
                      throws IOException
Specified by:
readLine in interface DataInput
Throws:
IOException

readUTF

public final String readUTF()
                     throws IOException
Specified by:
readUTF in interface DataInput
Throws:
IOException

readObject

public final Object readObject()
                        throws IOException
Specified by:
readObject in interface ObjectInput
Throws:
IOException

readCompactInt

public final int readCompactInt()
                         throws IOException
Reads an int value in a compact format (see IOUtil). If actual encoded value does not fit into an int data type, then it is truncated to int value (only lower 32 bits are returned); the number is read entirely in this case. Equivalent to IOUtil.readCompactInt(java.io.DataInput) method.

Returns:
the int value read
Throws:
IOException - if an I/O error occurs

readCompactLong

public final long readCompactLong()
                           throws IOException
Reads a long value in a compact format (see IOUtil). Equivalent to IOUtil.readCompactLong(java.io.DataInput) method.

Returns:
the long value read
Throws:
IOException - if an I/O error occurs

readByteArray

public final byte[] readByteArray()
                           throws IOException
Reads an array of bytes in a compact encapsulation format. This method defines length as a number of bytes. Equivalent to IOUtil.readByteArray(java.io.DataInput) method.

Returns:
the byte array read
Throws:
IOException - if an I/O error occurs

readUTFChar

public final int readUTFChar()
                      throws IOException
Reads Unicode code point in a UTF-8 format. Overlong UTF-8 and CESU-8-encoded surrogates are accepted and read without errors. Equivalent to IOUtil.readUTFChar(java.io.DataInput) method.

Returns:
the Unicode code point read
Throws:
UTFDataFormatException - if the bytes do not represent a valid UTF-8 encoding of a character
IOException - if an I/O error occurs

readUTFString

public final String readUTFString()
                           throws IOException
Reads Unicode string in a UTF-8 format with compact encapsulation (see IOUtil). Overlong UTF-8 and CESU-8-encoded surrogates are accepted and read without errors. This method defines length as a number of bytes. Equivalent to IOUtil.readUTFString(java.io.DataInput) method.

Returns:
the Unicode string read
Throws:
UTFDataFormatException - if the bytes do not represent a valid UTF-8 encoding of a string
IOException - if an I/O error occurs

totalPosition

public final long totalPosition()
Returns total position of this input stream. The total position is defined as an offset of the next byte to be read from the beginning of the stream. It is equal to the number of read bytes adjusted according to outcome of repositioning methods like seek(long), skip(long), rewind(long), reset(), etc.


seek

public final void seek(long totalPosition)
                throws IOException
Repositions this buffered input to specified total position. If specified total position is smaller than current total position, then rewind(long) operation for appropriate distance is performed; the stream has to be properly marked to support this operation. If specified total position is larger than current total position, then skip(long) operation for appropriate distance is performed until requested total position is reached; if end of stream happens before this operation can be completed then EOFException is thrown.

Throws:
IllegalArgumentException - if total position is negative
IllegalStateException - if this stream is not marked or if attempting to rewind past the marked position
IOException - if an I/O error occurs
EOFException - if end of stream is reached

markSupported

public final boolean markSupported()
All BufferedInput implementations support mark(int) and reset(), so this method always returns true.

Overrides:
markSupported in class InputStream

mark

public final void mark(int readLimit)
Marks the current position in this input stream. A subsequent call to the reset() method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated. Negative value for readLimit may be used in order to invalidate the mark.

Overrides:
mark in class InputStream

mark

public void mark(long readLimit)
Marks the current position in this input stream. A subsequent call to the reset() method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated. Negative value for readLimit may be used in order to invalidate the mark.


reset

public void reset()
Repositions this stream to the position at the time the mark(int) method was last called on this input stream.

The general contract of reset is:

Overrides:
reset in class InputStream
Throws:
IllegalStateException - if this stream is not marked

rewind

public abstract void rewind(long n)
Rewinds specified number of bytes. The input cannot be rewound past the marked position.

Parameters:
n - the number of bytes to rewind
Throws:
IllegalStateException - if this stream is not marked or if attempting to rewind past the marked position

checkRewind

protected final void checkRewind(long n)
Throws appropriate exception if rewind for specified distance is impossible.



Copyright © 2013 Devexperts. All Rights Reserved.