I am quite new to Java. I have data structured like this:
tree 503(there is a null character here)040000 tree 0bfec80bc7b7aa0a50e0b373044929cb0aae278b build/
100755 blob e30876c61eb7712ad09b50593a30513d9b173805 build.xml
100755 blob 1574df4a2de5d2884f33a5d3e30ae3daa5affee1 manifest.mf
040000 tree fdab6a12a56a709212f105c9c8e31cd062d01e0a nbproject/
040000 tree 3c977009b6104310d7f3a66200f29bff8cda7576 src/
100755 blob de4bcf5c862c352531819e082204bb51ddabaa9c test_file
100755 blob 26af8cac3b104b4b6514621b1c512e353307a867 unicode.txt
I call each line of this an entry. To parse this data into an array of entries, I wrote this;
class TreeEntry {
public String perms;
public String type;
public String hash;
public String name;
@Override
public String toString() {
return perms + " " + type + " " + hash + " " + name;
}
}
private static TreeEntry[] parseTree(String hash) {
String tree = readObject(hash);
if (!tree.startsWith("tree")) {
System.err.println("Given hash doesn't belong to a tree object");
return null;
}
int currentArrayCapacity = 4;
int currentEntries = 0;
TreeEntry[] entries = new TreeEntry[currentArrayCapacity];
// We are interested in data after null character
int nullCharacterPosition = 0;
while (tree.charAt(nullCharacterPosition) != '\0') {
nullCharacterPosition++;
}
tree = tree.substring(nullCharacterPosition + 1);
StringTokenizer st = new StringTokenizer(tree, "\n");
while (st.hasMoreElements()) {
TreeEntry e = new TreeEntry();
StringTokenizer st2 = new StringTokenizer((String)st.nextElement(), " ");
e.perms = (String) st2.nextElement();
e.type = (String) st2.nextElement();
e.hash = (String) st2.nextElement();
e.name = (String) st2.nextElement();
// There can be space in name
while (st2.hasMoreElements()) {
e.name += " " + (String) st2.nextElement();
}
currentEntries++;
// To increase capacity of the array
if (currentEntries > currentArrayCapacity) {
currentArrayCapacity = currentArrayCapacity * 2;
TreeEntry[] tmp = new TreeEntry[currentArrayCapacity];
System.arraycopy(entries, 0, tmp, 0, entries.length);
entries = tmp;
}
entries[currentEntries - 1] = e;
}
return entries;
}
How does it look as a Java code, and how does it look data structures and algorithms wise?