org.eclipse.jgit.util
Class TemporaryBuffer

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

public class TemporaryBuffer
extends java.io.OutputStream

A fully buffered output stream using local disk storage for large data.

Initially this output stream buffers to memory, like ByteArrayOutputStream might do, but it shifts to using an on disk temporary file if the output gets too large.

The content of this buffered stream may be sent to another OutputStream only after this stream has been properly closed by close().


Constructor Summary
TemporaryBuffer()
          Create a new empty temporary buffer.
 
Method Summary
 void close()
           
 void copy(java.io.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.
 long length()
          Obtain the length (in bytes) of the buffer.
 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(java.io.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
 

Constructor Detail

TemporaryBuffer

public TemporaryBuffer()
Create a new empty temporary buffer.

Method Detail

write

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

write

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

copy

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

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

close

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

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 java.io.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:
java.io.IOException - an error occurred reading from a local temporary file
java.lang.OutOfMemoryError - the buffer cannot fit in memory

writeTo

public void writeTo(java.io.OutputStream os,
                    ProgressMonitor pm)
             throws java.io.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:
java.io.IOException - an error occurred reading from a temporary file on the local system, or writing to the output stream.

destroy

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