2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org
.spearce
.jgit
.lib
;
41 import java
.io
.IOException
;
42 import java
.io
.InputStream
;
44 import org
.spearce
.jgit
.errors
.CorruptObjectException
;
45 import org
.spearce
.jgit
.util
.NB
;
47 class PackIndexV1
extends PackIndex
{
48 private static final int IDX_HDR_LEN
= 256 * 4;
50 private byte[][] idxdata
;
52 private long objectCnt
;
54 PackIndexV1(final InputStream fd
, final byte[] hdr
)
55 throws CorruptObjectException
, IOException
{
56 final byte[] fanoutTable
= new byte[IDX_HDR_LEN
];
57 System
.arraycopy(hdr
, 0, fanoutTable
, 0, hdr
.length
);
58 NB
.readFully(fd
, fanoutTable
, hdr
.length
, IDX_HDR_LEN
- hdr
.length
);
60 final long[] idxHeader
= new long[256]; // really unsigned 32-bit...
61 for (int k
= 0; k
< idxHeader
.length
; k
++)
62 idxHeader
[k
] = NB
.decodeUInt32(fanoutTable
, k
* 4);
63 idxdata
= new byte[idxHeader
.length
][];
64 for (int k
= 0; k
< idxHeader
.length
; k
++) {
67 n
= (int)(idxHeader
[k
]);
69 n
= (int)(idxHeader
[k
]-idxHeader
[k
-1]);
72 idxdata
[k
] = new byte[n
* (Constants
.OBJECT_ID_LENGTH
+ 4)];
73 NB
.readFully(fd
, idxdata
[k
], 0, idxdata
[k
].length
);
76 objectCnt
= idxHeader
[255];
79 long getObjectCount() {
83 long findOffset(final AnyObjectId objId
) {
84 final int levelOne
= objId
.getFirstByte();
85 byte[] data
= idxdata
[levelOne
];
88 int high
= data
.length
/ (4 + Constants
.OBJECT_ID_LENGTH
);
91 final int mid
= (low
+ high
) / 2;
92 final int pos
= ((4 + Constants
.OBJECT_ID_LENGTH
) * mid
) + 4;
93 final int cmp
= objId
.compareTo(data
, pos
);
97 int b0
= data
[pos
-4] & 0xff;
98 int b1
= data
[pos
-3] & 0xff;
99 int b2
= data
[pos
-2] & 0xff;
100 int b3
= data
[pos
-1] & 0xff;
101 return (((long)b0
) << 24) | ( b1
<< 16 ) | ( b2
<< 8 ) | (b3
);
104 } while (low
< high
);