org.glassfish.grizzly.utils
Class BufferOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.glassfish.grizzly.utils.BufferOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class BufferOutputStream
extends OutputStream

OutputStream implementation to write to a Buffer.


Constructor Summary
BufferOutputStream(MemoryManager mm)
          Creates the BufferOutputStream without initial Buffer.
BufferOutputStream(MemoryManager mm, Buffer buffer)
          Creates the BufferOutputStream using passed Buffer as initial.
BufferOutputStream(MemoryManager mm, Buffer buffer, boolean reallocate)
          Creates the BufferOutputStream using passed Buffer as initial.
 
Method Summary
protected  Buffer allocateNewBuffer(MemoryManager memoryManager, int size)
           
 void close()
           
 void flush()
           
 Buffer getBuffer()
          Get the result Buffer (not flipped).
 boolean isReallocate()
          Returns true if "reallocate" strategy is used or false otherwise.
 void reset()
           
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BufferOutputStream

public BufferOutputStream(MemoryManager mm)
Creates the BufferOutputStream without initial Buffer. Created BufferOutputStream won't use "reallocate" strategy, which means if internal Buffer window gets overloaded - it will be appended to the CompositeBuffer and new window will be allocated.

Parameters:
mm - MemoryManager

BufferOutputStream

public BufferOutputStream(MemoryManager mm,
                          Buffer buffer)
Creates the BufferOutputStream using passed Buffer as initial. Created BufferOutputStream won't use "reallocate" strategy, which means if internal Buffer window gets overloaded - it will be appended to the CompositeBuffer and new window will be allocated.

Parameters:
mm - MemoryManager
buffer - initial Buffer

BufferOutputStream

public BufferOutputStream(MemoryManager mm,
                          Buffer buffer,
                          boolean reallocate)
Creates the BufferOutputStream using passed Buffer as initial. Created BufferOutputStream can choose whether use or not "reallocate" strategy. Using "reallocate" strategy means following: if internal Buffer window gets overloaded - it will be reallocated using MemoryManager.reallocate(org.glassfish.grizzly.Buffer, int), which may lead to extra memory allocation and bytes copying (bigger memory chunk might be allocated to keep original stream data plus extra data been written to stream), the benefit of this is that stream data will be located in the single memory chunk. If we don't use "reallocate" strategy - when internal Buffer gets overloaded it will be appended to the CompositeBuffer and new window Buffer will be allocated then.

Parameters:
mm - MemoryManager
buffer - initial Buffer
Method Detail

getBuffer

public Buffer getBuffer()
Get the result Buffer (not flipped).

Returns:
the result Buffer (not flipped).

isReallocate

public boolean isReallocate()
Returns true if "reallocate" strategy is used or false otherwise. Using "reallocate" strategy means following: if internal Buffer window gets overloaded - it will be reallocated using MemoryManager.reallocate(org.glassfish.grizzly.Buffer, int), which may lead to extra memory allocation and bytes copying (bigger memory chunk might be allocated to keep original stream data plus extra data been written to stream), the benefit of this is that stream data will be located in the single memory chunk. If we don't use "reallocate" strategy - when internal Buffer gets overloaded it will be appended to the CompositeBuffer and new window Buffer will be allocated then.

Returns:
true if "reallocate" strategy is used or false otherwise.

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

reset

public void reset()

allocateNewBuffer

protected Buffer allocateNewBuffer(MemoryManager memoryManager,
                                   int size)


Copyright © 2012 Oracle Corporation. All Rights Reserved.