Move pure IO utility functions to a utility class of its own.
[jgit.git] / org.eclipse.jgit / src / org / eclipse / jgit / lib / UnpackedObjectLoader.java
blob968dada0cdcde12e6984f9f4b6f813398440596d
1 /*
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
15 * conditions are met:
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
28 * written permission.
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;
47 import java.io.File;
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;
58 /**
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;
68 /**
69 * Construct an ObjectLoader to read from the file.
71 * @param path
72 * location of the loose object to read.
73 * @param id
74 * expected identity of the object being loaded, if known.
75 * @throws FileNotFoundException
76 * the loose object file does not exist.
77 * @throws IOException
78 * the loose object file exists, but is corrupt.
80 public UnpackedObjectLoader(final File path, final AnyObjectId id)
81 throws IOException {
82 this(IO.readFully(path), id);
85 /**
86 * Construct an ObjectLoader from a loose object's compressed form.
88 * @param compressed
89 * entire content of the loose object file.
90 * @throws CorruptObjectException
91 * The compressed data supplied does not match the format for a
92 * valid loose object.
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
106 // object.
108 final Inflater inflater = InflaterCache.get();
109 try {
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];
114 int avail = 0;
115 while (!inflater.finished() && avail < hdr.length)
116 try {
117 avail += inflater.inflate(hdr, avail, hdr.length
118 - avail);
119 } catch (DataFormatException dfe) {
120 final CorruptObjectException coe;
121 coe = new CorruptObjectException(id, "bad stream");
122 coe.initCause(dfe);
123 inflater.end();
124 throw coe;
126 if (avail < 5)
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);
132 if (objectSize < 0)
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];
137 if (p.value < avail)
138 System.arraycopy(hdr, p.value, bytes, 0, avail - p.value);
139 decompress(id, inflater, avail - p.value);
140 } else {
141 int p = 0;
142 int c = compressed[p++] & 0xff;
143 final int typeCode = (c >> 4) & 7;
144 int size = c & 15;
145 int shift = 4;
146 while ((c & 0x80) != 0) {
147 c = compressed[p++] & 0xff;
148 size += (c & 0x7f) << shift;
149 shift += 7;
152 switch (typeCode) {
153 case Constants.OBJ_COMMIT:
154 case Constants.OBJ_TREE:
155 case Constants.OBJ_BLOB:
156 case Constants.OBJ_TAG:
157 objectType = typeCode;
158 break;
159 default:
160 throw new CorruptObjectException(id, "invalid type");
163 objectSize = size;
164 bytes = new byte[objectSize];
165 inflater.setInput(compressed, p, compressed.length - p);
166 decompress(id, inflater, 0);
168 } finally {
169 InflaterCache.release(inflater);
173 private void decompress(final AnyObjectId id, final Inflater inf, int p)
174 throws CorruptObjectException {
175 try {
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");
181 coe.initCause(dfe);
182 throw coe;
184 if (p != objectSize)
185 throw new CorruptObjectException(id, "incorrect length");
188 @Override
189 public int getType() {
190 return objectType;
193 @Override
194 public long getSize() {
195 return objectSize;
198 @Override
199 public byte[] getCachedBytes() {
200 return bytes;
203 @Override
204 public int getRawType() {
205 return objectType;
208 @Override
209 public long getRawSize() {
210 return objectSize;