Class AbstractWriteBuffer.AbstractBufferOutput
- java.lang.Object
-
- java.io.OutputStream
-
- com.tangosol.io.AbstractWriteBuffer.AbstractBufferOutput
-
- All Implemented Interfaces:
com.oracle.coherence.common.io.OutputStreaming
,OutputStreaming
,WriteBuffer.BufferOutput
,Closeable
,DataOutput
,Flushable
,AutoCloseable
- Direct Known Subclasses:
ByteArrayWriteBuffer.ByteArrayBufferOutput
,ByteBufferWriteBuffer.ByteBufferOutput
,DelegatingWriteBuffer.DelegatingBufferOutput
,MultiBufferWriteBuffer.MultiBufferOutput
- Enclosing class:
- AbstractWriteBuffer
public class AbstractWriteBuffer.AbstractBufferOutput extends OutputStream implements WriteBuffer.BufferOutput
AbstractBufferOutput is a concrete implementation of BufferOutput for the non-concrete AbstractWriteBuffer implementation.- Author:
- cp 2005.03.24
-
-
Field Summary
Fields Modifier and Type Field Description protected int
m_ofWrite
Current write offset within the containing WriteBuffer.-
Fields inherited from interface com.tangosol.io.WriteBuffer.BufferOutput
MAX_PACKED_INT_SIZE, MAX_PACKED_LONG_SIZE
-
-
Constructor Summary
Constructors Constructor Description AbstractBufferOutput()
Construct an AbstractBufferOutput that will begin writing at the start of the containing WriteBuffer.AbstractBufferOutput(int of)
Construct an AbstractBufferOutput that will begin writing at the specified offset within the containing WriteBuffer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
calcUTF(String s)
Figure out how many bytes it will take to hold the passed String.void
close()
Close the OutputStream and release any system resources associated with it.void
flush()
Flushes this OutputStream and forces any buffered output bytes to be written.protected int
formatUTF(byte[] ab, int ofb, char[] ach, int cch)
Format the passed characters as UTF into the passed byte array.protected void
formatUTF(byte[] ab, int ofb, int cb, String s)
Format the passed String as UTF into the passed byte array.WriteBuffer
getBuffer()
Get the WriteBuffer object that this BufferOutput is writing to.protected char[]
getCharBuf()
Obtain a temp buffer used to avoid allocations fromString.toCharArray()
and repeated calls toString.charAt(int)
.int
getOffset()
Determine the current offset of this BufferOutput within the underlying WriteBuffer.void
setOffset(int of)
Specify the offset of the next byte to write to the underlying WriteBuffer.void
write(byte[] ab)
Writes all the bytes in the arrayab
.void
write(byte[] ab, int of, int cb)
Writescb
bytes starting at offsetof
from the arrayab
.void
write(int b)
Writes the eight low-order bits of the argumentb
.void
writeBoolean(boolean f)
Writes the boolean value f.void
writeBuffer(ReadBuffer buf)
Write all the bytes from the passed ReadBuffer object.void
writeBuffer(ReadBuffer buf, int of, int cb)
Writecb
bytes from the passed ReadBuffer object starting at offsetof
within the passed ReadBuffer.void
writeByte(int b)
Writes the eight low-order bits of the argument b.void
writeBytes(String s)
Writes the String s, but only the low-order byte from each character of the String is written.void
writeChar(int ch)
Writes a char value, comprised of the 16 low-order bits of the argument ch; the 16 high-order bits of ch are ignored.void
writeChars(String s)
Writes the String s as a sequence of characters.void
writeDouble(double dfl)
Writes a double value.void
writeFloat(float fl)
Writes a float value.void
writeInt(int n)
Writes an int value.void
writeLong(long l)
Writes a long value.void
writePackedInt(int n)
Write an int value using a variable-length storage-format.void
writePackedLong(long l)
Write a long value using a variable-length storage-format.void
writeSafeUTF(String s)
Write a variable-length encoded UTF packed String.void
writeShort(int n)
Writes a short value, comprised of the 16 low-order bits of the argument n; the 16 high-order bits of n are ignored.void
writeStream(InputStreaming stream)
Write the remaining contents of the specified InputStreaming object.void
writeStream(InputStreaming stream, int cb)
Write the specified number of bytes of the specified InputStreaming object.void
writeUTF(String s)
Writes the String s as a sequence of characters, but using UTF-8 encoding for the characters, and including the String length data so that the correspondingDataInput.readUTF()
method can reconstitute a String from the written data.protected void
writeUTF(String s, int cch, int cb)
Write out the characters of the passed String as UTF-8 data.-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
AbstractBufferOutput
public AbstractBufferOutput()
Construct an AbstractBufferOutput that will begin writing at the start of the containing WriteBuffer.
-
AbstractBufferOutput
public AbstractBufferOutput(int of)
Construct an AbstractBufferOutput that will begin writing at the specified offset within the containing WriteBuffer.- Parameters:
of
- the offset at which to begin writing
-
-
Method Detail
-
write
public void write(int b) throws IOException
Writes the eight low-order bits of the argumentb
. The 24 high-order bits ofb
are ignored.- Specified by:
write
in interfaceDataOutput
- Specified by:
write
in interfacecom.oracle.coherence.common.io.OutputStreaming
- Specified by:
write
in classOutputStream
- Parameters:
b
- the byte to write (passed as an integer)- Throws:
IOException
- if an I/O error occurs
-
write
public void write(byte[] ab) throws IOException
Writes all the bytes in the arrayab
.- Specified by:
write
in interfaceDataOutput
- Specified by:
write
in interfacecom.oracle.coherence.common.io.OutputStreaming
- Overrides:
write
in classOutputStream
- Parameters:
ab
- the byte array to write- Throws:
IOException
- if an I/O error occurs
-
write
public void write(byte[] ab, int of, int cb) throws IOException
Writescb
bytes starting at offsetof
from the arrayab
.- Specified by:
write
in interfaceDataOutput
- Specified by:
write
in interfacecom.oracle.coherence.common.io.OutputStreaming
- Overrides:
write
in classOutputStream
- Parameters:
ab
- the byte array to write fromof
- the offset intoab
to start writing fromcb
- the number of bytes fromab
to write- Throws:
IOException
- if an I/O error occurs
-
flush
public void flush() throws IOException
Flushes this OutputStream and forces any buffered output bytes to be written.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in interfacecom.oracle.coherence.common.io.OutputStreaming
- Overrides:
flush
in classOutputStream
- Throws:
IOException
- if an I/O error occurs
-
close
public void close() throws IOException
Close the OutputStream and release any system resources associated with it.BufferOutput implementations do not pass this call down onto an underlying stream, if any.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfacecom.oracle.coherence.common.io.OutputStreaming
- Specified by:
close
in interfaceWriteBuffer.BufferOutput
- Overrides:
close
in classOutputStream
- Throws:
IOException
- if an I/O error occurs
-
writeBoolean
public void writeBoolean(boolean f) throws IOException
Writes the boolean value f.- Specified by:
writeBoolean
in interfaceDataOutput
- Specified by:
writeBoolean
in interfaceWriteBuffer.BufferOutput
- Parameters:
f
- the boolean to be written- Throws:
IOException
- if an I/O error occurs
-
writeByte
public void writeByte(int b) throws IOException
Writes the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.- Specified by:
writeByte
in interfaceDataOutput
- Specified by:
writeByte
in interfaceWriteBuffer.BufferOutput
- Parameters:
b
- the byte to write (passed as an integer)- Throws:
IOException
- if an I/O error occurs
-
writeShort
public void writeShort(int n) throws IOException
Writes a short value, comprised of the 16 low-order bits of the argument n; the 16 high-order bits of n are ignored.- Specified by:
writeShort
in interfaceDataOutput
- Specified by:
writeShort
in interfaceWriteBuffer.BufferOutput
- Parameters:
n
- the short to write (passed as an integer)- Throws:
IOException
- if an I/O error occurs
-
writeChar
public void writeChar(int ch) throws IOException
Writes a char value, comprised of the 16 low-order bits of the argument ch; the 16 high-order bits of ch are ignored.- Specified by:
writeChar
in interfaceDataOutput
- Specified by:
writeChar
in interfaceWriteBuffer.BufferOutput
- Parameters:
ch
- the char to write (passed as an integer)- Throws:
IOException
- if an I/O error occurs
-
writeInt
public void writeInt(int n) throws IOException
Writes an int value.- Specified by:
writeInt
in interfaceDataOutput
- Specified by:
writeInt
in interfaceWriteBuffer.BufferOutput
- Parameters:
n
- the int to write- Throws:
IOException
- if an I/O error occurs
-
writeLong
public void writeLong(long l) throws IOException
Writes a long value.- Specified by:
writeLong
in interfaceDataOutput
- Specified by:
writeLong
in interfaceWriteBuffer.BufferOutput
- Parameters:
l
- the long to write- Throws:
IOException
- if an I/O error occurs
-
writeFloat
public void writeFloat(float fl) throws IOException
Writes a float value.- Specified by:
writeFloat
in interfaceDataOutput
- Specified by:
writeFloat
in interfaceWriteBuffer.BufferOutput
- Parameters:
fl
- the float to write- Throws:
IOException
- if an I/O error occurs
-
writeDouble
public void writeDouble(double dfl) throws IOException
Writes a double value.- Specified by:
writeDouble
in interfaceDataOutput
- Specified by:
writeDouble
in interfaceWriteBuffer.BufferOutput
- Parameters:
dfl
- the double to write- Throws:
IOException
- if an I/O error occurs
-
writeBytes
public void writeBytes(String s) throws IOException
Writes the String s, but only the low-order byte from each character of the String is written.- Specified by:
writeBytes
in interfaceDataOutput
- Specified by:
writeBytes
in interfaceWriteBuffer.BufferOutput
- Parameters:
s
- the String to write- Throws:
IOException
- if an I/O error occurs
-
writeChars
public void writeChars(String s) throws IOException
Writes the String s as a sequence of characters.- Specified by:
writeChars
in interfaceDataOutput
- Specified by:
writeChars
in interfaceWriteBuffer.BufferOutput
- Parameters:
s
- the String to write- Throws:
IOException
- if an I/O error occurs
-
writeUTF
public void writeUTF(String s) throws IOException
Writes the String s as a sequence of characters, but using UTF-8 encoding for the characters, and including the String length data so that the correspondingDataInput.readUTF()
method can reconstitute a String from the written data.- Specified by:
writeUTF
in interfaceDataOutput
- Specified by:
writeUTF
in interfaceWriteBuffer.BufferOutput
- Parameters:
s
- the String to write- Throws:
IOException
- if an I/O error occurs
-
getBuffer
public WriteBuffer getBuffer()
Get the WriteBuffer object that this BufferOutput is writing to.- Specified by:
getBuffer
in interfaceWriteBuffer.BufferOutput
- Returns:
- the underlying WriteBuffer object
-
writeSafeUTF
public void writeSafeUTF(String s) throws IOException
Write a variable-length encoded UTF packed String. The major differences between this implementation and DataOutput is that this implementation supports null values and is not limited to 64KB UTF-encoded values.The binary format for a Safe UTF value is a "packed int" for the binary length followed by the UTF-encoded byte stream. The length is either -1 (indicating a null String) or in the range 0 .. Integer.MAX_VALUE (inclusive). The UTF-encoded portion uses a format identical to DataOutput.
- Specified by:
writeSafeUTF
in interfaceWriteBuffer.BufferOutput
- Parameters:
s
- a String value to write; may be null- Throws:
IOException
- if an I/O error occurs
-
writePackedInt
public void writePackedInt(int n) throws IOException
Write an int value using a variable-length storage-format.The format differs from DataOutput in that DataOutput always uses a fixed-length 4-byte Big Endian binary format for int values. The "packed" format includes a sign bit (0x40) and a continuation bit (0x80) in the first byte, followed by the least 6 significant bits of the int value. Subsequent bytes (each appearing only if the previous byte had its continuation bit set) include a continuation bit (0x80) and the next least 7 significant bits of the int value. In this way, a 32-bit value is encoded into 1-5 bytes, depending on the magnitude of the int value being encoded.
- Specified by:
writePackedInt
in interfaceWriteBuffer.BufferOutput
- Parameters:
n
- an int value to write- Throws:
IOException
- if an I/O error occurs
-
writePackedLong
public void writePackedLong(long l) throws IOException
Write a long value using a variable-length storage-format.The format differs from DataOutput in that DataOutput always uses a fixed-length 8-byte Big Endian binary format for long values. The "packed" format includes a sign bit (0x40) and a continuation bit (0x80) in the first byte, followed by the least 6 significant bits of the long value. Subsequent bytes (each appearing only if the previous byte had its continuation bit set) include a continuation bit (0x80) and the next least 7 significant bits of the long value. In this way, a 64-bit value is encoded into 1-10 bytes, depending on the magnitude of the long value being encoded.
- Specified by:
writePackedLong
in interfaceWriteBuffer.BufferOutput
- Parameters:
l
- a long value to write- Throws:
IOException
- if an I/O error occurs
-
writeBuffer
public void writeBuffer(ReadBuffer buf) throws IOException
Write all the bytes from the passed ReadBuffer object.This is functionally equivalent to the following code:
getBuffer().write(getOffset(), buf);
- Specified by:
writeBuffer
in interfaceWriteBuffer.BufferOutput
- Parameters:
buf
- a ReadBuffer object- Throws:
IOException
- if an I/O error occurs
-
writeBuffer
public void writeBuffer(ReadBuffer buf, int of, int cb) throws IOException
Writecb
bytes from the passed ReadBuffer object starting at offsetof
within the passed ReadBuffer.This is functionally equivalent to the following code:
getBuffer().write(getOffset(), buf, of, cb);
- Specified by:
writeBuffer
in interfaceWriteBuffer.BufferOutput
- Parameters:
buf
- a ReadBuffer objectof
- the offset within the ReadBuffer of the first byte to write to this BufferOutputcb
- the number of bytes to write- Throws:
IOException
- if an I/O error occurs
-
writeStream
public void writeStream(InputStreaming stream) throws IOException
Write the remaining contents of the specified InputStreaming object.This is functionally equivalent to the following code:
getBuffer().write(getOffset(), stream);
- Specified by:
writeStream
in interfaceWriteBuffer.BufferOutput
- Parameters:
stream
- the stream of bytes to write to this BufferOutput- Throws:
IOException
- if an I/O error occurs, specifically if an IOException occurs reading from the passed stream
-
writeStream
public void writeStream(InputStreaming stream, int cb) throws IOException
Write the specified number of bytes of the specified InputStreaming object.This is functionally equivalent to the following code:
getBuffer().write(getOffset(), stream, cb);
- Specified by:
writeStream
in interfaceWriteBuffer.BufferOutput
- Parameters:
stream
- the stream of bytes to write to this BufferOutputcb
- the exact number of bytes to read from the stream and write to this BufferOutput- Throws:
EOFException
- if the stream is exhausted before the number of bytes indicated could be readIOException
- if an I/O error occurs, specifically if an IOException occurs reading from the passed stream
-
getOffset
public final int getOffset()
Determine the current offset of this BufferOutput within the underlying WriteBuffer.- Specified by:
getOffset
in interfaceWriteBuffer.BufferOutput
- Returns:
- the offset of the next byte to write to the WriteBuffer
-
setOffset
public void setOffset(int of)
Specify the offset of the next byte to write to the underlying WriteBuffer.- Specified by:
setOffset
in interfaceWriteBuffer.BufferOutput
- Parameters:
of
- the offset of the next byte to write to the WriteBuffer
-
writeUTF
protected void writeUTF(String s, int cch, int cb) throws IOException
Write out the characters of the passed String as UTF-8 data.- Parameters:
s
- the Stringcch
- the number of characterscb
- the number of bytes as returned from calcUTF- Throws:
IOException
- if an I/O error occurs
-
getCharBuf
protected final char[] getCharBuf()
Obtain a temp buffer used to avoid allocations fromString.toCharArray()
and repeated calls toString.charAt(int)
.- Returns:
- a char buffer of CHAR_BUF_SIZE characters long
-
calcUTF
protected final int calcUTF(String s)
Figure out how many bytes it will take to hold the passed String.This method is tightly bound to formatUTF.
- Parameters:
s
- the String- Returns:
- the binary UTF length
-
formatUTF
protected final void formatUTF(byte[] ab, int ofb, int cb, String s)
Format the passed String as UTF into the passed byte array.This method is tightly bound to calcUTF.
- Parameters:
ab
- the byte array to format intoofb
- the offset into the byte array to write the first bytecb
- the number of bytes that the UTF will take as returned by calcUTFs
- the String
-
formatUTF
protected final int formatUTF(byte[] ab, int ofb, char[] ach, int cch)
Format the passed characters as UTF into the passed byte array.- Parameters:
ab
- the byte array to format intoofb
- the offset into the byte array to write the first byteach
- the array of characters to formatcch
- the number of characters to format- Returns:
- cb the number of bytes written to the array
-
-