2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
4 * and other copyright owners as documented in the project's IP log.
6 * This program and the accompanying materials are made available
7 * under the terms of the Eclipse Distribution License v1.0 which
8 * accompanies this distribution, is reproduced below, and is
9 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials provided
23 * with the distribution.
25 * - Neither the name of the Eclipse Foundation, Inc. nor the
26 * names of its contributors may be used to endorse or promote
27 * products derived from this software without specific prior
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 package org
.eclipse
.jgit
.lib
;
48 import java
.io
.FileNotFoundException
;
49 import java
.io
.IOException
;
50 import java
.util
.zip
.DataFormatException
;
51 import java
.util
.zip
.Inflater
;
53 import org
.eclipse
.jgit
.errors
.CorruptObjectException
;
54 import org
.eclipse
.jgit
.util
.IO
;
55 import org
.eclipse
.jgit
.util
.MutableInteger
;
56 import org
.eclipse
.jgit
.util
.RawParseUtils
;
59 * Loose object loader. This class loads an object not stored in a pack.
61 public class UnpackedObjectLoader
extends ObjectLoader
{
62 private final int objectType
;
64 private final int objectSize
;
66 private final byte[] bytes
;
69 * Construct an ObjectLoader to read from the file.
72 * location of the loose object to read.
74 * expected identity of the object being loaded, if known.
75 * @throws FileNotFoundException
76 * the loose object file does not exist.
78 * the loose object file exists, but is corrupt.
80 public UnpackedObjectLoader(final File path
, final AnyObjectId id
)
82 this(IO
.readFully(path
), id
);
86 * Construct an ObjectLoader from a loose object's compressed form.
89 * entire content of the loose object file.
90 * @throws CorruptObjectException
91 * The compressed data supplied does not match the format for a
94 public UnpackedObjectLoader(final byte[] compressed
)
95 throws CorruptObjectException
{
96 this(compressed
, null);
99 private UnpackedObjectLoader(final byte[] compressed
, final AnyObjectId id
)
100 throws CorruptObjectException
{
101 // Try to determine if this is a legacy format loose object or
102 // a new style loose object. The legacy format was completely
103 // compressed with zlib so the first byte must be 0x78 (15-bit
104 // window size, deflated) and the first 16 bit word must be
105 // evenly divisible by 31. Otherwise its a new style loose
108 final Inflater inflater
= InflaterCache
.get();
110 final int fb
= compressed
[0] & 0xff;
111 if (fb
== 0x78 && (((fb
<< 8) | compressed
[1] & 0xff) % 31) == 0) {
112 inflater
.setInput(compressed
);
113 final byte[] hdr
= new byte[64];
115 while (!inflater
.finished() && avail
< hdr
.length
)
117 avail
+= inflater
.inflate(hdr
, avail
, hdr
.length
119 } catch (DataFormatException dfe
) {
120 final CorruptObjectException coe
;
121 coe
= new CorruptObjectException(id
, "bad stream");
127 throw new CorruptObjectException(id
, "no header");
129 final MutableInteger p
= new MutableInteger();
130 objectType
= Constants
.decodeTypeString(id
, hdr
, (byte) ' ', p
);
131 objectSize
= RawParseUtils
.parseBase10(hdr
, p
.value
, p
);
133 throw new CorruptObjectException(id
, "negative size");
134 if (hdr
[p
.value
++] != 0)
135 throw new CorruptObjectException(id
, "garbage after size");
136 bytes
= new byte[objectSize
];
138 System
.arraycopy(hdr
, p
.value
, bytes
, 0, avail
- p
.value
);
139 decompress(id
, inflater
, avail
- p
.value
);
142 int c
= compressed
[p
++] & 0xff;
143 final int typeCode
= (c
>> 4) & 7;
146 while ((c
& 0x80) != 0) {
147 c
= compressed
[p
++] & 0xff;
148 size
+= (c
& 0x7f) << shift
;
153 case Constants
.OBJ_COMMIT
:
154 case Constants
.OBJ_TREE
:
155 case Constants
.OBJ_BLOB
:
156 case Constants
.OBJ_TAG
:
157 objectType
= typeCode
;
160 throw new CorruptObjectException(id
, "invalid type");
164 bytes
= new byte[objectSize
];
165 inflater
.setInput(compressed
, p
, compressed
.length
- p
);
166 decompress(id
, inflater
, 0);
169 InflaterCache
.release(inflater
);
173 private void decompress(final AnyObjectId id
, final Inflater inf
, int p
)
174 throws CorruptObjectException
{
176 while (!inf
.finished())
177 p
+= inf
.inflate(bytes
, p
, objectSize
- p
);
178 } catch (DataFormatException dfe
) {
179 final CorruptObjectException coe
;
180 coe
= new CorruptObjectException(id
, "bad stream");
185 throw new CorruptObjectException(id
, "incorrect length");
189 public int getType() {
194 public long getSize() {
199 public byte[] getCachedBytes() {
204 public int getRawType() {
209 public long getRawSize() {