Initial set of files.
[egit/egit-new.git] / src / org / spearce / jgit / lib / Tree.java
blobb62a67cb64ca6793667edc1d38ee7e0cfab70db6
1 package org.spearce.jgit.lib;
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 import java.util.List;
9 public class Tree implements Treeish {
10 private final ObjectDatabase objdb;
12 private final ObjectId treeId;
14 private final List entries;
16 public Tree(final ObjectDatabase db, final ObjectId id, final InputStream is)
17 throws IOException {
18 objdb = db;
19 treeId = id;
21 final ArrayList tempEnts = new ArrayList();
22 for (;;) {
23 int c;
24 int mode;
25 final ByteArrayOutputStream nameBuf;
26 final byte[] entId;
27 int entIdLen;
29 c = is.read();
30 if (c == -1) {
31 break;
33 if (c < '0' || c > '7') {
34 throw new CorruptObjectException("Invalid tree entry in " + id);
36 mode = c - '0';
37 for (;;) {
38 c = is.read();
39 if (' ' == c) {
40 break;
42 if (c < '0' || c > '7') {
43 throw new CorruptObjectException("Invalid tree entry in "
44 + id);
46 mode *= 8;
47 mode += c - '0';
50 nameBuf = new ByteArrayOutputStream(128);
51 for (;;) {
52 c = is.read();
53 if (c == -1) {
54 throw new CorruptObjectException("Invalid tree entry in "
55 + id);
57 if (0 == c) {
58 break;
60 nameBuf.write(c);
63 entId = new byte[20];
64 entIdLen = 0;
65 while ((c = is.read(entId, entIdLen, entId.length - entIdLen)) > 0) {
66 entIdLen += c;
68 if (entIdLen != entId.length) {
69 throw new CorruptObjectException("Invalid tree entry in " + id);
72 tempEnts.add(new Entry(mode, new String(nameBuf.toByteArray(),
73 "UTF-8"), new ObjectId(entId)));
76 entries = tempEnts;
79 public ObjectId getTreeId() {
80 return treeId;
83 public List getTreeEntries() {
84 return entries;
87 public class Entry {
88 private final int mode;
90 private final String name;
92 private final ObjectId id;
94 private Tree treeObj;
96 private Entry(final int mode, final String name, final ObjectId id) {
97 this.mode = mode;
98 this.name = name;
99 this.id = id;
102 public boolean isTree() {
103 return (getMode() & 040000) != 0;
106 public boolean isSymlink() {
107 return (getMode() & 020000) != 0;
110 public boolean isExecutable() {
111 return (getMode() & 0100) != 0;
114 public String getName() {
115 return name;
118 public int getMode() {
119 return mode;
122 public ObjectId getId() {
123 return id;
126 public Tree getTree() throws IOException {
127 if (treeObj == null) {
128 treeObj = objdb.openTree(getId());
130 return treeObj;
133 public ObjectReader openBlob() throws IOException {
134 return objdb.openBlob(getId());
137 public String toString() {
138 final StringBuffer r = new StringBuffer();
139 final String modeStr = Integer.toString(getMode(), 8);
140 r.append(getId());
141 r.append(' ');
142 if (isTree()) {
143 r.append('D');
144 } else if (isSymlink()) {
145 r.append('S');
146 } else if (isExecutable()) {
147 r.append('X');
148 } else {
149 r.append('F');
151 r.append(' ');
152 if (modeStr.length() == 5) {
153 r.append('0');
155 r.append(modeStr);
156 r.append(' ');
157 r.append(getName());
158 return r.toString();