Package org.glassfish.grizzly.streams
Class AbstractStreamWriter
- java.lang.Object
-
- org.glassfish.grizzly.streams.AbstractStreamWriter
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Stream
,StreamWriter
- Direct Known Subclasses:
DefaultStreamWriter
,TransformerStreamWriter
public abstract class AbstractStreamWriter extends Object implements StreamWriter
Write the primitive Java type to the current ByteBuffer. If it doesn't fit, call the BufferHandler, and write to the result, which becomes the new current ByteBuffer. Arrays will be written across multiple ByteBuffers if necessary, but all primitives will be written to a single ByteBuffer.- Author:
- Ken Cavanaugh
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractStreamWriter.DisposeBufferCompletionHandler
-
Field Summary
Fields Modifier and Type Field Description protected boolean
isOutputBuffered
protected static Logger
logger
protected Output
output
protected static Integer
ZERO
protected static GrizzlyFuture<Integer>
ZERO_READY_FUTURE
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractStreamWriter(Connection connection, Output streamOutput)
Create a new ByteBufferWriter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
GrizzlyFuture<Integer>
close(CompletionHandler<Integer> completionHandler)
Close theStreamWriter
and make sure all data was flushed.<E> GrizzlyFuture<Stream>
encode(Transformer<E,Buffer> encoder, E object)
<E> GrizzlyFuture<Stream>
encode(Transformer<E,Buffer> encoder, E object, CompletionHandler<Stream> completionHandler)
GrizzlyFuture<Integer>
flush()
Cause the overflow handler to be called even if buffer is not full.GrizzlyFuture<Integer>
flush(CompletionHandler<Integer> completionHandler)
Cause the overflow handler to be called even if buffer is not full.Connection
getConnection()
Get theConnection
this StreamWriter belongs to.long
getTimeout(TimeUnit timeunit)
Get the timeout for StreamWriter I/O operations.boolean
isClosed()
Returns true, if StreamReader has been closed, or false otherwise.void
setTimeout(long timeout, TimeUnit timeunit)
Set the timeout for StreamWriter I/O operations.void
writeBoolean(boolean data)
Write the boolean value to the StreamWriter.void
writeBooleanArray(boolean[] data)
Write the array of boolean values to the StreamWriter.void
writeBuffer(Buffer b)
Write theBuffer
to the StreamWriter.void
writeByte(byte data)
Write the byte value to the StreamWriter.void
writeByteArray(byte[] data)
Write the array of byte values to the StreamWriter.void
writeByteArray(byte[] data, int offset, int length)
Write the part of array of byte values to the StreamWriter, using specific offset and length values.void
writeChar(char data)
Write the char value to the StreamWriter.void
writeCharArray(char[] data)
Write the array of char values to the StreamWriter.void
writeDouble(double data)
Write the double value to the StreamWriter.void
writeDoubleArray(double[] data)
Write the array of double values to the StreamWriter.void
writeFloat(float data)
Write the float value to the StreamWriter.void
writeFloatArray(float[] data)
Write the array of float values to the StreamWriter.void
writeInt(int data)
Write the int value to the StreamWriter.void
writeIntArray(int[] data)
Write the array of int values to the StreamWriter.void
writeLong(long data)
Write the long value to the StreamWriter.void
writeLongArray(long[] data)
Write the array of long values to the StreamWriter.void
writeShort(short data)
Write the short value to the StreamWriter.void
writeShortArray(short[] data)
Write the array of short values to the StreamWriter.
-
-
-
Field Detail
-
logger
protected static final Logger logger
-
ZERO
protected static final Integer ZERO
-
ZERO_READY_FUTURE
protected static final GrizzlyFuture<Integer> ZERO_READY_FUTURE
-
isOutputBuffered
protected final boolean isOutputBuffered
-
output
protected final Output output
-
-
Constructor Detail
-
AbstractStreamWriter
protected AbstractStreamWriter(Connection connection, Output streamOutput)
Create a new ByteBufferWriter. An instance maintains a current buffer for use in writing. Whenever the current buffer is insufficient to hold the required data, the BufferHandler is called, and the result of the handler is the new current buffer. The handler is responsible for the disposition of the contents of the old buffer.
-
-
Method Detail
-
flush
public GrizzlyFuture<Integer> flush() throws IOException
Cause the overflow handler to be called even if buffer is not full.- Specified by:
flush
in interfaceStreamWriter
- Throws:
IOException
-
flush
public GrizzlyFuture<Integer> flush(CompletionHandler<Integer> completionHandler) throws IOException
Cause the overflow handler to be called even if buffer is not full.- Specified by:
flush
in interfaceStreamWriter
- Throws:
IOException
-
isClosed
public boolean isClosed()
Returns true, if StreamReader has been closed, or false otherwise.- Specified by:
isClosed
in interfaceStreamWriter
- Returns:
- true, if StreamReader has been closed, or false otherwise.
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
close
public GrizzlyFuture<Integer> close(CompletionHandler<Integer> completionHandler) throws IOException
Close theStreamWriter
and make sure all data was flushed.- Specified by:
close
in interfaceStreamWriter
- Throws:
IOException
-
writeBuffer
public void writeBuffer(Buffer b) throws IOException
Write theBuffer
to the StreamWriter.- Specified by:
writeBuffer
in interfaceStreamWriter
- Parameters:
b
-Buffer
.- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean data) throws IOException
Write the boolean value to the StreamWriter.- Specified by:
writeBoolean
in interfaceStreamWriter
- Parameters:
data
- boolean value.- Throws:
IOException
-
writeByte
public void writeByte(byte data) throws IOException
Write the byte value to the StreamWriter.- Specified by:
writeByte
in interfaceStreamWriter
- Parameters:
data
- byte value.- Throws:
IOException
-
writeChar
public void writeChar(char data) throws IOException
Write the char value to the StreamWriter.- Specified by:
writeChar
in interfaceStreamWriter
- Parameters:
data
- char value.- Throws:
IOException
-
writeShort
public void writeShort(short data) throws IOException
Write the short value to the StreamWriter.- Specified by:
writeShort
in interfaceStreamWriter
- Parameters:
data
- short value.- Throws:
IOException
-
writeInt
public void writeInt(int data) throws IOException
Description copied from interface:StreamWriter
Write the int value to the StreamWriter.- Specified by:
writeInt
in interfaceStreamWriter
- Parameters:
data
- int value.- Throws:
IOException
-
writeLong
public void writeLong(long data) throws IOException
Write the long value to the StreamWriter.- Specified by:
writeLong
in interfaceStreamWriter
- Parameters:
data
- long value.- Throws:
IOException
-
writeFloat
public void writeFloat(float data) throws IOException
Write the float value to the StreamWriter.- Specified by:
writeFloat
in interfaceStreamWriter
- Parameters:
data
- float value.- Throws:
IOException
-
writeDouble
public void writeDouble(double data) throws IOException
Write the double value to the StreamWriter.- Specified by:
writeDouble
in interfaceStreamWriter
- Parameters:
data
- double value.- Throws:
IOException
-
writeBooleanArray
public void writeBooleanArray(boolean[] data) throws IOException
Write the array of boolean values to the StreamWriter.- Specified by:
writeBooleanArray
in interfaceStreamWriter
- Parameters:
data
- array of boolean values.- Throws:
IOException
-
writeByteArray
public void writeByteArray(byte[] data) throws IOException
Write the array of byte values to the StreamWriter.- Specified by:
writeByteArray
in interfaceStreamWriter
- Parameters:
data
- array of byte values.- Throws:
IOException
-
writeByteArray
public void writeByteArray(byte[] data, int offset, int length) throws IOException
Write the part of array of byte values to the StreamWriter, using specific offset and length values.- Specified by:
writeByteArray
in interfaceStreamWriter
- Parameters:
data
- array of byte values.offset
- array offset to start from.length
- number of bytes to write.- Throws:
IOException
-
writeCharArray
public void writeCharArray(char[] data) throws IOException
Write the array of char values to the StreamWriter.- Specified by:
writeCharArray
in interfaceStreamWriter
- Parameters:
data
- array of char values.- Throws:
IOException
-
writeShortArray
public void writeShortArray(short[] data) throws IOException
Write the array of short values to the StreamWriter.- Specified by:
writeShortArray
in interfaceStreamWriter
- Parameters:
data
- array of short values.- Throws:
IOException
-
writeIntArray
public void writeIntArray(int[] data) throws IOException
Write the array of int values to the StreamWriter.- Specified by:
writeIntArray
in interfaceStreamWriter
- Parameters:
data
- array of int values.- Throws:
IOException
-
writeLongArray
public void writeLongArray(long[] data) throws IOException
Write the array of long values to the StreamWriter.- Specified by:
writeLongArray
in interfaceStreamWriter
- Parameters:
data
- array of long values.- Throws:
IOException
-
writeFloatArray
public void writeFloatArray(float[] data) throws IOException
Write the array of float values to the StreamWriter.- Specified by:
writeFloatArray
in interfaceStreamWriter
- Parameters:
data
- array of float values.- Throws:
IOException
-
writeDoubleArray
public void writeDoubleArray(double[] data) throws IOException
Write the array of double values to the StreamWriter.- Specified by:
writeDoubleArray
in interfaceStreamWriter
- Parameters:
data
- array of double values.- Throws:
IOException
-
encode
public <E> GrizzlyFuture<Stream> encode(Transformer<E,Buffer> encoder, E object) throws IOException
- Specified by:
encode
in interfaceStreamWriter
- Throws:
IOException
-
encode
public <E> GrizzlyFuture<Stream> encode(Transformer<E,Buffer> encoder, E object, CompletionHandler<Stream> completionHandler) throws IOException
- Specified by:
encode
in interfaceStreamWriter
- Throws:
IOException
-
getConnection
public Connection getConnection()
Get theConnection
this StreamWriter belongs to.- Specified by:
getConnection
in interfaceStream
- Specified by:
getConnection
in interfaceStreamWriter
- Returns:
- the
Connection
this StreamWriter belongs to.
-
getTimeout
public long getTimeout(TimeUnit timeunit)
Get the timeout for StreamWriter I/O operations.- Specified by:
getTimeout
in interfaceStreamWriter
- Parameters:
timeunit
- timeout unitTimeUnit
.- Returns:
- the timeout for StreamWriter I/O operations.
-
setTimeout
public void setTimeout(long timeout, TimeUnit timeunit)
Set the timeout for StreamWriter I/O operations.- Specified by:
setTimeout
in interfaceStreamWriter
- Parameters:
timeout
- the timeout for StreamWriter I/O operations.timeunit
- timeout unitTimeUnit
.
-
-