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
;
43 import java
.util
.Iterator
;
44 import java
.util
.NoSuchElementException
;
46 import org
.spearce
.jgit
.errors
.CorruptObjectException
;
47 import org
.spearce
.jgit
.util
.NB
;
49 class PackIndexV1
extends PackIndex
{
50 private static final int IDX_HDR_LEN
= 256 * 4;
52 private byte[][] idxdata
;
54 private long objectCnt
;
56 PackIndexV1(final InputStream fd
, final byte[] hdr
)
57 throws CorruptObjectException
, IOException
{
58 final byte[] fanoutTable
= new byte[IDX_HDR_LEN
];
59 System
.arraycopy(hdr
, 0, fanoutTable
, 0, hdr
.length
);
60 NB
.readFully(fd
, fanoutTable
, hdr
.length
, IDX_HDR_LEN
- hdr
.length
);
62 final long[] idxHeader
= new long[256]; // really unsigned 32-bit...
63 for (int k
= 0; k
< idxHeader
.length
; k
++)
64 idxHeader
[k
] = NB
.decodeUInt32(fanoutTable
, k
* 4);
65 idxdata
= new byte[idxHeader
.length
][];
66 for (int k
= 0; k
< idxHeader
.length
; k
++) {
69 n
= (int) (idxHeader
[k
]);
71 n
= (int) (idxHeader
[k
] - idxHeader
[k
- 1]);
74 idxdata
[k
] = new byte[n
* (Constants
.OBJECT_ID_LENGTH
+ 4)];
75 NB
.readFully(fd
, idxdata
[k
], 0, idxdata
[k
].length
);
78 objectCnt
= idxHeader
[255];
81 long getObjectCount() {
85 long findOffset(final AnyObjectId objId
) {
86 final int levelOne
= objId
.getFirstByte();
87 byte[] data
= idxdata
[levelOne
];
90 int high
= data
.length
/ (4 + Constants
.OBJECT_ID_LENGTH
);
93 final int mid
= (low
+ high
) / 2;
94 final int pos
= ((4 + Constants
.OBJECT_ID_LENGTH
) * mid
) + 4;
95 final int cmp
= objId
.compareTo(data
, pos
);
99 int b0
= data
[pos
- 4] & 0xff;
100 int b1
= data
[pos
- 3] & 0xff;
101 int b2
= data
[pos
- 2] & 0xff;
102 int b3
= data
[pos
- 1] & 0xff;
103 return (((long) b0
) << 24) | (b1
<< 16) | (b2
<< 8) | (b3
);
106 } while (low
< high
);
110 public Iterator
<MutableEntry
> iterator() {
111 return new IndexV1Iterator();
114 private class IndexV1Iterator
extends EntriesIterator
{
115 private int levelOne
;
117 private int levelTwo
;
119 public MutableEntry
next() {
120 for (; levelOne
< idxdata
.length
; levelOne
++) {
121 if (idxdata
[levelOne
] == null)
124 if (levelTwo
< idxdata
[levelOne
].length
) {
125 long offset
= NB
.decodeUInt32(idxdata
[levelOne
], levelTwo
);
126 objectId
.setOffset(offset
);
127 objectId
.fromRaw(idxdata
[levelOne
], levelTwo
+ 4);
128 levelTwo
+= Constants
.OBJECT_ID_LENGTH
+ 4;
135 throw new NoSuchElementException();