1 Clazz.declarePackage ("JU");
2 Clazz.load (["JU.BinaryDocument", "$.CompoundDocHeader", "$.Lst"], "JU.CompoundDocument", ["java.io.DataInputStream", "JU.CompoundDocDirEntry", "$.SB", "$.ZipData"], function () {
3 c$ = Clazz.decorateAsClass (function () {
10 this.shortSectorSize = 0;
11 this.nShortSectorsPerStandardSector = 0;
12 this.nIntPerSector = 0;
13 this.nDirEntriesperSector = 0;
15 Clazz.instantialize (this, arguments);
16 }, JU, "CompoundDocument", JU.BinaryDocument);
17 Clazz.prepareFields (c$, function () {
18 this.header = new JU.CompoundDocHeader (this);
19 this.directory = new JU.Lst ();
21 Clazz.makeConstructor (c$,
23 Clazz.superConstructor (this, JU.CompoundDocument);
24 this.isBigEndian = true;
26 Clazz.overrideMethod (c$, "setStream",
27 function (jzt, bis, isBigEndian) {
30 this.stream = new java.io.DataInputStream (bis);
31 }this.stream.mark (2147483647);
32 if (!this.readHeader ()) return;
33 this.getSectorAllocationTable ();
34 this.getShortSectorAllocationTable ();
35 this.getDirectoryTable ();
36 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~B");
37 Clazz.defineMethod (c$, "getDirectory",
39 return this.directory;
41 Clazz.defineMethod (c$, "getDirectoryListing",
42 function (separator) {
44 for (var i = 0; i < this.directory.size (); i++) {
45 var thisEntry = this.directory.get (i);
46 if (!thisEntry.isEmpty) str += separator + thisEntry.entryName + "\tlen=" + thisEntry.lenStream + "\tSID=" + thisEntry.SIDfirstSector + (thisEntry.isStandard ? "\tfileOffset=" + this.getOffset (thisEntry.SIDfirstSector) : "");
50 Clazz.defineMethod (c$, "getAllData",
52 return this.getAllDataFiles (null, null);
54 Clazz.overrideMethod (c$, "getAllDataMapped",
55 function (prefix, binaryFileList, fileData) {
56 fileData.put ("#Directory_Listing", this.getDirectoryListing ("|"));
57 binaryFileList = "|" + binaryFileList + "|";
58 for (var i = 0; i < this.directory.size (); i++) {
59 var thisEntry = this.directory.get (i);
60 if (!thisEntry.isEmpty && thisEntry.entryType != 5) {
61 var name = thisEntry.entryName;
62 System.out.println ("CompoundDocument file " + name);
63 var isBinary = (binaryFileList.indexOf ("|" + name + "|") >= 0);
64 if (isBinary) name += ":asBinaryString";
65 var data = new JU.SB ();
66 data.append ("BEGIN Directory Entry ").append (name).append ("\n");
67 data.appendSB (this.getEntryAsString (thisEntry, isBinary));
68 data.append ("\nEND Directory Entry ").append (name).append ("\n");
69 fileData.put (prefix + "/" + name, data.toString ());
72 }, "~S,~S,java.util.Map");
73 Clazz.overrideMethod (c$, "getAllDataFiles",
74 function (binaryFileList, firstFile) {
75 if (firstFile != null) {
76 for (var i = 0; i < this.directory.size (); i++) {
77 var thisEntry = this.directory.get (i);
78 if (thisEntry.entryName.equals (firstFile)) {
79 this.directory.remove (i);
80 this.directory.add (1, thisEntry);
83 }this.data = new JU.SB ();
84 this.data.append ("Compound Document File Directory: ");
85 this.data.append (this.getDirectoryListing ("|"));
86 this.data.append ("\n");
87 binaryFileList = "|" + binaryFileList + "|";
88 for (var i = 0; i < this.directory.size (); i++) {
89 var thisEntry = this.directory.get (i);
90 if (!thisEntry.isEmpty && thisEntry.entryType != 5) {
91 var name = thisEntry.entryName;
92 if (name.endsWith (".gz")) name = name.substring (0, name.length - 3);
93 this.data.append ("BEGIN Directory Entry ").append (name).append ("\n");
94 this.data.appendSB (this.getEntryAsString (thisEntry, binaryFileList.indexOf ("|" + thisEntry.entryName + "|") >= 0));
95 this.data.append ("\n");
96 this.data.append ("END Directory Entry ").append (thisEntry.entryName).append ("\n");
101 Clazz.defineMethod (c$, "getFileAsString",
102 function (entryName) {
103 for (var i = 0; i < this.directory.size (); i++) {
104 var thisEntry = this.directory.get (i);
105 if (thisEntry.entryName.equals (entryName)) return this.getEntryAsString (thisEntry, false);
109 Clazz.defineMethod (c$, "getOffset",
111 return (SID + 1) * this.sectorSize;
113 Clazz.defineMethod (c$, "gotoSector",
115 this.seek (this.getOffset (SID));
117 Clazz.defineMethod (c$, "readHeader",
119 if (!this.header.readData ()) return false;
120 this.sectorSize = 1 << this.header.sectorPower;
121 this.shortSectorSize = 1 << this.header.shortSectorPower;
122 this.nShortSectorsPerStandardSector = Clazz.doubleToInt (this.sectorSize / this.shortSectorSize);
123 this.nIntPerSector = Clazz.doubleToInt (this.sectorSize / 4);
124 this.nDirEntriesperSector = Clazz.doubleToInt (this.sectorSize / 128);
127 Clazz.defineMethod (c$, "getSectorAllocationTable",
131 this.SAT = Clazz.newIntArray (this.header.nSATsectors * this.nIntPerSector + 109, 0);
133 for (var i = 0; i < 109; i++) {
134 thisSID = this.header.MSAT0[i];
135 if (thisSID < 0) break;
136 this.gotoSector (thisSID);
137 for (var j = 0; j < this.nIntPerSector; j++) {
138 this.SAT[nSID++] = this.readInt ();
141 var nMaster = this.header.nAdditionalMATsectors;
142 thisSID = this.header.SID_MSAT_next;
143 var MSAT = Clazz.newIntArray (this.nIntPerSector, 0);
144 out : while (nMaster-- > 0 && thisSID >= 0) {
145 this.gotoSector (thisSID);
146 for (var i = 0; i < this.nIntPerSector; i++) MSAT[i] = this.readInt ();
148 for (var i = 0; i < this.nIntPerSector - 1; i++) {
150 if (thisSID < 0) break out;
151 this.gotoSector (thisSID);
152 for (var j = this.nIntPerSector; --j >= 0; ) this.SAT[nSID++] = this.readInt ();
155 thisSID = MSAT[this.nIntPerSector - 1];
158 if (Clazz.exceptionOf (e, Exception)) {
159 System.out.println (e.toString ());
165 Clazz.defineMethod (c$, "getShortSectorAllocationTable",
168 var thisSID = this.header.SID_SSAT_start;
169 var nMax = this.header.nSSATsectors * this.nIntPerSector;
170 this.SSAT = Clazz.newIntArray (nMax, 0);
172 while (thisSID > 0 && nSSID < nMax) {
173 this.gotoSector (thisSID);
174 for (var j = 0; j < this.nIntPerSector; j++) {
175 this.SSAT[nSSID++] = this.readInt ();
177 thisSID = this.SAT[thisSID];
180 if (Clazz.exceptionOf (e, Exception)) {
181 System.out.println (e.toString ());
187 Clazz.defineMethod (c$, "getDirectoryTable",
189 var thisSID = this.header.SID_DIR_start;
191 this.rootEntry = null;
193 while (thisSID > 0) {
194 this.gotoSector (thisSID);
195 for (var j = this.nDirEntriesperSector; --j >= 0; ) {
196 thisEntry = new JU.CompoundDocDirEntry (this);
197 thisEntry.readData ();
198 if (thisEntry.lenStream > 0) {
199 this.directory.addLast (thisEntry);
200 }if (thisEntry.entryType == 5) this.rootEntry = thisEntry;
202 thisSID = this.SAT[thisSID];
205 if (Clazz.exceptionOf (e, Exception)) {
206 System.out.println (e.toString ());
212 Clazz.defineMethod (c$, "getEntryAsString",
213 function (thisEntry, asBinaryString) {
214 if (thisEntry.isEmpty) return new JU.SB ();
215 return (thisEntry.isStandard ? this.getStandardStringData (thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString) : this.getShortStringData (thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString));
216 }, "JU.CompoundDocDirEntry,~B");
217 Clazz.defineMethod (c$, "getStandardStringData",
218 function (thisSID, nBytes, asBinaryString) {
219 var data = new JU.SB ();
220 var byteBuf = Clazz.newByteArray (this.sectorSize, 0);
221 var gzipData = new JU.ZipData (nBytes);
223 while (thisSID > 0 && nBytes > 0) {
224 this.gotoSector (thisSID);
225 nBytes = this.getSectorData (data, byteBuf, this.sectorSize, nBytes, asBinaryString, gzipData);
226 thisSID = this.SAT[thisSID];
228 if (nBytes == -9999) return new JU.SB ();
230 if (Clazz.exceptionOf (e, Exception)) {
231 System.out.println (e.toString ());
236 if (gzipData.isEnabled) gzipData.addTo (this.jzt, data);
239 Clazz.defineMethod (c$, "getSectorData",
240 function (data, byteBuf, nSectorBytes, nBytes, asBinaryString, gzipData) {
241 this.readByteArray (byteBuf, 0, byteBuf.length);
242 var n = gzipData.addBytes (byteBuf, nSectorBytes, nBytes);
243 if (n >= 0) return n;
244 if (asBinaryString) {
245 for (var i = 0; i < nSectorBytes; i++) {
246 data.append (Integer.toHexString (byteBuf[i] & 0xFF)).appendC (' ');
247 if (--nBytes < 1) break;
250 for (var i = 0; i < nSectorBytes; i++) {
251 if (byteBuf[i] == 0) return -9999;
252 data.appendC (String.fromCharCode (byteBuf[i]));
253 if (--nBytes < 1) break;
256 }, "JU.SB,~A,~N,~N,~B,JU.ZipData");
257 Clazz.defineMethod (c$, "getShortStringData",
258 function (shortSID, nBytes, asBinaryString) {
259 var data = new JU.SB ();
260 if (this.rootEntry == null) return data;
261 var thisSID = this.rootEntry.SIDfirstSector;
263 var byteBuf = Clazz.newByteArray (this.shortSectorSize, 0);
264 var gzipData = new JU.ZipData (nBytes);
266 while (thisSID >= 0 && shortSID >= 0 && nBytes > 0) {
267 while (shortSID - ptShort >= this.nShortSectorsPerStandardSector) {
268 ptShort += this.nShortSectorsPerStandardSector;
269 thisSID = this.SAT[thisSID];
271 this.seek (this.getOffset (thisSID) + (shortSID - ptShort) * this.shortSectorSize);
272 nBytes = this.getSectorData (data, byteBuf, this.shortSectorSize, nBytes, asBinaryString, gzipData);
273 shortSID = this.SSAT[shortSID];
276 if (Clazz.exceptionOf (e, Exception)) {
277 System.out.println (data.toString ());
278 System.out.println ("reader error in CompoundDocument " + e.toString ());
283 if (gzipData.isEnabled) gzipData.addTo (this.jzt, data);