From 38c5b26245c3bc36c46aa8a3423d885e8a99aaa8 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 29 Jun 2011 09:21:00 -0700 Subject: [PATCH] PackIndex: Support reading from any InputStream Change-Id: If065a9e33a8f3a03e9758eb7612af2fc460c87e5 Signed-off-by: Shawn O. Pearce --- .../eclipse/jgit/storage/file/PackIndex.java | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/PackIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/PackIndex.java index fc1b748f5e..a5a4e75a34 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/PackIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/PackIndex.java @@ -48,11 +48,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.text.MessageFormat; import java.util.Iterator; import java.util.Set; import org.eclipse.jgit.JGitText; +import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.AbbreviatedObjectId; import org.eclipse.jgit.lib.AnyObjectId; @@ -91,18 +93,7 @@ public abstract class PackIndex implements Iterable { public static PackIndex open(final File idxFile) throws IOException { final FileInputStream fd = new FileInputStream(idxFile); try { - final byte[] hdr = new byte[8]; - IO.readFully(fd, hdr, 0, hdr.length); - if (isTOC(hdr)) { - final int v = NB.decodeInt32(hdr, 4); - switch (v) { - case 2: - return new PackIndexV2(fd); - default: - throw new IOException(MessageFormat.format(JGitText.get().unsupportedPackIndexVersion, v)); - } - } - return new PackIndexV1(fd, hdr); + return read(fd); } catch (IOException ioe) { final String path = idxFile.getAbsolutePath(); final IOException err; @@ -118,6 +109,39 @@ public abstract class PackIndex implements Iterable { } } + /** + * Read an existing pack index file from a buffered stream. + *

+ * The format of the file will be automatically detected and a proper access + * implementation for that format will be constructed and returned to the + * caller. The file may or may not be held open by the returned instance. + * + * @param fd + * stream to read the index file from. The stream must be + * buffered as some small IOs are performed against the stream. + * The caller is responsible for closing the stream. + * @return a copy of the index in-memory. + * @throws IOException + * the stream cannot be read. + * @throws CorruptObjectException + * the stream does not contain a valid pack index. + */ + public static PackIndex read(InputStream fd) throws IOException, + CorruptObjectException { + final byte[] hdr = new byte[8]; + IO.readFully(fd, hdr, 0, hdr.length); + if (isTOC(hdr)) { + final int v = NB.decodeInt32(hdr, 4); + switch (v) { + case 2: + return new PackIndexV2(fd); + default: + throw new IOException(MessageFormat.format(JGitText.get().unsupportedPackIndexVersion, v)); + } + } + return new PackIndexV1(fd, hdr); + } + private static boolean isTOC(final byte[] h) { final byte[] toc = PackIndexWriter.TOC; for (int i = 0; i < toc.length; i++) -- 2.39.5