org.eclipse.jgit.util
Class TemporaryBuffer

java.lang.Object
  extended by java.io.OutputStream
      extended by org.eclipse.jgit.util.TemporaryBuffer
All Implemented Interfaces:
Closeable, Flushable
Direct Known Subclasses:
TemporaryBuffer.Heap, TemporaryBuffer.LocalFile

public abstract class TemporaryBuffer
extends OutputStream

A fully buffered output stream.

Subclasses determine the behavior when the in-memory buffer capacity has been exceeded and additional bytes are still being received for output.


Nested Class Summary
static class TemporaryBuffer.Heap
          A temporary buffer that will never exceed its in-memory limit.
static class TemporaryBuffer.LocalFile
          A fully buffered output stream using local disk storage for large data.
 
Field Summary
protected static int DEFAULT_IN_CORE_LIMIT
          Default limit for in-core storage.
 
Constructor Summary
protected TemporaryBuffer(int limit)
          Create a new empty temporary buffer.
 
Method Summary
 void close()
           
 void copy(InputStream in)
          Copy all bytes remaining on the input stream into this buffer.
 void destroy()
          Clear this buffer so it has no data, and cannot be used again.
protected  void doFlush()
          Dumps the entire buffer into the overflow stream, and flushes it.
 long length()
          Obtain the length (in bytes) of the buffer.
 InputStream openInputStream()
          Open an input stream to read from the buffered data.
protected abstract  OutputStream overflow()
          Open the overflow output stream, so the remaining output can be stored.
 void reset()
          Reset this buffer for reuse, purging all buffered content.
 byte[] toByteArray()
          Convert this buffer's contents into a contiguous byte array.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 void writeTo(OutputStream os, ProgressMonitor pm)
          Send this buffer to an output stream.
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_IN_CORE_LIMIT

protected static final int DEFAULT_IN_CORE_LIMIT
Default limit for in-core storage.

See Also:
Constant Field Values
Constructor Detail

TemporaryBuffer

protected TemporaryBuffer(int limit)
Create a new empty temporary buffer.

Parameters:
limit - maximum number of bytes to store in memory before entering the overflow output path.
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
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

doFlush

protected void doFlush()
                throws IOException
Dumps the entire buffer into the overflow stream, and flushes it.

Throws:
IOException - the overflow stream cannot be started, or the buffer contents cannot be written to it, or it failed to flush.

copy

public void copy(InputStream in)
          throws IOException
Copy all bytes remaining on the input stream into this buffer.

Parameters:
in - the stream to read from, until EOF is reached.
Throws:
IOException - an error occurred reading from the input stream, or while writing to a local temporary file.

length

public long length()
Obtain the length (in bytes) of the buffer.

The length is only accurate after close() has been invoked.

Returns:
total length of the buffer, in bytes.

toByteArray

public byte[] toByteArray()
                   throws IOException
Convert this buffer's contents into a contiguous byte array.

The buffer is only complete after close() has been invoked.

Returns:
the complete byte array; length matches length().
Throws:
IOException - an error occurred reading from a local temporary file
OutOfMemoryError - the buffer cannot fit in memory

writeTo

public void writeTo(OutputStream os,
                    ProgressMonitor pm)
             throws IOException
Send this buffer to an output stream.

This method may only be invoked after close() has completed normally, to ensure all data is completely transferred.

Parameters:
os - stream to send this buffer's complete content to.
pm - if not null progress updates are sent here. Caller should initialize the task and the number of work units to length()/1024.
Throws:
IOException - an error occurred reading from a temporary file on the local system, or writing to the output stream.

openInputStream

public InputStream openInputStream()
                            throws IOException
Open an input stream to read from the buffered data.

This method may only be invoked after close() has completed normally, to ensure all data is completely transferred.

Returns:
a stream to read from the buffer. The caller must close the stream when it is no longer useful.
Throws:
IOException - an error occurred opening the temporary file.

reset

public void reset()
Reset this buffer for reuse, purging all buffered content.


overflow

protected abstract OutputStream overflow()
                                  throws IOException
Open the overflow output stream, so the remaining output can be stored.

Returns:
the output stream to receive the buffered content, followed by the remaining output.
Throws:
IOException - the buffer cannot create the overflow stream.

close

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

destroy

public void destroy()
Clear this buffer so it has no data, and cannot be used again.



Copyright © 2012. All Rights Reserved.