2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3 * and other copyright owners as documented in the project's IP log.
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 package org
.eclipse
.jgit
.util
;
46 import java
.io
.BufferedOutputStream
;
48 import java
.io
.FileInputStream
;
49 import java
.io
.FileOutputStream
;
50 import java
.io
.IOException
;
51 import java
.io
.InputStream
;
52 import java
.io
.OutputStream
;
53 import java
.util
.ArrayList
;
55 import org
.eclipse
.jgit
.lib
.NullProgressMonitor
;
56 import org
.eclipse
.jgit
.lib
.ProgressMonitor
;
59 * A fully buffered output stream using local disk storage for large data.
61 * Initially this output stream buffers to memory, like ByteArrayOutputStream
62 * might do, but it shifts to using an on disk temporary file if the output gets
65 * The content of this buffered stream may be sent to another OutputStream only
66 * after this stream has been properly closed by {@link #close()}.
68 public class TemporaryBuffer
extends OutputStream
{
69 static final int DEFAULT_IN_CORE_LIMIT
= 1024 * 1024;
71 /** Chain of data, if we are still completely in-core; otherwise null. */
72 private ArrayList
<Block
> blocks
;
75 * Maximum number of bytes we will permit storing in memory.
77 * When this limit is reached the data will be shifted to a file on disk,
78 * preventing the JVM heap from growing out of control.
80 private int inCoreLimit
;
83 * Location of our temporary file if we are on disk; otherwise null.
85 * If we exceeded the {@link #inCoreLimit} we nulled out {@link #blocks} and
86 * created this file instead. All output goes here through {@link #diskOut}.
88 private File onDiskFile
;
90 /** If writing to {@link #onDiskFile} this is a buffered stream to it. */
91 private OutputStream diskOut
;
93 /** Create a new empty temporary buffer. */
94 public TemporaryBuffer() {
95 inCoreLimit
= DEFAULT_IN_CORE_LIMIT
;
96 blocks
= new ArrayList
<Block
>(inCoreLimit
/ Block
.SZ
);
97 blocks
.add(new Block());
101 public void write(final int b
) throws IOException
{
102 if (blocks
== null) {
109 if (reachedInCoreLimit()) {
117 s
.buffer
[s
.count
++] = (byte) b
;
121 public void write(final byte[] b
, int off
, int len
) throws IOException
{
122 if (blocks
!= null) {
126 if (reachedInCoreLimit())
133 final int n
= Math
.min(Block
.SZ
- s
.count
, len
);
134 System
.arraycopy(b
, off
, s
.buffer
, s
.count
, n
);
142 diskOut
.write(b
, off
, len
);
146 * Copy all bytes remaining on the input stream into this buffer.
149 * the stream to read from, until EOF is reached.
150 * @throws IOException
151 * an error occurred reading from the input stream, or while
152 * writing to a local temporary file.
154 public void copy(final InputStream in
) throws IOException
{
155 if (blocks
!= null) {
159 if (reachedInCoreLimit())
165 final int n
= in
.read(s
.buffer
, s
.count
, Block
.SZ
- s
.count
);
172 final byte[] tmp
= new byte[Block
.SZ
];
174 while ((n
= in
.read(tmp
)) > 0)
175 diskOut
.write(tmp
, 0, n
);
178 private Block
last() {
179 return blocks
.get(blocks
.size() - 1);
182 private boolean reachedInCoreLimit() throws IOException
{
183 if (blocks
.size() * Block
.SZ
< inCoreLimit
)
186 onDiskFile
= File
.createTempFile("jgit_", ".buffer");
187 diskOut
= new FileOutputStream(onDiskFile
);
189 final Block last
= blocks
.remove(blocks
.size() - 1);
190 for (final Block b
: blocks
)
191 diskOut
.write(b
.buffer
, 0, b
.count
);
194 diskOut
= new BufferedOutputStream(diskOut
, Block
.SZ
);
195 diskOut
.write(last
.buffer
, 0, last
.count
);
199 public void close() throws IOException
{
200 if (diskOut
!= null) {
210 * Obtain the length (in bytes) of the buffer.
212 * The length is only accurate after {@link #close()} has been invoked.
214 * @return total length of the buffer, in bytes.
216 public long length() {
217 if (onDiskFile
!= null)
218 return onDiskFile
.length();
220 final Block last
= last();
221 return ((long) blocks
.size()) * Block
.SZ
- (Block
.SZ
- last
.count
);
225 * Convert this buffer's contents into a contiguous byte array.
227 * The buffer is only complete after {@link #close()} has been invoked.
229 * @return the complete byte array; length matches {@link #length()}.
230 * @throws IOException
231 * an error occurred reading from a local temporary file
232 * @throws OutOfMemoryError
233 * the buffer cannot fit in memory
235 public byte[] toByteArray() throws IOException
{
236 final long len
= length();
237 if (Integer
.MAX_VALUE
< len
)
238 throw new OutOfMemoryError("Length exceeds maximum array size");
240 final byte[] out
= new byte[(int) len
];
241 if (blocks
!= null) {
243 for (final Block b
: blocks
) {
244 System
.arraycopy(b
.buffer
, 0, out
, outPtr
, b
.count
);
248 final FileInputStream in
= new FileInputStream(onDiskFile
);
250 IO
.readFully(in
, out
, 0, (int) len
);
259 * Send this buffer to an output stream.
261 * This method may only be invoked after {@link #close()} has completed
262 * normally, to ensure all data is completely transferred.
265 * stream to send this buffer's complete content to.
267 * if not null progress updates are sent here. Caller should
268 * initialize the task and the number of work units to
269 * <code>{@link #length()}/1024</code>.
270 * @throws IOException
271 * an error occurred reading from a temporary file on the local
272 * system, or writing to the output stream.
274 public void writeTo(final OutputStream os
, ProgressMonitor pm
)
277 pm
= NullProgressMonitor
.INSTANCE
;
278 if (blocks
!= null) {
279 // Everything is in core so we can stream directly to the output.
281 for (final Block b
: blocks
) {
282 os
.write(b
.buffer
, 0, b
.count
);
283 pm
.update(b
.count
/ 1024);
286 // Reopen the temporary file and copy the contents.
288 final FileInputStream in
= new FileInputStream(onDiskFile
);
291 final byte[] buf
= new byte[Block
.SZ
];
292 while ((cnt
= in
.read(buf
)) >= 0) {
293 os
.write(buf
, 0, cnt
);
294 pm
.update(cnt
/ 1024);
302 /** Clear this buffer so it has no data, and cannot be used again. */
303 public void destroy() {
306 if (diskOut
!= null) {
309 } catch (IOException err
) {
310 // We shouldn't encounter an error closing the file.
316 if (onDiskFile
!= null) {
317 if (!onDiskFile
.delete())
318 onDiskFile
.deleteOnExit();
324 static final int SZ
= 8 * 1024;
326 final byte[] buffer
= new byte[SZ
];