Move pure IO utility functions to a utility class of its own.
[jgit.git] / org.eclipse.jgit / src / org / eclipse / jgit / util / TemporaryBuffer.java
blobbcd858e74ae325e4e7a754b401555166832c42df
1 /*
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
14 * conditions are met:
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
27 * written permission.
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;
47 import java.io.File;
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;
58 /**
59 * A fully buffered output stream using local disk storage for large data.
60 * <p>
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
63 * too large.
64 * <p>
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;
74 /**
75 * Maximum number of bytes we will permit storing in memory.
76 * <p>
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;
82 /**
83 * Location of our temporary file if we are on disk; otherwise null.
84 * <p>
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());
100 @Override
101 public void write(final int b) throws IOException {
102 if (blocks == null) {
103 diskOut.write(b);
104 return;
107 Block s = last();
108 if (s.isFull()) {
109 if (reachedInCoreLimit()) {
110 diskOut.write(b);
111 return;
114 s = new Block();
115 blocks.add(s);
117 s.buffer[s.count++] = (byte) b;
120 @Override
121 public void write(final byte[] b, int off, int len) throws IOException {
122 if (blocks != null) {
123 while (len > 0) {
124 Block s = last();
125 if (s.isFull()) {
126 if (reachedInCoreLimit())
127 break;
129 s = new Block();
130 blocks.add(s);
133 final int n = Math.min(Block.SZ - s.count, len);
134 System.arraycopy(b, off, s.buffer, s.count, n);
135 s.count += n;
136 len -= n;
137 off += n;
141 if (len > 0)
142 diskOut.write(b, off, len);
146 * Copy all bytes remaining on the input stream into this buffer.
148 * @param in
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) {
156 for (;;) {
157 Block s = last();
158 if (s.isFull()) {
159 if (reachedInCoreLimit())
160 break;
161 s = new Block();
162 blocks.add(s);
165 final int n = in.read(s.buffer, s.count, Block.SZ - s.count);
166 if (n < 1)
167 return;
168 s.count += n;
172 final byte[] tmp = new byte[Block.SZ];
173 int n;
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)
184 return false;
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);
192 blocks = null;
194 diskOut = new BufferedOutputStream(diskOut, Block.SZ);
195 diskOut.write(last.buffer, 0, last.count);
196 return true;
199 public void close() throws IOException {
200 if (diskOut != null) {
201 try {
202 diskOut.close();
203 } finally {
204 diskOut = null;
210 * Obtain the length (in bytes) of the buffer.
211 * <p>
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.
226 * <p>
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) {
242 int outPtr = 0;
243 for (final Block b : blocks) {
244 System.arraycopy(b.buffer, 0, out, outPtr, b.count);
245 outPtr += b.count;
247 } else {
248 final FileInputStream in = new FileInputStream(onDiskFile);
249 try {
250 IO.readFully(in, out, 0, (int) len);
251 } finally {
252 in.close();
255 return out;
259 * Send this buffer to an output stream.
260 * <p>
261 * This method may only be invoked after {@link #close()} has completed
262 * normally, to ensure all data is completely transferred.
264 * @param os
265 * stream to send this buffer's complete content to.
266 * @param pm
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)
275 throws IOException {
276 if (pm == null)
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);
285 } else {
286 // Reopen the temporary file and copy the contents.
288 final FileInputStream in = new FileInputStream(onDiskFile);
289 try {
290 int cnt;
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);
296 } finally {
297 in.close();
302 /** Clear this buffer so it has no data, and cannot be used again. */
303 public void destroy() {
304 blocks = null;
306 if (diskOut != null) {
307 try {
308 diskOut.close();
309 } catch (IOException err) {
310 // We shouldn't encounter an error closing the file.
311 } finally {
312 diskOut = null;
316 if (onDiskFile != null) {
317 if (!onDiskFile.delete())
318 onDiskFile.deleteOnExit();
319 onDiskFile = null;
323 static class Block {
324 static final int SZ = 8 * 1024;
326 final byte[] buffer = new byte[SZ];
328 int count;
330 boolean isFull() {
331 return count == SZ;