KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > ms > DefaultEntry


1 /* DefaultEntry
2 *
3 * Created on September 12, 2006
4 *
5 * Copyright (C) 2006 Internet Archive.
6 *
7 * This file is part of the Heritrix web crawler (crawler.archive.org).
8 *
9 * Heritrix is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * any later version.
13 *
14 * Heritrix is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser Public License
20 * along with Heritrix; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.archive.util.ms;
24
25 import java.io.IOException JavaDoc;
26 import java.nio.ByteBuffer JavaDoc;
27 import java.nio.ByteOrder JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.archive.util.IoUtils;
32 import org.archive.io.SeekInputStream;
33
34 class DefaultEntry implements Entry {
35
36     
37     private DefaultBlockFileSystem origin;
38     private String JavaDoc name;
39     private EntryType type;
40     private int previous;
41     private int next;
42     private int child;
43     private int startBlock;
44     private int size;
45     private int index;
46     
47     
48     public DefaultEntry(DefaultBlockFileSystem origin, SeekInputStream input, int index)
49     throws IOException JavaDoc {
50         this.index = index;
51         // FIXME: Read directly from the stream
52
this.origin = origin;
53         byte[] temp = new byte[128];
54         IoUtils.readFully(input, temp);
55         ByteBuffer JavaDoc buf = ByteBuffer.wrap(temp);
56         buf.order(ByteOrder.LITTLE_ENDIAN);
57         buf.position(0);
58         
59         StringBuilder JavaDoc nameBuf = new StringBuilder JavaDoc();
60         
61         char ch = buf.getChar();
62         while (ch != 0) {
63             nameBuf.append(ch);
64             ch = buf.getChar();
65         }
66         this.name = nameBuf.toString();
67         
68         byte typeFlag = buf.get(0x42);
69         switch (typeFlag) {
70             case 1:
71                 this.type = EntryType.DIRECTORY;
72                 break;
73             case 2:
74                 this.type = EntryType.FILE;
75                 break;
76             case 5:
77                 this.type = EntryType.ROOT;
78                 break;
79             default:
80                 throw new IllegalStateException JavaDoc("Invalid type: " + typeFlag);
81         }
82         
83         this.previous = buf.getInt(0x44);
84         this.next = buf.getInt(0x48);
85         this.child = buf.getInt(0x4C);
86         this.startBlock = buf.getInt(0x74);
87         this.size = buf.getInt(0x78);
88     }
89     
90     
91     public String JavaDoc getName() {
92         return name;
93     }
94     
95     
96     public EntryType getType() {
97         return type;
98     }
99    
100     
101     public Entry getNext() throws IOException JavaDoc {
102         return origin.getEntry(next);
103     }
104     
105     
106     public Entry getPrevious() throws IOException JavaDoc {
107         return origin.getEntry(previous);
108     }
109     
110     
111     public Entry getChild() throws IOException JavaDoc {
112         return origin.getEntry(child);
113     }
114     
115     public SeekInputStream open() throws IOException JavaDoc {
116         return new BlockInputStream(origin, startBlock);
117     }
118
119
120     public List JavaDoc<Entry> list() throws IOException JavaDoc {
121         if (child < 0) {
122             throw new IllegalStateException JavaDoc("Can't list non-directory.");
123         }
124         Entry child = getChild();
125         ArrayList JavaDoc<Entry> r = new ArrayList JavaDoc<Entry>();
126         list(r, child);
127         return r;
128     }
129
130
131     public static void list(List JavaDoc<Entry> list, Entry e) throws IOException JavaDoc {
132         if (e == null) {
133             return;
134         }
135         list.add(e);
136         list(list, e.getPrevious());
137         list(list, e.getNext());
138     }
139
140
141     public int getIndex() {
142         return index;
143     }
144
145
146     public String JavaDoc toString() {
147         StringBuilder JavaDoc result = new StringBuilder JavaDoc("Entry{");
148         result.append("name=").append(name);
149         result.append(" index=").append(index);
150         result.append(" type=").append(type);
151         result.append(" size=").append(size);
152         result.append(" prev=").append(previous);
153         result.append(" next=").append(next);
154         result.append(" child=").append(child);
155         result.append(" startBlock=").append(startBlock);
156         result.append("}");
157         return result.toString();
158     }
159
160
161 }
162
Popular Tags