Package org.jibx.runtime.impl
Class InByteBuffer
- java.lang.Object
-
- org.jibx.runtime.impl.InByteBuffer
-
- All Implemented Interfaces:
IByteBuffer,IInByteBuffer
public class InByteBuffer extends java.lang.Object implements IInByteBuffer
Byte buffer wrapping an input stream. Clients need to obey the interface access rules.- Author:
- Dennis M. Sosnoski
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUFFER_SIZEDefault input buffer size.private byte[]m_bufferBuffer for input bytes.private booleanm_isEndFlag for end of stream reached.private intm_limitOffset past end of bytes in buffer.private intm_offsetCurrent offset for removing bytes from buffer.private java.io.InputStreamm_streamStream for byte input.
-
Constructor Summary
Constructors Constructor Description InByteBuffer()Constructor using default buffer size.InByteBuffer(int size)Constructor with size specified.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private booleanfillBuffer(int size)Read data into the buffer to assure at least a minimum number of bytes are available.voidfinish()Complete usage of the current stream.byte[]getBuffer()Get the byte array buffer.intgetLimit()Get offset past the end of data in buffer.intgetOffset()Get the index of the next byte to be read.booleanrequire(int size)Require some number of bytes of data.voidreset()Reset to initial state for reuse.voidsetInput(java.io.InputStream is)Set the actual input stream to be used for this buffer.voidsetOffset(int offset)Set the current offset.
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
Default input buffer size.- See Also:
- Constant Field Values
-
m_stream
private java.io.InputStream m_stream
Stream for byte input.
-
m_isEnd
private boolean m_isEnd
Flag for end of stream reached.
-
m_buffer
private byte[] m_buffer
Buffer for input bytes.
-
m_limit
private int m_limit
Offset past end of bytes in buffer.
-
m_offset
private int m_offset
Current offset for removing bytes from buffer.
-
-
Method Detail
-
fillBuffer
private boolean fillBuffer(int size) throws java.io.IOExceptionRead data into the buffer to assure at least a minimum number of bytes are available. Any retained data is first copied down to the start of the buffer array. Next, data is read from the wrapped stream into the available space in the buffer. The actual number of characters read by a call to this method is normally between one and the space available in the buffer array.- Parameters:
size- minimum number of bytes required- Returns:
trueif data has been read into buffer,falseif not- Throws:
java.io.IOException- on error reading from wrapped stream
-
reset
public void reset()
Reset to initial state for reuse.
-
setInput
public void setInput(java.io.InputStream is)
Set the actual input stream to be used for this buffer. If an input stream is currently open when this is called the existing stream is closed and any buffered data discarded, with any errors ignored.- Parameters:
is- stream
-
getBuffer
public byte[] getBuffer()
Get the byte array buffer.- Specified by:
getBufferin interfaceIByteBuffer- Returns:
- array
-
getOffset
public int getOffset()
Get the index of the next byte to be read. After reading data, thesetOffset(int)method must be used to update the current offset before any other operations are performed on the buffer.- Specified by:
getOffsetin interfaceIByteBuffer- Returns:
- offset
-
setOffset
public void setOffset(int offset)
Set the current offset. This must be used to update the stored buffer state after reading any data.- Specified by:
setOffsetin interfaceIByteBuffer- Parameters:
offset-
-
getLimit
public int getLimit()
Get offset past the end of data in buffer.- Specified by:
getLimitin interfaceIInByteBuffer- Returns:
- offset past end of data
-
require
public boolean require(int size) throws java.io.IOExceptionRequire some number of bytes of data. When this call is made the buffer can discard all data up to the current offset, and will copy retained data down to the start of the buffer array and read more data from the input stream if necessary to make the requested number of bytes available. This call may cause the byte array buffer to be replaced, sogetBuffer(),getLimit(), andgetOffset()must all be called again before any further use of the buffer.- Specified by:
requirein interfaceIInByteBuffer- Parameters:
size- desired number of bytes- Returns:
trueif request satisfied,falseif not- Throws:
java.io.IOException
-
finish
public void finish() throws java.io.IOExceptionComplete usage of the current stream. This method should be called whenever the application is done reading from the buffered stream. Once this method is called, a call tosetInput(InputStream)is required before the buffer can again be used.- Specified by:
finishin interfaceIByteBuffer- Throws:
java.io.IOException
-
-