Implemented subset mapping.
[egit/egit-new.git] / org.spearce.jgit / src / org / spearce / jgit / lib / PackedObjectReader.java
blobd3ac153603201add65656416d151ede71544764a
1 package org.spearce.jgit.lib;
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.zip.DataFormatException;
7 import java.util.zip.Inflater;
9 public class PackedObjectReader extends ObjectReader
11 private final PackReader pack;
13 private final long dataOffset;
15 private final ObjectId deltaBase;
17 private String objectType;
19 private long objectSize;
21 PackedObjectReader(
22 final PackReader pr,
23 final String type,
24 final long size,
25 final long offset,
26 final ObjectId base)
28 pack = pr;
29 dataOffset = offset;
30 deltaBase = base;
31 if (base != null)
33 objectSize = -1;
35 else
37 objectSize = size;
38 objectType = type;
42 public void setId(final ObjectId id)
44 super.setId(id);
47 public String getType() throws IOException
49 if (objectType == null && deltaBase != null)
51 objectType = baseReader().getType();
53 return objectType;
56 public long getSize() throws IOException
58 if (objectSize == -1 && deltaBase != null)
60 final PatchDeltaStream p;
61 p = new PatchDeltaStream(packStream(), null);
62 objectSize = p.getResultLength();
63 p.close();
65 return objectSize;
68 public ObjectId getDeltaBaseId()
70 return deltaBase;
73 public long getDataOffset()
75 return dataOffset;
78 public InputStream getInputStream() throws IOException
80 if (deltaBase != null)
82 final ObjectReader b = baseReader();
83 final PatchDeltaStream p = new PatchDeltaStream(packStream(), b);
84 if (objectSize == -1)
86 objectSize = p.getResultLength();
88 if (objectType == null)
90 objectType = b.getType();
92 return p;
94 return packStream();
97 public void close() throws IOException
101 private ObjectReader baseReader() throws IOException
103 final ObjectReader or = pack.resolveBase(getDeltaBaseId());
104 if (or == null)
106 throw new CorruptObjectException(
107 deltaBase,
108 "is a delta base for another object but is missing.");
110 return or;
113 private BufferedInputStream packStream()
115 return new BufferedInputStream(new PackStream());
118 private class PackStream extends InputStream
120 private Inflater inf = new Inflater();
122 private byte[] in = new byte[2048];
124 private long offset = getDataOffset();
126 public int read() throws IOException
128 final byte[] singleByteBuffer = new byte[1];
129 if (read(singleByteBuffer, 0, 1) == 1)
131 return singleByteBuffer[0];
133 else
135 return -1;
139 public int read(final byte[] b, final int off, final int len)
140 throws IOException
142 if (inf.finished())
144 return -1;
147 if (inf.needsInput())
149 final int n = pack.read(offset, in, 0, in.length);
150 inf.setInput(in, 0, n);
151 offset += n;
156 final int n = inf.inflate(b, off, len);
157 if (inf.finished())
159 pack.unread(inf.getRemaining());
161 return n;
163 catch (DataFormatException dfe)
165 final IOException e = new IOException("Corrupt ZIP stream.");
166 e.initCause(dfe);
167 throw e;
171 public void close()
173 if (inf != null)
175 inf.end();
176 inf = null;
177 in = null;