Package org.eclipse.jgit.util
Class TemporaryBuffer
- java.lang.Object
-
- java.io.OutputStream
-
- org.eclipse.jgit.util.TemporaryBuffer
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
- Direct Known Subclasses:
TemporaryBuffer.Heap,TemporaryBuffer.LocalFile
public abstract class TemporaryBuffer extends java.io.OutputStreamA 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
Nested Classes Modifier and Type Class Description static classTemporaryBuffer.HeapA temporary buffer that will never exceed its in-memory limit.static classTemporaryBuffer.LocalFileA fully buffered output stream using local disk storage for large data.
-
Field Summary
Fields Modifier and Type Field Description protected static intDEFAULT_IN_CORE_LIMITDefault limit for in-core storage.
-
Constructor Summary
Constructors Modifier Constructor Description protectedTemporaryBuffer(int limit)Create a new empty temporary buffer.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclose()voidcopy(java.io.InputStream in)Copy all bytes remaining on the input stream into this buffer.voiddestroy()Clear this buffer so it has no data, and cannot be used again.protected voiddoFlush()Dumps the entire buffer into the overflow stream, and flushes it.longlength()Obtain the length (in bytes) of the buffer.java.io.InputStreamopenInputStream()Open an input stream to read from the buffered data.protected abstract java.io.OutputStreamoverflow()Open the overflow output stream, so the remaining output can be stored.voidreset()Reset this buffer for reuse, purging all buffered content.byte[]toByteArray()Convert this buffer's contents into a contiguous byte array.voidwrite(byte[] b, int off, int len)voidwrite(int b)voidwriteTo(java.io.OutputStream os, ProgressMonitor pm)Send this buffer to an output stream.
-
-
-
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
-
-
Method Detail
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
doFlush
protected void doFlush() throws java.io.IOExceptionDumps the entire buffer into the overflow stream, and flushes it.- Throws:
java.io.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(java.io.InputStream in) throws java.io.IOExceptionCopy 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.
-
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.IOExceptionConvert 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 filejava.lang.OutOfMemoryError- the buffer cannot fit in memory
-
writeTo
public void writeTo(java.io.OutputStream os, ProgressMonitor pm) throws java.io.IOExceptionSend 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.
-
openInputStream
public java.io.InputStream openInputStream() throws java.io.IOExceptionOpen 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:
java.io.IOException- an error occurred opening the temporary file.
-
reset
public void reset()
Reset this buffer for reuse, purging all buffered content.
-
overflow
protected abstract java.io.OutputStream overflow() throws java.io.IOExceptionOpen 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:
java.io.IOException- the buffer cannot create the overflow stream.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException
-
destroy
public void destroy()
Clear this buffer so it has no data, and cannot be used again.
-
-