com.devexperts.io
Class ByteArrayInput

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

public class ByteArrayInput
extends BufferedInput

An implementation of BufferedInput using single byte array buffer.


Field Summary
 
Fields inherited from class com.devexperts.io.BufferedInput
buffer, EMPTY_BYTE_ARRAY, limit, markLimit, markPosition, position, totalPositionBase
 
Constructor Summary
ByteArrayInput()
          Creates a new byte array input without pre-allocated buffer.
ByteArrayInput(byte[] buf)
          Creates a new byte array input with specified buffer as its input.
ByteArrayInput(byte[] buf, int offset, int length)
          Creates a new byte array input with specified buffer and range as its input.
 
Method Summary
 void clear()
          Clears this byte array input.
 void ensureCapacity(int requiredCapacity)
          Ensures that the byte array used for buffering has at least the specified capacity.
 byte[] getBuffer()
          Returns byte array used for buffering.
 int getLimit()
          Returns limit.
 int getPosition()
          Returns position.
protected  int readData()
          This method is invoked when input methods need more bytes to read data.
 void rewind(long n)
          Rewinds specified number of bytes.
 void setBuffer(byte[] newBuffer)
          Sets specified byte array to be used for buffering.
 void setInput(byte[] buf, int offset, int length)
          Sets specified buffer and range as an input.
 void setLimit(int newLimit)
          Sets limit as specified.
 void setPosition(int newPosition)
          Sets position as specified.
 void setTotalPosition(long totalPosition)
          Sets total position as specified.
 
Methods inherited from class com.devexperts.io.BufferedInput
available, checkEOB, checkRewind, mark, mark, markSupported, needData, read, read, read, readBoolean, readByte, readByteArray, readChar, readCompactInt, readCompactLong, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readObject, readShort, readUnsignedByte, readUnsignedShort, readUTF, readUTFChar, readUTFString, reset, seek, skip, skipBytes, throwEOFException, totalPosition
 
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
 

Constructor Detail

ByteArrayInput

public ByteArrayInput()
Creates a new byte array input without pre-allocated buffer.


ByteArrayInput

public ByteArrayInput(byte[] buf)
Creates a new byte array input with specified buffer as its input.

Parameters:
buf - the input buffer

ByteArrayInput

public ByteArrayInput(byte[] buf,
                      int offset,
                      int length)
Creates a new byte array input with specified buffer and range as its input. The position is set to offset, the limit is set to offset+length.

Parameters:
buf - the input buffer
offset - the offset in the buffer of the first byte to read
length - the maximum number of bytes to read from the buffer
Throws:
IllegalArgumentException - if buf, offset and length do not constitute a valid range
Method Detail

readData

protected int readData()
                throws IOException
Description copied from class: BufferedInput
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.

Specified by:
readData in class BufferedInput
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

setInput

public void setInput(byte[] buf,
                     int offset,
                     int length)
Sets specified buffer and range as an input. The total position is set to zero, the position is set to offset, the limit is set to offset+length, the mark is invalidated.

Parameters:
buf - the input buffer
offset - the offset in the buffer of the first byte to read
length - the maximum number of bytes to read from the buffer
Throws:
IllegalArgumentException - if buf, offset and length do not constitute a valid range

getBuffer

public byte[] getBuffer()
Returns byte array used for buffering. Never returns null.


setBuffer

public void setBuffer(byte[] newBuffer)
Sets specified byte array to be used for buffering. Null is treated as empty array. The total position is set to zero, the position is set to zero, the limit is set to the capacity of new buffer, the mark is invalidated.


getPosition

public int getPosition()
Returns position.


setPosition

public void setPosition(int newPosition)
Sets position as specified. The total position is adjusted by the same distance as position. If the position is smaller than the mark position then the mark is invalidated.

Throws:
IllegalArgumentException - if the new position is negative or is larger than the limit

getLimit

public int getLimit()
Returns limit.


setLimit

public void setLimit(int newLimit)
Sets limit as specified. If the position is larger than the new limit then it is set to the new limit and total position is adjusted by the same distance as position. If the limit is smaller than the mark position then the mark is invalidated.

Throws:
IllegalArgumentException - if the new limit is negative or is larger than the capacity

setTotalPosition

public void setTotalPosition(long totalPosition)
Sets total position as specified. This method does not reposition the stream, it merely changes total position value as requested.

Throws:
IllegalArgumentException - if the new total position is negative

clear

public void clear()
Clears this byte array input. The total position is set to zero, the position is set to zero, the limit is set to the capacity, the mark is invalidated.


ensureCapacity

public void ensureCapacity(int requiredCapacity)
Ensures that the byte array used for buffering has at least the specified capacity. This method reallocates buffer if needed and copies content of old buffer into new one.


rewind

public void rewind(long n)
Description copied from class: BufferedInput
Rewinds specified number of bytes. The input cannot be rewound past the marked position.

Specified by:
rewind in class BufferedInput
Parameters:
n - the number of bytes to rewind


Copyright © 2013 Devexperts. All Rights Reserved.