@Override public void writeExternal(ObjectOutput oo) throws IOException { /* * Serialization format: * byte 1: serial version * byte 2: name length (l) * byte 3 to l+3: name * byte l+4 to l+67: size of node * byte l+68: children count * byte l+69 to n: children (recursive) */ if (name != null && name.length() > Short.MAX_VALUE) throw new IOException("Name of node exceeds maximum allowed size of " + Short.MAX_VALUE); if (children.size() > Short.MAX_VALUE) throw new IOException( "Number of subfolders in " + name + " exceeds maximum allowed number: " + Short.MAX_VALUE); oo.writeByte(serialVersion); if (name != null) { oo.writeByte(name.length()); oo.writeBytes(name); } else { oo.writeByte(1); oo.writeByte(0); } oo.writeLong(size); oo.writeByte(children.size()); for (FSTree child : children.values()) { child.writeExternal(oo); } }
/** Recursive helper method for get() */ private FSTree get(StringTokenizer tokenizer) { if (tokenizer.hasMoreElements()) { String name = tokenizer.nextToken(); FSTree child = children.get(name); if (child == null) return null; return child.get(tokenizer); } else { return this; } }
/** Recursive helper method for the enumeration methods. */ private void enumerateRecurse(List<String> list, StringBuilder builder, boolean includeAll) { int pos = builder.length(); if (name != null) { if (pos > 0) builder.append(File.separatorChar); builder.append(name); } if (children.isEmpty()) { list.add(builder.toString()); } else { if (includeAll) list.add(builder.toString()); for (FSTree child : children.values()) child.enumerateRecurse(list, builder, includeAll); } builder.setLength(pos); }
/** Recursive helper method for remove() */ private long remove(StringTokenizer tokenizer) { if (tokenizer.hasMoreTokens()) { String name = tokenizer.nextToken(); FSTree child = children.get(name); if (!tokenizer.hasMoreTokens() && child != null) { children.remove(name); size -= child.size; return child.size; } else if (child != null) { long cSize = child.remove(tokenizer); size -= cSize; return cSize; } } return 0L; }
/** Recursive helper method for add() */ private void addRecurse(StringTokenizer tokenizer, long size) { this.size += size; if (tokenizer.hasMoreTokens()) { String name = tokenizer.nextToken(); if (tokenizer.hasMoreTokens()) { FSTree child = children.get(name); if (child == null) { child = new FSTree(name, 0L, this); children.put(new String(name.toCharArray()), child); } child.addRecurse(tokenizer, size); } else { children.put(new String(name.toCharArray()), new FSTree(name, size, this)); } } }
@Override public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException { if (oi.readByte() != serialVersion) throw new IOException("Serial version mismatch"); int nameLength = (short) oi.readByte() & 0xFF; // Remove possible negative sign extension from the cast byte[] name = new byte[nameLength]; oi.readFully(name, 0, nameLength); if (nameLength == 1 && name[0] == 0) this.name = null; else this.name = new String(name); size = oi.readLong(); int childCount = (short) oi.readByte() & 0xFF; for (int i = 0; i < childCount; i++) { FSTree child = new FSTree(null, 0L, this); child.readExternal(oi); children.put(child.getName(), child); } }
public FSTreeIterator(FSTree nextNode) { leaves = new LinkedList<String>(); nextNode.enumerateLeaves(leaves); leavesIterator = leaves.iterator(); }