org.glassfish.grizzly.streams
Class AbstractStreamWriter

java.lang.Object
  extended by org.glassfish.grizzly.streams.AbstractStreamWriter
All Implemented Interfaces:
Closeable, StreamWriter
Direct Known Subclasses:
StreamWriterDecorator, TCPNIOStreamWriter, UDPNIOStreamWriter

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
static class AbstractStreamWriter.DisposeBufferCompletionHandler
           
 
Field Summary
protected  Buffer buffer
           
protected  int bufferSize
           
protected static Integer ZERO
           
protected static Future<Integer> ZERO_READY_FUTURE
           
 
Constructor Summary
protected AbstractStreamWriter()
           
protected AbstractStreamWriter(Connection connection)
          Create a new ByteBufferWriter.
 
Method Summary
 void close()
          
 Future<Integer> close(CompletionHandler<Integer> completionHandler)
          Close the StreamWriter and make sure all data was flushed.
protected abstract  Future<Integer> close0(CompletionHandler<Integer> completionHandler)
           
 void ensure(int size)
          Ensure that the requested amount of space is available
 Future<Integer> flush()
          Cause the overflow handler to be called even if buffer is not full.
 Future<Integer> flush(CompletionHandler<Integer> completionHandler)
          Cause the overflow handler to be called even if buffer is not full.
protected abstract  Future<Integer> flush0(Buffer buffer, CompletionHandler<Integer> completionHandler)
           
 Buffer getBuffer()
          Get the current Buffer, where the StreamWriter buffers output.
 int getBufferSize()
          Get the preferred Buffer size to be used for StreamWriter write operations.
 Connection getConnection()
          Get the Connection this StreamWriter belongs to.
 long getTimeout(TimeUnit timeunit)
          Get the timeout for StreamWriter I/O operations.
 boolean isBlocking()
          Returns the StreamReader mode.
protected  Buffer newBuffer(int size)
           
protected  Future<Integer> overflow()
           
protected  Future<Integer> overflow(CompletionHandler<Integer> completionHandler)
           
 void setBlocking(boolean isBlocking)
          Sets the StreamReader mode.
 void setBufferSize(int size)
          Set the preferred Buffer size to be used for StreamWriter write operations.
 void setConnection(Connection connection)
           
 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 the Buffer to the StreamWriter.
protected  void writeBuffer(Buffer b, CompletionHandler completionHandler)
           
 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.
 void writeStream(StreamReader streamReader)
          Puts StreamReader available data to this StreamWriter This method will make possible direct writing from StreamReader, avoiding Buffer copying.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZERO

protected static final Integer ZERO

ZERO_READY_FUTURE

protected static final Future<Integer> ZERO_READY_FUTURE

bufferSize

protected int bufferSize

buffer

protected Buffer buffer
Constructor Detail

AbstractStreamWriter

protected AbstractStreamWriter()

AbstractStreamWriter

protected AbstractStreamWriter(Connection connection)
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

isBlocking

public boolean isBlocking()
Returns the StreamReader mode. true, if StreamReader is operating in blocking mode, or false otherwise.

Specified by:
isBlocking in interface StreamWriter
Returns:
the StreamReader mode.

setBlocking

public void setBlocking(boolean isBlocking)
Sets the StreamReader mode.

Specified by:
setBlocking in interface StreamWriter
Parameters:
isBlocking - true, if StreamReader is operating in blocking mode, or false otherwise.

overflow

protected Future<Integer> overflow()
                            throws IOException
Throws:
IOException

overflow

protected Future<Integer> overflow(CompletionHandler<Integer> completionHandler)
                            throws IOException
Throws:
IOException

flush

public Future<Integer> flush()
                      throws IOException
Cause the overflow handler to be called even if buffer is not full.

Specified by:
flush in interface StreamWriter
Throws:
IOException

flush

public Future<Integer> flush(CompletionHandler<Integer> completionHandler)
                      throws IOException
Cause the overflow handler to be called even if buffer is not full.

Specified by:
flush in interface StreamWriter
Throws:
IOException

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Throws:
IOException

close

public Future<Integer> close(CompletionHandler<Integer> completionHandler)
                      throws IOException
Close the StreamWriter and make sure all data was flushed.

Specified by:
close in interface StreamWriter
Throws:
IOException

ensure

public void ensure(int size)
            throws IOException
Ensure that the requested amount of space is available

Throws:
IOException

writeBuffer

public void writeBuffer(Buffer b)
                 throws IOException
Write the Buffer to the StreamWriter.

Specified by:
writeBuffer in interface StreamWriter
Parameters:
b - Buffer.
Throws:
IOException

writeBuffer

protected void writeBuffer(Buffer b,
                           CompletionHandler completionHandler)
                    throws IOException
Throws:
IOException

writeStream

public void writeStream(StreamReader streamReader)
                 throws IOException
Puts StreamReader available data to this StreamWriter This method will make possible direct writing from StreamReader, avoiding Buffer copying.

Specified by:
writeStream in interface StreamWriter
Parameters:
streamReader - StreamReader
Throws:
IOException

writeBoolean

public void writeBoolean(boolean data)
                  throws IOException
Write the boolean value to the StreamWriter.

Specified by:
writeBoolean in interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
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 interface StreamWriter
Parameters:
data - array of double values.
Throws:
IOException

getConnection

public Connection getConnection()
Get the Connection this StreamWriter belongs to.

Specified by:
getConnection in interface StreamWriter
Returns:
the Connection this StreamWriter belongs to.

setConnection

public void setConnection(Connection connection)

getBuffer

public Buffer getBuffer()
Get the current Buffer, where the StreamWriter buffers output.

Specified by:
getBuffer in interface StreamWriter
Returns:
the current Buffer, where the StreamWriter buffers output.

newBuffer

protected Buffer newBuffer(int size)

getBufferSize

public int getBufferSize()
Get the preferred Buffer size to be used for StreamWriter write operations.

Specified by:
getBufferSize in interface StreamWriter
Returns:
the preferred Buffer size to be used for StreamWriter write operations.

setBufferSize

public void setBufferSize(int size)
Set the preferred Buffer size to be used for StreamWriter write operations.

Specified by:
setBufferSize in interface StreamWriter
Parameters:
size - the preferred Buffer size to be used for StreamWriter write operations.

getTimeout

public long getTimeout(TimeUnit timeunit)
Get the timeout for StreamWriter I/O operations.

Specified by:
getTimeout in interface StreamWriter
Parameters:
timeunit - timeout unit TimeUnit.
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 interface StreamWriter
Parameters:
timeout - the timeout for StreamWriter I/O operations.
timeunit - timeout unit TimeUnit.

flush0

protected abstract Future<Integer> flush0(Buffer buffer,
                                          CompletionHandler<Integer> completionHandler)
                                   throws IOException
Throws:
IOException

close0

protected abstract Future<Integer> close0(CompletionHandler<Integer> completionHandler)
                                   throws IOException
Throws:
IOException


Copyright © 2009 SUN Microsystems. All Rights Reserved.