Initial set of files.
[egit/egit-new.git] / src / org / spearce / jgit / lib / ObjectReader.java
blobf0b0fee55e0a700e27c71882a06b0bdaf1f62923
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 final ObjectId objectId;
13 private final String objectType;
15 private final int objectSize;
17 private InflaterInputStream inflater;
19 public ObjectReader(final ObjectId id, final InputStream src)
20 throws IOException {
21 objectId = id;
22 inflater = new InflaterInputStream(src);
24 final StringBuffer tempType = new StringBuffer(16);
25 int tempSize = 0;
26 int c;
28 for (;;) {
29 c = inflater.read();
30 if (' ' == c) {
31 break;
33 if (c < 'a' || c > 'z') {
34 throw new CorruptObjectException("Corrupt header in "
35 + objectId);
37 if (tempType.length() >= 16) {
38 throw new CorruptObjectException("Type header exceed limit in "
39 + objectId);
41 tempType.append((char) c);
43 objectType = tempType.toString();
45 for (;;) {
46 c = inflater.read();
47 if (0 == c) {
48 break;
50 if (c < '0' || c > '9') {
51 throw new CorruptObjectException("Corrupt header in "
52 + objectId);
54 tempSize *= 10;
55 tempSize += c - '0';
57 objectSize = tempSize;
60 public ObjectId getId() {
61 return objectId;
64 public String getType() {
65 return objectType;
68 public int getSize() {
69 return objectSize;
72 public BufferedReader getBufferedReader()
73 throws UnsupportedEncodingException {
74 return new BufferedReader(new InputStreamReader(inflater, "UTF-8"));
77 public InputStream getInputStream() {
78 return inflater;
81 public void close() throws IOException {
82 inflater.close();
83 inflater = null;