]> source.dussan.org Git - jgit.git/commitdiff
PackIndex: Support reading from any InputStream 26/3926/2
authorShawn O. Pearce <spearce@spearce.org>
Wed, 29 Jun 2011 16:21:00 +0000 (09:21 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Thu, 28 Jul 2011 17:07:09 +0000 (10:07 -0700)
Change-Id: If065a9e33a8f3a03e9758eb7612af2fc460c87e5
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit/src/org/eclipse/jgit/storage/file/PackIndex.java

index fc1b748f5e2fab31fb1532aadac89f127cb688f0..a5a4e75a34941f782e5eae30f53c856fd042e877 100644 (file)
@@ -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<PackIndex.MutableEntry> {
        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<PackIndex.MutableEntry> {
                }
        }
 
+       /**
+        * Read an existing pack index file from a buffered stream.
+        * <p>
+        * 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++)