View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.util;
46  
47  import java.io.BufferedOutputStream;
48  import java.io.File;
49  import java.io.FileInputStream;
50  import java.io.FileOutputStream;
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.io.OutputStream;
54  import java.util.ArrayList;
55  
56  import org.eclipse.jgit.internal.JGitText;
57  import org.eclipse.jgit.lib.NullProgressMonitor;
58  import org.eclipse.jgit.lib.ProgressMonitor;
59  
60  /**
61   * A fully buffered output stream.
62   * <p>
63   * Subclasses determine the behavior when the in-memory buffer capacity has been
64   * exceeded and additional bytes are still being received for output.
65   */
66  public abstract class TemporaryBuffer extends OutputStream {
67  	/** Default limit for in-core storage. */
68  	protected static final int DEFAULT_IN_CORE_LIMIT = 1024 * 1024;
69  
70  	/** Chain of data, if we are still completely in-core; otherwise null. */
71  	ArrayList<Block> blocks;
72  
73  	/**
74  	 * Maximum number of bytes we will permit storing in memory.
75  	 * <p>
76  	 * When this limit is reached the data will be shifted to a file on disk,
77  	 * preventing the JVM heap from growing out of control.
78  	 */
79  	private int inCoreLimit;
80  
81  	/** Initial size of block list. */
82  	private int initialBlocks;
83  
84  	/** If {@link #inCoreLimit} has been reached, remainder goes here. */
85  	private OutputStream overflow;
86  
87  	/**
88  	 * Create a new empty temporary buffer.
89  	 *
90  	 * @param limit
91  	 *            maximum number of bytes to store in memory before entering the
92  	 *            overflow output path; also used as the estimated size.
93  	 */
94  	protected TemporaryBuffer(final int limit) {
95  		this(limit, limit);
96  	}
97  
98  	/**
99  	 * Create a new empty temporary buffer.
100 	 *
101 	 * @param estimatedSize
102 	 *            estimated size of storage used, to size the initial list of
103 	 *            block pointers.
104 	 * @param limit
105 	 *            maximum number of bytes to store in memory before entering the
106 	 *            overflow output path.
107 	 * @since 4.0
108 	 */
109 	protected TemporaryBuffer(final int estimatedSize, final int limit) {
110 		if (estimatedSize > limit)
111 			throw new IllegalArgumentException();
112 		this.inCoreLimit = limit;
113 		this.initialBlocks = (estimatedSize - 1) / Block.SZ + 1;
114 		reset();
115 	}
116 
117 	/** {@inheritDoc} */
118 	@Override
119 	public void write(final int b) throws IOException {
120 		if (overflow != null) {
121 			overflow.write(b);
122 			return;
123 		}
124 
125 		Block s = last();
126 		if (s.isFull()) {
127 			if (reachedInCoreLimit()) {
128 				overflow.write(b);
129 				return;
130 			}
131 
132 			s = new Block();
133 			blocks.add(s);
134 		}
135 		s.buffer[s.count++] = (byte) b;
136 	}
137 
138 	/** {@inheritDoc} */
139 	@Override
140 	public void write(final byte[] b, int off, int len) throws IOException {
141 		if (overflow == null) {
142 			while (len > 0) {
143 				Block s = last();
144 				if (s.isFull()) {
145 					if (reachedInCoreLimit())
146 						break;
147 
148 					s = new Block();
149 					blocks.add(s);
150 				}
151 
152 				final int n = Math.min(s.buffer.length - s.count, len);
153 				System.arraycopy(b, off, s.buffer, s.count, n);
154 				s.count += n;
155 				len -= n;
156 				off += n;
157 			}
158 		}
159 
160 		if (len > 0)
161 			overflow.write(b, off, len);
162 	}
163 
164 	/**
165 	 * Dumps the entire buffer into the overflow stream, and flushes it.
166 	 *
167 	 * @throws java.io.IOException
168 	 *             the overflow stream cannot be started, or the buffer contents
169 	 *             cannot be written to it, or it failed to flush.
170 	 */
171 	protected void doFlush() throws IOException {
172 		if (overflow == null)
173 			switchToOverflow();
174 		overflow.flush();
175 	}
176 
177 	/**
178 	 * Copy all bytes remaining on the input stream into this buffer.
179 	 *
180 	 * @param in
181 	 *            the stream to read from, until EOF is reached.
182 	 * @throws java.io.IOException
183 	 *             an error occurred reading from the input stream, or while
184 	 *             writing to a local temporary file.
185 	 */
186 	public void copy(final InputStream in) throws IOException {
187 		if (blocks != null) {
188 			for (;;) {
189 				Block s = last();
190 				if (s.isFull()) {
191 					if (reachedInCoreLimit())
192 						break;
193 					s = new Block();
194 					blocks.add(s);
195 				}
196 
197 				int n = in.read(s.buffer, s.count, s.buffer.length - s.count);
198 				if (n < 1)
199 					return;
200 				s.count += n;
201 			}
202 		}
203 
204 		final byte[] tmp = new byte[Block.SZ];
205 		int n;
206 		while ((n = in.read(tmp)) > 0)
207 			overflow.write(tmp, 0, n);
208 	}
209 
210 	/**
211 	 * Obtain the length (in bytes) of the buffer.
212 	 * <p>
213 	 * The length is only accurate after {@link #close()} has been invoked.
214 	 *
215 	 * @return total length of the buffer, in bytes.
216 	 */
217 	public long length() {
218 		return inCoreLength();
219 	}
220 
221 	private long inCoreLength() {
222 		final Block last = last();
223 		return ((long) blocks.size() - 1) * Block.SZ + last.count;
224 	}
225 
226 	/**
227 	 * Convert this buffer's contents into a contiguous byte array.
228 	 * <p>
229 	 * The buffer is only complete after {@link #close()} has been invoked.
230 	 *
231 	 * @return the complete byte array; length matches {@link #length()}.
232 	 * @throws java.io.IOException
233 	 *             an error occurred reading from a local temporary file
234 	 */
235 	public byte[] toByteArray() throws IOException {
236 		final long len = length();
237 		if (Integer.MAX_VALUE < len)
238 			throw new OutOfMemoryError(JGitText.get().lengthExceedsMaximumArraySize);
239 		final byte[] out = new byte[(int) len];
240 		int outPtr = 0;
241 		for (final Block b : blocks) {
242 			System.arraycopy(b.buffer, 0, out, outPtr, b.count);
243 			outPtr += b.count;
244 		}
245 		return out;
246 	}
247 
248 	/**
249 	 * Convert this buffer's contents into a contiguous byte array. If this size
250 	 * of the buffer exceeds the limit only return the first {@code limit} bytes
251 	 * <p>
252 	 * The buffer is only complete after {@link #close()} has been invoked.
253 	 *
254 	 * @param limit
255 	 *            the maximum number of bytes to be returned
256 	 * @return the byte array limited to {@code limit} bytes.
257 	 * @throws java.io.IOException
258 	 *             an error occurred reading from a local temporary file
259 	 * @since 4.2
260 	 */
261 	public byte[] toByteArray(int limit) throws IOException {
262 		final long len = Math.min(length(), limit);
263 		if (Integer.MAX_VALUE < len)
264 			throw new OutOfMemoryError(
265 					JGitText.get().lengthExceedsMaximumArraySize);
266 		final byte[] out = new byte[(int) len];
267 		int outPtr = 0;
268 		for (final Block b : blocks) {
269 			System.arraycopy(b.buffer, 0, out, outPtr, b.count);
270 			outPtr += b.count;
271 		}
272 		return out;
273 	}
274 
275 	/**
276 	 * Send this buffer to an output stream.
277 	 * <p>
278 	 * This method may only be invoked after {@link #close()} has completed
279 	 * normally, to ensure all data is completely transferred.
280 	 *
281 	 * @param os
282 	 *            stream to send this buffer's complete content to.
283 	 * @param pm
284 	 *            if not null progress updates are sent here. Caller should
285 	 *            initialize the task and the number of work units to <code>
286 	 *            {@link #length()}/1024</code>.
287 	 * @throws java.io.IOException
288 	 *             an error occurred reading from a temporary file on the local
289 	 *             system, or writing to the output stream.
290 	 */
291 	public void writeTo(final OutputStream os, ProgressMonitor pm)
292 			throws IOException {
293 		if (pm == null)
294 			pm = NullProgressMonitor.INSTANCE;
295 		for (final Block b : blocks) {
296 			os.write(b.buffer, 0, b.count);
297 			pm.update(b.count / 1024);
298 		}
299 	}
300 
301 	/**
302 	 * Open an input stream to read from the buffered data.
303 	 * <p>
304 	 * This method may only be invoked after {@link #close()} has completed
305 	 * normally, to ensure all data is completely transferred.
306 	 *
307 	 * @return a stream to read from the buffer. The caller must close the
308 	 *         stream when it is no longer useful.
309 	 * @throws java.io.IOException
310 	 *             an error occurred opening the temporary file.
311 	 */
312 	public InputStream openInputStream() throws IOException {
313 		return new BlockInputStream();
314 	}
315 
316 	/**
317 	 * Reset this buffer for reuse, purging all buffered content.
318 	 */
319 	public void reset() {
320 		if (overflow != null) {
321 			destroy();
322 		}
323 		if (blocks != null)
324 			blocks.clear();
325 		else
326 			blocks = new ArrayList<>(initialBlocks);
327 		blocks.add(new Block(Math.min(inCoreLimit, Block.SZ)));
328 	}
329 
330 	/**
331 	 * Open the overflow output stream, so the remaining output can be stored.
332 	 *
333 	 * @return the output stream to receive the buffered content, followed by
334 	 *         the remaining output.
335 	 * @throws java.io.IOException
336 	 *             the buffer cannot create the overflow stream.
337 	 */
338 	protected abstract OutputStream overflow() throws IOException;
339 
340 	private Block last() {
341 		return blocks.get(blocks.size() - 1);
342 	}
343 
344 	private boolean reachedInCoreLimit() throws IOException {
345 		if (inCoreLength() < inCoreLimit)
346 			return false;
347 
348 		switchToOverflow();
349 		return true;
350 	}
351 
352 	private void switchToOverflow() throws IOException {
353 		overflow = overflow();
354 
355 		final Block last = blocks.remove(blocks.size() - 1);
356 		for (final Block b : blocks)
357 			overflow.write(b.buffer, 0, b.count);
358 		blocks = null;
359 
360 		overflow = new BufferedOutputStream(overflow, Block.SZ);
361 		overflow.write(last.buffer, 0, last.count);
362 	}
363 
364 	/** {@inheritDoc} */
365 	@Override
366 	public void close() throws IOException {
367 		if (overflow != null) {
368 			try {
369 				overflow.close();
370 			} finally {
371 				overflow = null;
372 			}
373 		}
374 	}
375 
376 	/**
377 	 * Clear this buffer so it has no data, and cannot be used again.
378 	 */
379 	public void destroy() {
380 		blocks = null;
381 
382 		if (overflow != null) {
383 			try {
384 				overflow.close();
385 			} catch (IOException err) {
386 				// We shouldn't encounter an error closing the file.
387 			} finally {
388 				overflow = null;
389 			}
390 		}
391 	}
392 
393 	/**
394 	 * A fully buffered output stream using local disk storage for large data.
395 	 * <p>
396 	 * Initially this output stream buffers to memory and is therefore similar
397 	 * to ByteArrayOutputStream, but it shifts to using an on disk temporary
398 	 * file if the output gets too large.
399 	 * <p>
400 	 * The content of this buffered stream may be sent to another OutputStream
401 	 * only after this stream has been properly closed by {@link #close()}.
402 	 */
403 	public static class LocalFile extends TemporaryBuffer {
404 		/** Directory to store the temporary file under. */
405 		private final File directory;
406 
407 		/**
408 		 * Location of our temporary file if we are on disk; otherwise null.
409 		 * <p>
410 		 * If we exceeded the {@link #inCoreLimit} we nulled out {@link #blocks}
411 		 * and created this file instead. All output goes here through
412 		 * {@link #overflow}.
413 		 */
414 		private File onDiskFile;
415 
416 		/**
417 		 * Create a new temporary buffer, limiting memory usage.
418 		 *
419 		 * @param directory
420 		 *            if the buffer has to spill over into a temporary file, the
421 		 *            directory where the file should be saved. If null the
422 		 *            system default temporary directory (for example /tmp) will
423 		 *            be used instead.
424 		 */
425 		public LocalFile(final File directory) {
426 			this(directory, DEFAULT_IN_CORE_LIMIT);
427 		}
428 
429 		/**
430 		 * Create a new temporary buffer, limiting memory usage.
431 		 *
432 		 * @param directory
433 		 *            if the buffer has to spill over into a temporary file, the
434 		 *            directory where the file should be saved. If null the
435 		 *            system default temporary directory (for example /tmp) will
436 		 *            be used instead.
437 		 * @param inCoreLimit
438 		 *            maximum number of bytes to store in memory. Storage beyond
439 		 *            this limit will use the local file.
440 		 */
441 		public LocalFile(final File directory, final int inCoreLimit) {
442 			super(inCoreLimit);
443 			this.directory = directory;
444 		}
445 
446 		@Override
447 		protected OutputStream overflow() throws IOException {
448 			onDiskFile = File.createTempFile("jgit_", ".buf", directory); //$NON-NLS-1$ //$NON-NLS-2$
449 			return new BufferedOutputStream(new FileOutputStream(onDiskFile));
450 		}
451 
452 		@Override
453 		public long length() {
454 			if (onDiskFile == null) {
455 				return super.length();
456 			}
457 			return onDiskFile.length();
458 		}
459 
460 		@Override
461 		public byte[] toByteArray() throws IOException {
462 			if (onDiskFile == null) {
463 				return super.toByteArray();
464 			}
465 
466 			final long len = length();
467 			if (Integer.MAX_VALUE < len)
468 				throw new OutOfMemoryError(JGitText.get().lengthExceedsMaximumArraySize);
469 			final byte[] out = new byte[(int) len];
470 			final FileInputStream in = new FileInputStream(onDiskFile);
471 			try {
472 				IO.readFully(in, out, 0, (int) len);
473 			} finally {
474 				in.close();
475 			}
476 			return out;
477 		}
478 
479 		@Override
480 		public void writeTo(final OutputStream os, ProgressMonitor pm)
481 				throws IOException {
482 			if (onDiskFile == null) {
483 				super.writeTo(os, pm);
484 				return;
485 			}
486 			if (pm == null)
487 				pm = NullProgressMonitor.INSTANCE;
488 			final FileInputStream in = new FileInputStream(onDiskFile);
489 			try {
490 				int cnt;
491 				final byte[] buf = new byte[Block.SZ];
492 				while ((cnt = in.read(buf)) >= 0) {
493 					os.write(buf, 0, cnt);
494 					pm.update(cnt / 1024);
495 				}
496 			} finally {
497 				in.close();
498 			}
499 		}
500 
501 		@Override
502 		public InputStream openInputStream() throws IOException {
503 			if (onDiskFile == null)
504 				return super.openInputStream();
505 			return new FileInputStream(onDiskFile);
506 		}
507 
508 		@Override
509 		public void destroy() {
510 			super.destroy();
511 
512 			if (onDiskFile != null) {
513 				try {
514 					if (!onDiskFile.delete())
515 						onDiskFile.deleteOnExit();
516 				} finally {
517 					onDiskFile = null;
518 				}
519 			}
520 		}
521 	}
522 
523 	/**
524 	 * A temporary buffer that will never exceed its in-memory limit.
525 	 * <p>
526 	 * If the in-memory limit is reached an IOException is thrown, rather than
527 	 * attempting to spool to local disk.
528 	 */
529 	public static class Heap extends TemporaryBuffer {
530 		/**
531 		 * Create a new heap buffer with a maximum storage limit.
532 		 *
533 		 * @param limit
534 		 *            maximum number of bytes that can be stored in this buffer;
535 		 *            also used as the estimated size. Storing beyond this many
536 		 *            will cause an IOException to be thrown during write.
537 		 */
538 		public Heap(final int limit) {
539 			super(limit);
540 		}
541 
542 		/**
543 		 * Create a new heap buffer with a maximum storage limit.
544 		 *
545 		 * @param estimatedSize
546 		 *            estimated size of storage used, to size the initial list of
547 		 *            block pointers.
548 		 * @param limit
549 		 *            maximum number of bytes that can be stored in this buffer.
550 		 *            Storing beyond this many will cause an IOException to be
551 		 *            thrown during write.
552 		 * @since 4.0
553 		 */
554 		public Heap(final int estimatedSize, final int limit) {
555 			super(estimatedSize, limit);
556 		}
557 
558 		@Override
559 		protected OutputStream overflow() throws IOException {
560 			throw new IOException(JGitText.get().inMemoryBufferLimitExceeded);
561 		}
562 	}
563 
564 	static class Block {
565 		static final int SZ = 8 * 1024;
566 
567 		final byte[] buffer;
568 
569 		int count;
570 
571 		Block() {
572 			buffer = new byte[SZ];
573 		}
574 
575 		Block(int sz) {
576 			buffer = new byte[sz];
577 		}
578 
579 		boolean isFull() {
580 			return count == buffer.length;
581 		}
582 	}
583 
584 	private class BlockInputStream extends InputStream {
585 		private byte[] singleByteBuffer;
586 		private int blockIndex;
587 		private Block block;
588 		private int blockPos;
589 
590 		BlockInputStream() {
591 			block = blocks.get(blockIndex);
592 		}
593 
594 		@Override
595 		public int read() throws IOException {
596 			if (singleByteBuffer == null)
597 				singleByteBuffer = new byte[1];
598 			int n = read(singleByteBuffer);
599 			return n == 1 ? singleByteBuffer[0] & 0xff : -1;
600 		}
601 
602 		@Override
603 		public long skip(long cnt) throws IOException {
604 			long skipped = 0;
605 			while (0 < cnt) {
606 				int n = (int) Math.min(block.count - blockPos, cnt);
607 				if (0 < n) {
608 					blockPos += n;
609 					skipped += n;
610 					cnt -= n;
611 				} else if (nextBlock())
612 					continue;
613 				else
614 					break;
615 			}
616 			return skipped;
617 		}
618 
619 		@Override
620 		public int read(byte[] b, int off, int len) throws IOException {
621 			if (len == 0)
622 				return 0;
623 			int copied = 0;
624 			while (0 < len) {
625 				int c = Math.min(block.count - blockPos, len);
626 				if (0 < c) {
627 					System.arraycopy(block.buffer, blockPos, b, off, c);
628 					blockPos += c;
629 					off += c;
630 					len -= c;
631 					copied += c;
632 				} else if (nextBlock())
633 					continue;
634 				else
635 					break;
636 			}
637 			return 0 < copied ? copied : -1;
638 		}
639 
640 		private boolean nextBlock() {
641 			if (++blockIndex < blocks.size()) {
642 				block = blocks.get(blockIndex);
643 				blockPos = 0;
644 				return true;
645 			}
646 			return false;
647 		}
648 	}
649 }