edu.emory.mathcs.util.io
Class BufferedPipe

java.lang.Object
  extended byedu.emory.mathcs.util.io.BufferedPipe

public class BufferedPipe
extends java.lang.Object

In-memory pipe that enables buffered sequential data transfer between threads. Pipe has two ends: source and sink. Source is an output stream into which the writer writes bytes. Sink is the input stream from which reader reads bytes. Data that was wrote to the source but not yet read from the sink are kept in a pipe buffer.

This implementation features dynamic buffer sizing, so that memory consumption is minimized if there is little data to buffer. Buffer is upsized if more space is needed and downsized when data is read. Resizing never causes data copying.

This implementation supports concurrent reads and writes.

Memory usage limits and allocation policy can be controlled via provided Allocator. For example, multiple pipes can share allocator with fixed maximum memory footprint, thus limiting total memory used for I/O buffering.

Version:
1.0
Author:
Dawid Kurzyniec
See Also:
InProcServerSocket, InProcSocket

Field Summary
protected  java.io.InputStream sink
          Sink of the pipe.
protected  java.io.OutputStream source
          Source of the pipe.
 
Constructor Summary
BufferedPipe()
          Creates a new pipe with a default shared allocator with 10 MB footprint limit, and a default initial chunk size of 8 KB.
BufferedPipe(Allocator allocator)
          Creates a new pipe with specified allocator and a default initial chunk size of 8 KB.
BufferedPipe(Allocator allocator, int chunksize)
          Creates a new pipe with specified allocator and initial chunk size.
BufferedPipe(int chunksize)
          Creates a new pipe with a default shared allocator with 10 MB footprint limit, and with specified initial chunk size.
 
Method Summary
 long length()
          Returns the current length of the pipe, that is, the number of bytes buffered and available for read.
 java.io.InputStream sink()
          Returns the sink of this pipe, from where reader can read data.
 java.io.OutputStream source()
          Returns the source of this pipe, where writer can write data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

source

protected final java.io.OutputStream source
Source of the pipe.


sink

protected final java.io.InputStream sink
Sink of the pipe.

Constructor Detail

BufferedPipe

public BufferedPipe()
Creates a new pipe with a default shared allocator with 10 MB footprint limit, and a default initial chunk size of 8 KB.


BufferedPipe

public BufferedPipe(int chunksize)
Creates a new pipe with a default shared allocator with 10 MB footprint limit, and with specified initial chunk size. Specifying large initial chunk size increases initial memory footprint, but may slightly improve initial performance of bulk data transfer.

Parameters:
chunksize - the initial chunk size.

BufferedPipe

public BufferedPipe(Allocator allocator)
Creates a new pipe with specified allocator and a default initial chunk size of 8 KB. The allocator is used to provide memory buffers used by the pipe.

Parameters:
allocator - allocator that will provide memory buffers for this pipe

BufferedPipe

public BufferedPipe(Allocator allocator,
                    int chunksize)
Creates a new pipe with specified allocator and initial chunk size. The allocator is used to provide memory buffers used by the pipe. Specifying large initial chunk size increases initial memory footprint, but may slightly improve performance for bulk data transfer.

Parameters:
allocator - allocator that will provide memory buffers for this pipe
chunksize - the initial chunk size
Method Detail

source

public java.io.OutputStream source()
Returns the source of this pipe, where writer can write data.

Returns:
the source of this pipe

sink

public java.io.InputStream sink()
Returns the sink of this pipe, from where reader can read data.

Returns:
the sink of this pipe

length

public long length()
Returns the current length of the pipe, that is, the number of bytes buffered and available for read. This is a snapshot value only.

Returns:
the current length of the pipe