git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 2/5 v2] Add copy(InputStream) to TemporaryBuffer

From
Shawn O. Pearce <spearce@spearce.org>
Date
Dec 11, 2008, 16:53 UTC
Message-ID
<20081211165345.GG32487@spearce.org>
In-Reply-To
<20081211155218.GF32487@spearce.org>

In some places we may find ourselves with an InputStream we need to copy into a TemporaryBuffer, so we can flatten out the entire stream to a single byte[]. Putting the copy loop here is more useful then duplicating it in application level code.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
  "Shawn O. Pearce" <spearce@spearce.org> wrote:
  > Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
  > > torsdag 11 december 2008 05:58:39 skrev Shawn O. Pearce:
  > > > +		final byte[] b = new byte[2048];
  > >
  > > Why not 8192 here too?
  > 
  > Blargh, you're right.  Actually what I should do is look to see
  > if blocks != null, in which case I should alloc a block and read
  > directly into it.  That would avoid one copy of the data.
  And now we do that...
  
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java b/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
index b1ffd6e..761f359 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
@@ -42,6 +42,7 @@
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 
@@ -135,6 +136,39 @@ public void write(final byte[] b, int off, int len) throws IOException {
 			diskOut.write(b, off, len);
 	}
 
+	/**
+	 * Copy all bytes remaining on the input stream into this buffer.
+	 * 
+	 * @param in
+	 *            the stream to read from, until EOF is reached.
+	 * @throws IOException
+	 *             an error occurred reading from the input stream, or while
+	 *             writing to a local temporary file.
+	 */
+	public void copy(final InputStream in) throws IOException {
+		if (blocks != null) {
+			for (;;) {
+				Block s = last();
+				if (s.isFull()) {
+					if (reachedInCoreLimit())
+						break;
+					s = new Block();
+					blocks.add(s);
+				}
+
+				final int n = in.read(s.buffer, s.count, Block.SZ - s.count);
+				if (n < 1)
+					return;
+				s.count += n;
+			}
+		}
+
+		final byte[] tmp = new byte[Block.SZ];
+		int n;
+		while ((n = in.read(tmp)) > 0)
+			diskOut.write(tmp, 0, n);
+	}
+
 	private Block last() {
 		return blocks.get(blocks.size() - 1);
 	}
-- 
1.6.1.rc2.306.ge5d5e


-- 
Shawn.
Previous: Shawn O. Pearce
Message 15 of 15 in “Patch parsing API”
  1. 0/5 Patch parsing APIShawn O. Pearce, Dec 11, 2008
  2. 1/5 Add toByteArray() to TemporaryBufferShawn O. Pearce, Dec 11, 2008
  3. 2/5 Add copy(InputStream) to TemporaryBufferShawn O. Pearce, Dec 11, 2008
  4. 3/5 Define FileHeader to parse the header block of a git diffShawn O. Pearce, Dec 11, 2008
  5. 4/5 Define Patch to parse a sequence of patch FileHeadersShawn O. Pearce, Dec 11, 2008
  6. 5/5 Add HunkHeader to represent a single hunk of a file within a patchShawn O. Pearce, Dec 11, 2008
  7. Robin RosenbergDec 11, 2008
  8. Shawn O. PearceDec 11, 2008
  9. Robin RosenbergDec 11, 2008
  10. Shawn O. PearceDec 11, 2008
  11. Robin RosenbergDec 11, 2008
  12. Shawn O. PearceDec 11, 2008
  13. Robin RosenbergDec 11, 2008
  14. Shawn O. PearceDec 11, 2008
  15. 2/5 Add copy(InputStream) to TemporaryBufferShawn O. Pearce, Dec 11, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.