Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

edu.emory.mathcs.util.io
Class BufferedPipe  view BufferedPipe download BufferedPipe.java

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 edu.emory.mathcs.util.allocator.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

Nested Class Summary
(package private) static class BufferedPipe.Chunk
           
private  class BufferedPipe.PipeRedirectibleInputStream
           
 
Field Summary
(package private)  edu.emory.mathcs.util.allocator.Allocator allocator
           
(package private)  BufferedPipe.Chunk begChunk
           
(package private)  int chunksize
           
private static int DATA
           
(package private) static edu.emory.mathcs.util.allocator.Allocator defaultAllocator
          Default allocator with maximum memory footprint of 10 MB.
(package private)  java.lang.Object emptyLock
           
(package private)  BufferedPipe.Chunk endChunk
           
private static int EOF
           
(package private)  java.lang.Object fullLock
           
protected  java.io.InputStream sink
          Sink of the pipe.
(package private)  boolean sinkClosed
           
protected  java.io.OutputStream source
          Source of the pipe.
(package private)  boolean sourceClosed
           
private static int TIMEOUT
           
(package private)  java.lang.Thread writerThread
           
 
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(edu.emory.mathcs.util.allocator.Allocator allocator)
          Creates a new pipe with specified allocator and a default initial chunk size of 8 KB.
BufferedPipe(edu.emory.mathcs.util.allocator.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
private  void checkWriteConsistency()
           
(package private)  void closeSink()
           
(package private)  void closeSource()
           
private  int ensureData(long timeout)
          Waits until there is at least one byte available for reading in current chunk
private  void ensureSpace()
          Called only by write() operations
private  void fireIOStateChanged()
           
private  boolean isEmpty()
          Used only by read() operations.
 long length()
          Returns the current length of the pipe, i.e.
private static java.io.InterruptedIOException newInterruptedIOException(java.lang.InterruptedException e)
           
(package private)  int read(byte[] buf, int off, int len, long timeout)
           
(package private)  int read(long timeout)
           
(package private)  int read(java.io.OutputStream os, int len, long timeout)
           
 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.
(package private)  void write(byte[] buf, int off, int len)
           
(package private)  void write(int v)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultAllocator

static edu.emory.mathcs.util.allocator.Allocator defaultAllocator
Default allocator with maximum memory footprint of 10 MB.


allocator

final edu.emory.mathcs.util.allocator.Allocator allocator

begChunk

volatile BufferedPipe.Chunk begChunk

endChunk

volatile BufferedPipe.Chunk endChunk

sourceClosed

volatile boolean sourceClosed

sinkClosed

volatile boolean sinkClosed

chunksize

volatile int chunksize

emptyLock

final java.lang.Object emptyLock

fullLock

final java.lang.Object fullLock

source

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


sink

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


writerThread

volatile java.lang.Thread writerThread

DATA

private static final int DATA
See Also:
Constant Field Values

EOF

private static final int EOF
See Also:
Constant Field Values

TIMEOUT

private static final int TIMEOUT
See Also:
Constant Field Values
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.


BufferedPipe

public BufferedPipe(edu.emory.mathcs.util.allocator.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.


BufferedPipe

public BufferedPipe(edu.emory.mathcs.util.allocator.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.

Method Detail

source

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


sink

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


length

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


read

int read(long timeout)
   throws java.io.IOException,
          edu.emory.mathcs.util.concurrent.TimeoutException

read

int read(byte[] buf,
         int off,
         int len,
         long timeout)
   throws java.io.IOException,
          edu.emory.mathcs.util.concurrent.TimeoutException

read

int read(java.io.OutputStream os,
         int len,
         long timeout)
   throws java.io.IOException,
          edu.emory.mathcs.util.concurrent.TimeoutException

write

void write(int v)
     throws java.io.IOException

write

void write(byte[] buf,
           int off,
           int len)
     throws java.io.IOException

closeSource

void closeSource()

closeSink

void closeSink()

fireIOStateChanged

private void fireIOStateChanged()

isEmpty

private boolean isEmpty()
Used only by read() operations.


ensureData

private int ensureData(long timeout)
                throws java.io.IOException
Waits until there is at least one byte available for reading in current chunk


ensureSpace

private void ensureSpace()
                  throws java.io.IOException
Called only by write() operations


checkWriteConsistency

private void checkWriteConsistency()
                            throws java.io.IOException

newInterruptedIOException

private static java.io.InterruptedIOException newInterruptedIOException(java.lang.InterruptedException e)