Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / PackIndex.java
blob5834ca62f45a5c5a7de81eb6a6169b7c1bbb02d9
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.lib;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.IOException;
45 import org.spearce.jgit.util.NB;
47 /**
48 * Access path to locate objects by {@link ObjectId} in a {@link PackFile}.
49 * <p>
50 * Indexes are strictly redundant information in that we can rebuild all of the
51 * data held in the index file from the on disk representation of the pack file
52 * itself, but it is faster to access for random requests because data is stored
53 * by ObjectId.
54 * </p>
56 public abstract class PackIndex {
57 /**
58 * Open an existing pack <code>.idx</code> file for reading.
59 * <p>
60 * The format of the file will be automatically detected and a proper access
61 * implementation for that format will be constructed and returned to the
62 * caller. The file may or may not be held open by the returned instance.
63 * </p>
65 * @param idxFile
66 * existing pack .idx to read.
67 * @return access implementation for the requested file.
68 * @throws FileNotFoundException
69 * the file does not exist.
70 * @throws IOException
71 * the file exists but could not be read due to security errors,
72 * unrecognized data version, or unexpected data corruption.
74 public static PackIndex open(final File idxFile) throws IOException {
75 final FileInputStream fd = new FileInputStream(idxFile);
76 try {
77 final byte[] hdr = new byte[8];
78 NB.readFully(fd, hdr, 0, hdr.length);
79 if (isTOC(hdr)) {
80 final int v = NB.decodeInt32(hdr, 4);
81 switch (v) {
82 case 2:
83 return new PackIndexV2(fd);
84 default:
85 throw new IOException("Unsupported pack index version " + v);
88 return new PackIndexV1(fd, hdr);
89 } catch (IOException ioe) {
90 final String path = idxFile.getAbsolutePath();
91 final IOException err;
92 err = new IOException("Unreadable pack index: " + path);
93 err.initCause(ioe);
94 throw err;
95 } finally {
96 try {
97 fd.close();
98 } catch (IOException err2) {
99 // ignore
104 private static boolean isTOC(final byte[] h) {
105 return h[0] == -1 && h[1] == 't' && h[2] == 'O' && h[3] == 'c';
109 * Determine if an object is contained within the pack file.
111 * @param id
112 * the object to look for. Must not be null.
113 * @return true if the object is listed in this index; false otherwise.
115 public boolean hasObject(final AnyObjectId id) {
116 return findOffset(id) != -1;
120 * Obtain the total number of objects described by this index.
122 * @return number of objects in this index, and likewise in the associated
123 * pack that this index was generated from.
125 abstract long getObjectCount();
128 * Locate the file offset position for the requested object.
130 * @param objId
131 * name of the object to locate within the pack.
132 * @return offset of the object's header and compressed content; -1 if the
133 * object does not exist in this index and is thus not stored in the
134 * associated pack.
136 abstract long findOffset(AnyObjectId objId);