001: /* DefaultEntry
002: *
003: * Created on September 12, 2006
004: *
005: * Copyright (C) 2006 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.util.ms;
024:
025: import java.io.IOException;
026: import java.nio.ByteBuffer;
027: import java.nio.ByteOrder;
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import org.archive.util.IoUtils;
032: import org.archive.io.SeekInputStream;
033:
034: class DefaultEntry implements Entry {
035:
036: private DefaultBlockFileSystem origin;
037: private String name;
038: private EntryType type;
039: private int previous;
040: private int next;
041: private int child;
042: private int startBlock;
043: private int size;
044: private int index;
045:
046: public DefaultEntry(DefaultBlockFileSystem origin,
047: SeekInputStream input, int index) throws IOException {
048: this .index = index;
049: // FIXME: Read directly from the stream
050: this .origin = origin;
051: byte[] temp = new byte[128];
052: IoUtils.readFully(input, temp);
053: ByteBuffer buf = ByteBuffer.wrap(temp);
054: buf.order(ByteOrder.LITTLE_ENDIAN);
055: buf.position(0);
056:
057: StringBuilder nameBuf = new StringBuilder();
058:
059: char ch = buf.getChar();
060: while (ch != 0) {
061: nameBuf.append(ch);
062: ch = buf.getChar();
063: }
064: this .name = nameBuf.toString();
065:
066: byte typeFlag = buf.get(0x42);
067: switch (typeFlag) {
068: case 1:
069: this .type = EntryType.DIRECTORY;
070: break;
071: case 2:
072: this .type = EntryType.FILE;
073: break;
074: case 5:
075: this .type = EntryType.ROOT;
076: break;
077: default:
078: throw new IllegalStateException("Invalid type: " + typeFlag);
079: }
080:
081: this .previous = buf.getInt(0x44);
082: this .next = buf.getInt(0x48);
083: this .child = buf.getInt(0x4C);
084: this .startBlock = buf.getInt(0x74);
085: this .size = buf.getInt(0x78);
086: }
087:
088: public String getName() {
089: return name;
090: }
091:
092: public EntryType getType() {
093: return type;
094: }
095:
096: public Entry getNext() throws IOException {
097: return origin.getEntry(next);
098: }
099:
100: public Entry getPrevious() throws IOException {
101: return origin.getEntry(previous);
102: }
103:
104: public Entry getChild() throws IOException {
105: return origin.getEntry(child);
106: }
107:
108: public SeekInputStream open() throws IOException {
109: return new BlockInputStream(origin, startBlock);
110: }
111:
112: public List<Entry> list() throws IOException {
113: if (child < 0) {
114: throw new IllegalStateException("Can't list non-directory.");
115: }
116: Entry child = getChild();
117: ArrayList<Entry> r = new ArrayList<Entry>();
118: list(r, child);
119: return r;
120: }
121:
122: public static void list(List<Entry> list, Entry e)
123: throws IOException {
124: if (e == null) {
125: return;
126: }
127: list.add(e);
128: list(list, e.getPrevious());
129: list(list, e.getNext());
130: }
131:
132: public int getIndex() {
133: return index;
134: }
135:
136: public String toString() {
137: StringBuilder result = new StringBuilder("Entry{");
138: result.append("name=").append(name);
139: result.append(" index=").append(index);
140: result.append(" type=").append(type);
141: result.append(" size=").append(size);
142: result.append(" prev=").append(previous);
143: result.append(" next=").append(next);
144: result.append(" child=").append(child);
145: result.append(" startBlock=").append(startBlock);
146: result.append("}");
147: return result.toString();
148: }
149:
150: }
|