Class ChannelDataOutput
java.lang.Object
org.apache.sis.internal.storage.io.ChannelData
org.apache.sis.internal.storage.io.ChannelDataOutput
- Direct Known Subclasses:
ChannelImageOutputStream
Provides convenience methods for working with a (Relationship with
This class API is compatibly with the
WritableByteChannel, ByteBuffer) pair.
The channel and the buffer must be supplied by the caller. It is okay if they have already been used
before ChannelDataOutput creation.
Encapsulation
This class exposes publicly the channel and the buffer because this class is not expected to perform all possible data manipulations that we can do with the buffers. This class is only a helper tool, which often needs to be completed by specialized operations performed directly on the buffer. However, users are encouraged to transfer data from the buffer to the channel using only the methods provided in this class if they want to keep theseek(long) and getStreamPosition() values accurate.
Since this class is only a helper tool, it does not "own" the channel and consequently does not provide
close() method. It is users responsibility to close the channel after usage.
Relationship with DataOutput
This class API is compatibly with the DataOutput interface, so subclasses can implement that
interface if they wish. This class does not implement DataOutput itself because it is not needed for
SIS purposes.
However, the ChannelImageOutputStream class implements the DataOutput interface, together with
the ImageOutputStream one, mostly for situations when inter-operability with
javax.imageio is needed.- Since:
- 0.5
- Version:
- 1.2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classHelper class for thewriteFully(…)methods, in order to avoid duplicating almost identical code many times. -
Field Summary
FieldsFields inherited from class org.apache.sis.internal.storage.io.ChannelData
buffer, bufferOffset, channelOffset, filename -
Constructor Summary
ConstructorsConstructorDescriptionChannelDataOutput(String filename, WritableByteChannel channel, ByteBuffer buffer) Creates a new data output for the given channel and using the given buffer. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidclear()Fills the buffer with the zero values from its position up to the limit.final voidensureBufferAccepts(int n) Makes sure that the buffer can accept at least n more bytes.final voidflush()Flushes thebuffercontent to the channel.(package private) final voidflushAndSetPosition(int position) Writes the buffer content up to the given position, then set the buffer position to the given value.longReturns the current byte position of the stream.final voidseek(long position) Moves to the given position in the stream, relative to the stream position at construction time.final voidwrite(byte[] src) Writes all bytes from the given array into the stream.final voidwrite(byte[] src, int offset, int length) Writeslengthbytes starting at indexoffsetfrom the given array.final voidwriteBit(int bit) Writes a single bit.final voidwriteBits(long bits, int numBits) Writes a sequence of bits.final voidwriteByte(int value) Writes the 8 low-order bits ofvto the stream.final voidwriteChar(int value) Writes char value (16 bits) into the steam.final voidwriteChars(char[] src) Writes all characters from the given array into the stream.final voidwriteChars(char[] src, int offset, int length) Writeslengthchars starting at indexoffsetfrom the given array.final voidwriteDouble(double value) Writes double value (64 bits) into the steam.final voidwriteDoubles(double[] src) Writes all doubles from the given array into the stream.final voidwriteDoubles(double[] src, int offset, int length) Writeslengthdoubles starting at indexoffsetfrom the given array.final voidwriteFloat(float value) Writes float value (32 bits) into the steam.final voidwriteFloats(float[] src) Writes all floats from the given array into the stream.final voidwriteFloats(float[] src, int offset, int length) Writeslengthfloats starting at indexoffsetfrom the given array.private voidWrites fully the buffer content from its position to its limit.final voidwriteInt(int value) Writes integer value (32 bits) into the steam.final voidwriteInts(int[] src) Writes all integers from the given array into the stream.final voidwriteInts(int[] src, int offset, int length) Writeslengthintegers starting at indexoffsetfrom the given array.final voidwriteLong(long value) Writes long value (64 bits) into the steam.final voidwriteLongs(long[] src) Writes all longs from the given array into the stream.final voidwriteLongs(long[] src, int offset, int length) Writeslengthlongs starting at indexoffsetfrom the given array.final voidwriteShort(int value) Writes the 16 low-order bits of value to the stream.final voidwriteShorts(short[] src) Writes all shorts from the given array into the stream.final voidwriteShorts(short[] src, int offset, int length) Writeslengthshorts starting at indexoffsetfrom the given array.Methods inherited from class org.apache.sis.internal.storage.io.ChannelData
clearBitOffset, flushBefore, getBitOffset, getFlushedPosition, mark, onEmptyTransfer, readBitFromBuffer, reset, reset, setBitOffset, setStreamPosition, skipRemainingBits, toString
-
Field Details
-
channel
The channel where data are written. This is supplied at construction time.
-
-
Constructor Details
-
ChannelDataOutput
public ChannelDataOutput(String filename, WritableByteChannel channel, ByteBuffer buffer) throws IOException Creates a new data output for the given channel and using the given buffer.- Parameters:
filename- a file identifier used only for formatting error message.channel- the channel where data are written.buffer- the buffer where to put the data.- Throws:
IOException- if an error occurred while creating the data output.
-
-
Method Details
-
ensureBufferAccepts
Makes sure that the buffer can accept at least n more bytes. It is caller's responsibility to ensure that the given number of bytes is not greater than the buffer capacity.After this method call, the buffer limit will be equal or greater than
position + n.- Parameters:
n- the minimal number of additional bytes that the buffer shall accept.- Throws:
IOException- if an error occurred while writing to the channel.
-
getStreamPosition
public long getStreamPosition()Returns the current byte position of the stream.- Specified by:
getStreamPositionin interfaceMarkable- Overrides:
getStreamPositionin classChannelData- Returns:
- the position of the stream.
-
writeBit
Writes a single bit. This method uses only the rightmost bit of the given argument; the upper 31 bits are ignored.- Parameters:
bit- the bit to write (rightmost bit).- Throws:
IOException- if an error occurred while creating the data output.
-
writeBits
Writes a sequence of bits. This method uses only thenumBitsrightmost bits; other bits are ignored.- Parameters:
bits- the bits to write (rightmost bits).numBits- the number of bits to write.- Throws:
IOException- if an error occurred while creating the data output.
-
writeByte
Writes the 8 low-order bits ofvto the stream. The 24 high-order bits ofvare ignored. This method ensures that there is space for at least 1 byte in the buffer, (writing previous bytes into the channel if necessary), then delegates toByteBuffer.put(byte).- Parameters:
value- byte to be written.- Throws:
IOException- if some I/O exception occurs during writing.
-
writeShort
Writes the 16 low-order bits of value to the stream. The 16 high-order bits ofvare ignored. This method ensures that there is space for at least 2 bytes in the buffer, (writing previous bytes into the channel if necessary), then delegates toByteBuffer.putShort(short).- Parameters:
value- short integer to be written.- Throws:
IOException- if some I/O exception occurs during writing.
-
writeChar
Writes char value (16 bits) into the steam. This method ensures that there is space for at least 2 bytes in the buffer, (writing previous bytes into the channel if necessary), then delegates toByteBuffer.putChar(char).- Parameters:
value- character to be written.- Throws:
IOException- if some I/O exception occurs during writing.
-
writeInt
Writes integer value (32 bits) into the steam. This method ensures that there is space for at least 4 bytes in the buffer, (writing previous bytes into the channel if necessary), then delegates toByteBuffer.putInt(int).- Parameters:
value- integer to be written.- Throws:
IOException- if some I/O exception occurs during writing.
-
writeLong
Writes long value (64 bits) into the steam. This method ensures that there is space for at least 4 bytes in the buffer, (writing previous bytes into the channel if necessary), then delegates toByteBuffer.putLong(long).- Parameters:
value- long integer to be written.- Throws:
IOException- if some I/O exception occurs during writing.
-
writeFloat
Writes float value (32 bits) into the steam. This method ensures that there is space for at least 4 bytes in the buffer, (writing previous bytes into the channel if necessary), then delegates toByteBuffer.putFloat(float).- Parameters:
value- floating point value to be written.- Throws:
IOException- if some I/O exception occurs during writing.
-
writeDouble
Writes double value (64 bits) into the steam. This method ensures that there is space for at least 8 bytes in the buffer, (writing previous bytes into the channel if necessary), then delegates toByteBuffer.putDouble(double).- Parameters:
value- double precision floating point value to be written.- Throws:
IOException- if some I/O exception occurs during writing.
-
write
Writes all bytes from the given array into the stream. The implementation is as below:- Parameters:
src- an array of bytes to be written into stream.- Throws:
IOException- if an error occurred while writing the stream.
-
writeShorts
Writes all shorts from the given array into the stream. The implementation is as below:- Parameters:
src- an array of shorts to be written into stream.- Throws:
IOException- if an error occurred while writing the stream.
-
writeChars
Writes all characters from the given array into the stream. The implementation is as below:- Parameters:
src- an array of characters to be written into stream.- Throws:
IOException- if an error occurred while writing the stream.
-
writeInts
Writes all integers from the given array into the stream. The implementation is as below:- Parameters:
src- an array of integers to be written into stream.- Throws:
IOException- if an error occurred while writing the stream.
-
writeLongs
Writes all longs from the given array into the stream. The implementation is as below:- Parameters:
src- an array of longs to be written into stream.- Throws:
IOException- if an error occurred while writing the stream.
-
writeFloats
Writes all floats from the given array into the stream. The implementation is as below:- Parameters:
src- an array of floats to be written into stream.- Throws:
IOException- if an error occurred while writing the stream.
-
writeDoubles
Writes all doubles from the given array into the stream. The implementation is as below:- Parameters:
src- an array of doubles to be written into stream.- Throws:
IOException- if an error occurred while writing the stream.
-
write
Writeslengthbytes starting at indexoffsetfrom the given array.- Parameters:
src- an array containing the bytes to write.offset- index withinsrcof the first byte to write.length- the number of bytes to write.- Throws:
IOException- if an error occurred while writing the stream.
-
writeChars
Writeslengthchars starting at indexoffsetfrom the given array.- Parameters:
src- an array containing the characters to write.offset- index withinsrcof the first char to write.length- the number of chars to write.- Throws:
IOException- if an error occurred while writing the stream.
-
writeShorts
Writeslengthshorts starting at indexoffsetfrom the given array.- Parameters:
src- an array containing the shorts to write.offset- index withinsrcof the first short to write.length- the number of shorts to write.- Throws:
IOException- if an error occurred while writing the stream.
-
writeInts
Writeslengthintegers starting at indexoffsetfrom the given array.- Parameters:
src- an array containing the integers to write.offset- index withinsrcof the first integer to write.length- the number of integers to write.- Throws:
IOException- if an error occurred while writing the stream.
-
writeLongs
Writeslengthlongs starting at indexoffsetfrom the given array.- Parameters:
src- an array containing the longs to write.offset- index withinsrcof the first long to write.length- the number of longs to write.- Throws:
IOException- if an error occurred while writing the stream.
-
writeFloats
Writeslengthfloats starting at indexoffsetfrom the given array.- Parameters:
src- an array containing the floats to write.offset- index withinsrcof the first float to write.length- the number of floats to write.- Throws:
IOException- if an error occurred while writing the stream.
-
writeDoubles
Writeslengthdoubles starting at indexoffsetfrom the given array.- Parameters:
src- an array containing the doubles to write.offset- index withinsrcof the first double to write.length- the number of doubles to write.- Throws:
IOException- if an error occurred while writing the stream.
-
clear
private void clear()Fills the buffer with the zero values from its position up to the limit. After this method call, the position is undetermined and shall be set to a new value by the caller. -
seek
Moves to the given position in the stream, relative to the stream position at construction time. If the given position is greater than the stream length, then the values of bytes between the previous stream length and the given position are unspecified. The limit is unchanged.- Specified by:
seekin classChannelData- Parameters:
position- the position where to move.- Throws:
IOException- if the stream cannot be moved to the given position.
-
flush
Flushes thebuffercontent to the channel. This method does not flush the channel itself.- Specified by:
flushin interfaceFlushable- Throws:
IOException- if an error occurred while writing to the channel.
-
flushAndSetPosition
Writes the buffer content up to the given position, then set the buffer position to the given value. The buffer limit is unchanged, and the buffer offset is incremented by the given value.- Overrides:
flushAndSetPositionin classChannelData- Throws:
IOException
-
writeFully
Writes fully the buffer content from its position to its limit. After this method call, the buffer position is equal to its limit.- Throws:
IOException- if an error occurred while writing to the channel.
-