Class ChannelData
- All Implemented Interfaces:
Markable
- Direct Known Subclasses:
ChannelDataInput,ChannelDataOutput
ChannelDataInput and ChannelDataOutput.
Common properties include a reference to a ByteBuffer supplied by the user and methods for
querying or modifying the stream position. This class does not define any read or write operations.- Since:
- 0.3
- Version:
- 1.3
- Author:
- Martin Desruisseaux (Geomatys)
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal ByteBufferThe buffer to use for transferring data between the channel to memory.final longThe position of the channel when thisChannelDatahas been created.final StringA short identifier (typically a filename without path) used for formatting error message. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidflushBefore(long position) Discards the initial portion of the stream prior to the indicated position.final intReturns the current bit offset, as an integer between 0 and 7 inclusive.final longReturns the earliest position in the stream to which seeking may be performed.longReturns the current byte position of the stream.final voidmark()Pushes the current stream position onto a stack of marked positions.protected voidInvoked when an operation between the channel and the buffer transferred no byte.final voidreset()Resets the current stream byte and bit positions from the stack of marked positions.final voidreset(long position) Moves to the given position in the stream and discards all marks at or after that position.abstract voidseek(long position) Moves to the given position in the stream, relative to the stream position at construction time.final voidsetBitOffset(int bitOffset) Sets the bit offset to the given value.final voidsetStreamPosition(long position) Sets the current byte position of the stream.final voidMoves the stream position to the next byte boundary.toString()Returns a string representation of this object for debugging purpose.
-
Field Details
-
filename
A short identifier (typically a filename without path) used for formatting error message. This is often the value given byStorageConnector.getStorageName(). -
buffer
The buffer to use for transferring data between the channel to memory. -
channelOffset
public final long channelOffsetThe position of the channel when thisChannelDatahas been created. This is almost always 0, but we allow other values in case the data to read or write are part of a bigger file.This value is added to the argument given to the
seek(long)method. Users can ignore this field, unless they want to invokeSeekableByteChannel.position(long)directly.
-
-
Method Details
-
getBitOffset
public final int getBitOffset()Returns the current bit offset, as an integer between 0 and 7 inclusive.According
ImageInputStreamcontract, the bit offset shall be reset to 0 by every call to anyreadorwritemethod exceptreadBit(),readBits(int),writeBit(int)andwriteBits(long, int).- Returns:
- the bit offset of the stream.
-
setBitOffset
public final void setBitOffset(int bitOffset) Sets the bit offset to the given value. The given value shall be between 0 inclusive to 8 exclusive.- Parameters:
bitOffset- the new bit offset of the stream.- Throws:
IllegalArgumentException- if the given offset is out of range.
-
skipRemainingBits
public final void skipRemainingBits()Moves the stream position to the next byte boundary. If the bit offset is zero, this method does nothing. Otherwise it skips the remaining bits in current byte. -
getStreamPosition
public long getStreamPosition()Returns the current byte position of the stream. The returned value is relative to the position that the stream had atChannelDataconstruction time.- Specified by:
getStreamPositionin interfaceMarkable- Returns:
- the position of the stream.
-
setStreamPosition
public final void setStreamPosition(long position) Sets the current byte position of the stream. This method does not seeks the stream; this method only modifies the value to be returned bygetStreamPosition(). This method can be invoked when some external code has performed some work with the channel and wants to inform thisChannelDataabout the new position resulting from this work. Notes:- Invoking this method clears the bit offset and the marks.
- This method does not need to be invoked when only the buffer position has changed.
- Parameters:
position- the new position of the stream.
-
getFlushedPosition
public final long getFlushedPosition()Returns the earliest position in the stream to which seeking may be performed.- Returns:
- the earliest legal position for seeking.
-
flushBefore
Discards the initial portion of the stream prior to the indicated position. Attempting to seek to an offset within the flushed portion of the stream may result in anIndexOutOfBoundsException.If the
bufferis read-only, then this method does nothing. Otherwise this method moves the data starting at the given position to the beginning of thebuffer, thus making more room for new data before the data at the given position is discarded.- Parameters:
position- the length of the stream prefix that may be flushed.- Throws:
IOException- if an I/O error occurred.
-
seek
Moves to the given position in the stream, relative to the stream position at construction time.- Parameters:
position- the position where to move.- Throws:
IOException- if the stream cannot be moved to the given position.
-
mark
public final void mark()Pushes the current stream position onto a stack of marked positions. Note thatChannelDatamaintains its own marks - the buffer's mark is left unchanged. -
reset
Resets the current stream byte and bit positions from the stack of marked positions. AnIOExceptionmay be be thrown if the previous marked position lies in the discarded portion of the stream.Departure from Image I/O specification
TheImageInputStream.reset()contract specifies that if there is no matching mark, then this method shall do nothing. This method throwsIOExceptioninstead; silently ignoring mismatch is considered too dangerous. This is a departure fromImageInputStreambut is consistent withInputStream.reset()contract.- Specified by:
resetin interfaceMarkable- Throws:
IOException- if this stream cannot move to the last mark position.- See Also:
-
reset
Moves to the given position in the stream and discards all marks at or after that position. If a mark exists at the given position, the bit offset is also restored.- Specified by:
resetin interfaceMarkable- Parameters:
position- position where to seek.- Throws:
IOException- if this stream cannot move to the specified mark position.
-
onEmptyTransfer
Invoked when an operation between the channel and the buffer transferred no byte. Note that this is unrelated to end-of-file, in which caseReadableByteChannel.read(ByteBuffer)returns -1. A return value of 0 happen for example if the channel is a socket in non-blocking mode and the socket buffer has not yet transferred new data.The current implementation sleeps an arbitrary number of time before to allow a new try. We do that in order to avoid high CPU consumption when data are expected to take more than a few nanoseconds to arrive.
- Throws:
IOException- if the implementation chooses to stop the process.
-
toString
Returns a string representation of this object for debugging purpose.
-