Created mapping between IContainers within Eclipse and
[egit/egit-new.git] / org.spearce.jgit / src / org / spearce / jgit / lib / ObjectReader.java
blobafdb05e83be2116511eb0dbbcfad4dd7648c8178
1 package org.spearce.jgit.lib;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.UnsupportedEncodingException;
8 import java.util.zip.InflaterInputStream;
10 public class ObjectReader {
11 private static final int TYPESZ = 16;
13 private final ObjectId objectId;
15 private final String objectType;
17 private final int objectSize;
19 private InflaterInputStream inflater;
21 public ObjectReader(final ObjectId id, final InputStream src)
22 throws IOException {
23 objectId = id;
24 inflater = new InflaterInputStream(src);
26 final StringBuffer tempType = new StringBuffer(TYPESZ);
27 int tempSize = 0;
28 int c;
30 for (;;) {
31 c = inflater.read();
32 if (' ' == c) {
33 break;
35 if (c < 'a' || c > 'z' || tempType.length() >= 16) {
36 throw new CorruptObjectException(id, "bad type in header");
38 tempType.append((char) c);
40 objectType = tempType.toString();
42 for (;;) {
43 c = inflater.read();
44 if (0 == c) {
45 break;
47 if (c < '0' || c > '9') {
48 throw new CorruptObjectException(id, "bad length in header");
50 tempSize *= 10;
51 tempSize += c - '0';
53 objectSize = tempSize;
56 public ObjectId getId() {
57 return objectId;
60 public String getType() {
61 return objectType;
64 public int getSize() {
65 return objectSize;
68 public InputStream getInputStream() {
69 if (inflater == null) {
70 throw new IllegalStateException("Already closed.");
72 return inflater;
75 public BufferedReader getBufferedReader()
76 throws UnsupportedEncodingException {
77 return new BufferedReader(new InputStreamReader(getInputStream(),
78 "UTF-8"));
81 public void close() throws IOException {
82 if (inflater != null) {
83 inflater.close();
84 inflater = null;