001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package wim_data;
028:
029: import java.util.Vector;
030: import java.util.Hashtable;
031: import java.io.ByteArrayOutputStream;
032: import java.io.DataOutputStream;
033: import java.io.IOException;
034:
035: /**
036: * Represents WIM file system.
037: */
038: public class FileSystem {
039:
040: /** File attribute constant. */
041: static final int DIR = 1;
042: /** File attribute constant. */
043: static final int PIN = 3;
044: /** File attribute constant. */
045: static final int PrK = 4;
046: /** File attribute constant. */
047: static final int WIM = 9;
048: /** File attribute constant. */
049: static final int READ = 16;
050: /** File attribute constant. */
051: static final int UPDATE = 48;
052: /** File attribute constant. */
053: static final int EMPTY = 64;
054:
055: /** Root directory for this file system. */
056: CardFile root;
057: /** Base directory for WIM files. */
058: CardFile base;
059:
060: /** Hash table used to determine file offsets in resuting data. */
061: Hashtable labels = new Hashtable(3);
062:
063: /**
064: * Constructor.
065: * @param path root directory for this file system
066: */
067: public FileSystem(short[] path) {
068:
069: root = new CardFile(path[0], DIR);
070: base = root;
071:
072: for (int i = 1; i < path.length; i++) {
073:
074: CardFile f = new CardFile(path[i], DIR);
075: base.addFile(f);
076: base = f;
077: }
078: base.type = WIM;
079: }
080:
081: /**
082: * Adds a new file to the file system.
083: * @param path file path
084: * @param type file type
085: * @param data file body
086: */
087: public void addFile(short[] path, int type, byte[] data) {
088:
089: if (path == null) {
090: return;
091: }
092: addFile(path, type, data, null);
093: }
094:
095: /**
096: * Adds a new file to the file system.
097: * @param path file path
098: * @param type file type
099: * @param data file body
100: * @param label label that can be used to retrieve file offset
101: */
102: public void addFile(short[] path, int type, byte[] data,
103: String label) {
104:
105: if (path == null) {
106: return;
107: }
108:
109: CardFile top;
110:
111: if (path.length > 1) {
112:
113: if (path[0] == 0x3fff) {
114: top = base;
115: } else if (path[0] == root.id) {
116: top = root;
117: } else {
118: throw new IllegalArgumentException("Invalid path");
119: }
120:
121: for (int i = 1; i < path.length - 1; i++) {
122:
123: CardFile f = top.getFile(path[i]);
124: if (f == null) {
125: f = new CardFile(path[i], DIR);
126: top.addFile(f);
127: }
128: if (f.type != DIR) {
129: throw new IllegalArgumentException("Invalid path 1");
130: }
131: top = f;
132: }
133: } else {
134: top = base;
135: }
136:
137: CardFile f = new CardFile(path[path.length - 1], type);
138: f.setData(data);
139: top.addFile(f);
140:
141: if (label != null) {
142: labels.put(label, f);
143: }
144: }
145:
146: /**
147: * Returs file offset in resulting data array.
148: * @param label file label
149: * @return offset value
150: */
151: public int getOffset(String label) {
152: CardFile c = (CardFile) labels.get(label);
153: if (c == null) {
154: System.out.println("Warning: label not found: " + label);
155: return -32768;
156: }
157: return c.offset;
158: }
159:
160: /**
161: * Returns binary representation of this file system.
162: * @return binary representation of this file system
163: * @throws IOException if I/O error occurs
164: */
165: public byte[] getEncoded() throws IOException {
166:
167: ByteArrayOutputStream baos = new ByteArrayOutputStream();
168: DataOutputStream out = new DataOutputStream(baos);
169:
170: root.write(out);
171: return baos.toByteArray();
172: }
173:
174: /**
175: * This class represents card file.
176: */
177: class CardFile {
178:
179: /** File identifier. */
180: int id;
181:
182: /** File type. */
183: int type;
184:
185: /** File body. */
186: byte[] data;
187:
188: /** Files that this file contains (if any). */
189: Vector files;
190:
191: /** File offset in binary representation of the file system. */
192: int offset;
193:
194: /**
195: * Constructor.
196: * @param id file identifier
197: * @param type file type
198: */
199: CardFile(int id, int type) {
200:
201: this .id = id;
202: this .type = type;
203: files = new Vector();
204: }
205:
206: /**
207: * Set file data.
208: * @param data file data
209: */
210: void setData(byte[] data) {
211: this .data = data;
212: }
213:
214: /**
215: * Add file to this DF.
216: * @param f the file
217: */
218: void addFile(CardFile f) {
219: files.add(f);
220: }
221:
222: /**
223: * Returns file with this ID in this DF or null.
224: * @param id file identifier
225: * @return file with this ID in this DF or null
226: */
227: CardFile getFile(int id) {
228:
229: CardFile f;
230:
231: for (int i = 0; i < files.size(); i++) {
232:
233: f = (CardFile) files.elementAt(i);
234: if (f.id == id) {
235: return f;
236: }
237: }
238: return null;
239: }
240:
241: /**
242: * Writes binary representation of this file into the stream.
243: * @param out output stream
244: * @throws IOException if I/O error occurs
245: */
246: void write(DataOutputStream out) throws IOException {
247:
248: out.writeShort(id);
249: out.writeByte(type);
250: if ((type & DIR) == 0) {
251: out.writeShort(data.length);
252: offset = out.size();
253:
254: if ((type & EMPTY) == 0) {
255: out.write(data);
256: } else {
257: int i = data.length - 1;
258: while (i > -1 && data[i] == 0) {
259: i--;
260: }
261: i++;
262: out.writeShort(i);
263: out.write(data, 0, i);
264: }
265: } else {
266: out.writeShort(files.size());
267: for (int i = 0; i < files.size(); i++) {
268:
269: CardFile f = (CardFile) files.elementAt(i);
270: f.write(out);
271: }
272: }
273: }
274: }
275: }
|