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
;
42 public void setId(final ObjectId id
)
47 public String
getType() throws IOException
49 if (objectType
== null && deltaBase
!= null)
51 objectType
= baseReader().getType();
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();
68 public ObjectId
getDeltaBaseId()
73 public long getDataOffset()
78 public InputStream
getInputStream() throws IOException
80 if (deltaBase
!= null)
82 final ObjectReader b
= baseReader();
83 final PatchDeltaStream p
= new PatchDeltaStream(packStream(), b
);
86 objectSize
= p
.getResultLength();
88 if (objectType
== null)
90 objectType
= b
.getType();
97 public void close() throws IOException
101 private ObjectReader
baseReader() throws IOException
103 final ObjectReader or
= pack
.resolveBase(getDeltaBaseId());
106 throw new CorruptObjectException(
108 "is a delta base for another object but is missing.");
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];
139 public int read(final byte[] b
, final int off
, final int len
)
147 if (inf
.needsInput())
149 final int n
= pack
.read(offset
, in
, 0, in
.length
);
150 inf
.setInput(in
, 0, n
);
156 final int n
= inf
.inflate(b
, off
, len
);
159 pack
.unread(inf
.getRemaining());
163 catch (DataFormatException dfe
)
165 final IOException e
= new IOException("Corrupt ZIP stream.");