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

[JGIT PATCH 1/5] Add toByteArray() to TemporaryBuffer

From
Shawn O. Pearce <spearce@spearce.org>
Date
Dec 11, 2008, 04:58 UTC
Message-ID
<1228971522-28764-2-git-send-email-spearce@spearce.org>
In-Reply-To
<1228971522-28764-1-git-send-email-spearce@spearce.org>

It can be more useful to convert a buffered output stream into a single byte array, without paying the penalties associated with ByteArrayOutputStream to do the same action.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/util/TemporaryBuffer.java |   34 ++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
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 d597c38..b1ffd6e 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/TemporaryBuffer.java
@@ -182,6 +182,40 @@ public long length() {
 	}
 
 	/**
+	 * Convert this buffer's contents into a contiguous byte array.
+	 * <p>
+	 * The buffer is only complete after {@link #close()} has been invoked.
+	 * 
+	 * @return the complete byte array; length matches {@link #length()}.
+	 * @throws IOException
+	 *             an error occurred reading from a local temporary file
+	 * @throws OutOfMemoryError
+	 *             the buffer cannot fit in memory
+	 */
+	public byte[] toByteArray() throws IOException {
+		final long len = length();
+		if (Integer.MAX_VALUE < len)
+			throw new OutOfMemoryError("Length exceeds maximum array size");
+
+		final byte[] out = new byte[(int) len];
+		if (blocks != null) {
+			int outPtr = 0;
+			for (final Block b : blocks) {
+				System.arraycopy(b.buffer, 0, out, outPtr, b.count);
+				outPtr += b.count;
+			}
+		} else {
+			final FileInputStream in = new FileInputStream(onDiskFile);
+			try {
+				NB.readFully(in, out, 0, (int) len);
+			} finally {
+				in.close();
+			}
+		}
+		return out;
+	}
+
+	/**
 	 * Send this buffer to an output stream.
 	 * <p>
 	 * This method may only be invoked after {@link #close()} has completed
-- 
1.6.1.rc2.299.gead4c
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 2 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.