[JGIT PATCH 2/5] Add copy(InputStream) to TemporaryBuffer
- From
Shawn O. Pearce <spearce@spearce.org>
- Date
- Dec 11, 2008, 04:58 UTC
- Message-ID
- <1228971522-28764-3-git-send-email-spearce@spearce.org>
- In-Reply-To
- <1228971522-28764-2-git-send-email-spearce@spearce.org>
In some places we may find it 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> --- .../src/org/spearce/jgit/util/TemporaryBuffer.java | 17 +++++++++++++++++ 1 files changed, 17 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 b1ffd6e..8f91246 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,22 @@ 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 { + final byte[] b = new byte[2048]; + int n; + while ((n = in.read(b)) > 0) + write(b, 0, n); + } + private Block last() { return blocks.get(blocks.size() - 1); }
-- 1.6.1.rc2.299.gead4c