JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / javajs / util / CompoundDocument.js
1 Clazz.declarePackage ("javajs.util");\r
2 Clazz.load (["javajs.util.BinaryDocument", "$.CompoundDocHeader", "$.Lst"], "javajs.util.CompoundDocument", ["java.io.DataInputStream", "javajs.util.CompoundDocDirEntry", "$.SB", "$.ZipData"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.header = null;\r
5 this.directory = null;\r
6 this.rootEntry = null;\r
7 this.SAT = null;\r
8 this.SSAT = null;\r
9 this.sectorSize = 0;\r
10 this.shortSectorSize = 0;\r
11 this.nShortSectorsPerStandardSector = 0;\r
12 this.nIntPerSector = 0;\r
13 this.nDirEntriesperSector = 0;\r
14 this.data = null;\r
15 Clazz.instantialize (this, arguments);\r
16 }, javajs.util, "CompoundDocument", javajs.util.BinaryDocument);\r
17 Clazz.prepareFields (c$, function () {\r
18 this.header =  new javajs.util.CompoundDocHeader (this);\r
19 this.directory =  new javajs.util.Lst ();\r
20 });\r
21 Clazz.makeConstructor (c$, \r
22 function () {\r
23 Clazz.superConstructor (this, javajs.util.CompoundDocument);\r
24 this.isBigEndian = true;\r
25 });\r
26 Clazz.overrideMethod (c$, "setStream", \r
27 function (jzt, bis, isBigEndian) {\r
28 this.jzt = jzt;\r
29 if (!this.isRandom) {\r
30 this.stream =  new java.io.DataInputStream (bis);\r
31 }this.stream.mark (2147483647);\r
32 if (!this.readHeader ()) return;\r
33 this.getSectorAllocationTable ();\r
34 this.getShortSectorAllocationTable ();\r
35 this.getDirectoryTable ();\r
36 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~B");\r
37 Clazz.defineMethod (c$, "getDirectory", \r
38 function () {\r
39 return this.directory;\r
40 });\r
41 Clazz.defineMethod (c$, "getDirectoryListing", \r
42 function (separator) {\r
43 var str = "";\r
44 for (var i = 0; i < this.directory.size (); i++) {\r
45 var thisEntry = this.directory.get (i);\r
46 if (!thisEntry.isEmpty) str += separator + thisEntry.entryName + "\tlen=" + thisEntry.lenStream + "\tSID=" + thisEntry.SIDfirstSector + (thisEntry.isStandard ? "\tfileOffset=" + this.getOffset (thisEntry.SIDfirstSector) : "");\r
47 }\r
48 return str;\r
49 }, "~S");\r
50 Clazz.defineMethod (c$, "getAllData", \r
51 function () {\r
52 return this.getAllDataFiles (null, null);\r
53 });\r
54 Clazz.overrideMethod (c$, "getAllDataMapped", \r
55 function (prefix, binaryFileList, fileData) {\r
56 fileData.put ("#Directory_Listing", this.getDirectoryListing ("|"));\r
57 binaryFileList = "|" + binaryFileList + "|";\r
58 for (var i = 0; i < this.directory.size (); i++) {\r
59 var thisEntry = this.directory.get (i);\r
60 if (!thisEntry.isEmpty && thisEntry.entryType != 5) {\r
61 var name = thisEntry.entryName;\r
62 System.out.println ("CompoundDocument file " + name);\r
63 var isBinary = (binaryFileList.indexOf ("|" + name + "|") >= 0);\r
64 if (isBinary) name += ":asBinaryString";\r
65 var data =  new javajs.util.SB ();\r
66 data.append ("BEGIN Directory Entry ").append (name).append ("\n");\r
67 data.appendSB (this.getEntryAsString (thisEntry, isBinary));\r
68 data.append ("\nEND Directory Entry ").append (name).append ("\n");\r
69 fileData.put (prefix + "/" + name, data.toString ());\r
70 }}\r
71 this.close ();\r
72 }, "~S,~S,java.util.Map");\r
73 Clazz.overrideMethod (c$, "getAllDataFiles", \r
74 function (binaryFileList, firstFile) {\r
75 if (firstFile != null) {\r
76 for (var i = 0; i < this.directory.size (); i++) {\r
77 var thisEntry = this.directory.get (i);\r
78 if (thisEntry.entryName.equals (firstFile)) {\r
79 this.directory.remove (i);\r
80 this.directory.add (1, thisEntry);\r
81 break;\r
82 }}\r
83 }this.data =  new javajs.util.SB ();\r
84 this.data.append ("Compound Document File Directory: ");\r
85 this.data.append (this.getDirectoryListing ("|"));\r
86 this.data.append ("\n");\r
87 binaryFileList = "|" + binaryFileList + "|";\r
88 for (var i = 0; i < this.directory.size (); i++) {\r
89 var thisEntry = this.directory.get (i);\r
90 if (!thisEntry.isEmpty && thisEntry.entryType != 5) {\r
91 var name = thisEntry.entryName;\r
92 if (name.endsWith (".gz")) name = name.substring (0, name.length - 3);\r
93 this.data.append ("BEGIN Directory Entry ").append (name).append ("\n");\r
94 this.data.appendSB (this.getEntryAsString (thisEntry, binaryFileList.indexOf ("|" + thisEntry.entryName + "|") >= 0));\r
95 this.data.append ("\n");\r
96 this.data.append ("END Directory Entry ").append (thisEntry.entryName).append ("\n");\r
97 }}\r
98 this.close ();\r
99 return this.data;\r
100 }, "~S,~S");\r
101 Clazz.defineMethod (c$, "getFileAsString", \r
102 function (entryName) {\r
103 for (var i = 0; i < this.directory.size (); i++) {\r
104 var thisEntry = this.directory.get (i);\r
105 if (thisEntry.entryName.equals (entryName)) return this.getEntryAsString (thisEntry, false);\r
106 }\r
107 return  new javajs.util.SB ();\r
108 }, "~S");\r
109 Clazz.defineMethod (c$, "getOffset", \r
110 ($fz = function (SID) {\r
111 return (SID + 1) * this.sectorSize;\r
112 }, $fz.isPrivate = true, $fz), "~N");\r
113 Clazz.defineMethod (c$, "gotoSector", \r
114 ($fz = function (SID) {\r
115 this.seek (this.getOffset (SID));\r
116 }, $fz.isPrivate = true, $fz), "~N");\r
117 Clazz.defineMethod (c$, "readHeader", \r
118 ($fz = function () {\r
119 if (!this.header.readData ()) return false;\r
120 this.sectorSize = 1 << this.header.sectorPower;\r
121 this.shortSectorSize = 1 << this.header.shortSectorPower;\r
122 this.nShortSectorsPerStandardSector = Clazz.doubleToInt (this.sectorSize / this.shortSectorSize);\r
123 this.nIntPerSector = Clazz.doubleToInt (this.sectorSize / 4);\r
124 this.nDirEntriesperSector = Clazz.doubleToInt (this.sectorSize / 128);\r
125 return true;\r
126 }, $fz.isPrivate = true, $fz));\r
127 Clazz.defineMethod (c$, "getSectorAllocationTable", \r
128 ($fz = function () {\r
129 var nSID = 0;\r
130 var thisSID;\r
131 this.SAT =  Clazz.newIntArray (this.header.nSATsectors * this.nIntPerSector + 109, 0);\r
132 try {\r
133 for (var i = 0; i < 109; i++) {\r
134 thisSID = this.header.MSAT0[i];\r
135 if (thisSID < 0) break;\r
136 this.gotoSector (thisSID);\r
137 for (var j = 0; j < this.nIntPerSector; j++) {\r
138 this.SAT[nSID++] = this.readInt ();\r
139 }\r
140 }\r
141 var nMaster = this.header.nAdditionalMATsectors;\r
142 thisSID = this.header.SID_MSAT_next;\r
143 var MSAT =  Clazz.newIntArray (this.nIntPerSector, 0);\r
144 out : while (nMaster-- > 0 && thisSID >= 0) {\r
145 this.gotoSector (thisSID);\r
146 for (var i = 0; i < this.nIntPerSector; i++) MSAT[i] = this.readInt ();\r
147 \r
148 for (var i = 0; i < this.nIntPerSector - 1; i++) {\r
149 thisSID = MSAT[i];\r
150 if (thisSID < 0) break out;\r
151 this.gotoSector (thisSID);\r
152 for (var j = this.nIntPerSector; --j >= 0; ) this.SAT[nSID++] = this.readInt ();\r
153 \r
154 }\r
155 thisSID = MSAT[this.nIntPerSector - 1];\r
156 }\r
157 } catch (e) {\r
158 if (Clazz.exceptionOf (e, Exception)) {\r
159 System.out.println (e.toString ());\r
160 } else {\r
161 throw e;\r
162 }\r
163 }\r
164 }, $fz.isPrivate = true, $fz));\r
165 Clazz.defineMethod (c$, "getShortSectorAllocationTable", \r
166 ($fz = function () {\r
167 var nSSID = 0;\r
168 var thisSID = this.header.SID_SSAT_start;\r
169 var nMax = this.header.nSSATsectors * this.nIntPerSector;\r
170 this.SSAT =  Clazz.newIntArray (nMax, 0);\r
171 try {\r
172 while (thisSID > 0 && nSSID < nMax) {\r
173 this.gotoSector (thisSID);\r
174 for (var j = 0; j < this.nIntPerSector; j++) {\r
175 this.SSAT[nSSID++] = this.readInt ();\r
176 }\r
177 thisSID = this.SAT[thisSID];\r
178 }\r
179 } catch (e) {\r
180 if (Clazz.exceptionOf (e, Exception)) {\r
181 System.out.println (e.toString ());\r
182 } else {\r
183 throw e;\r
184 }\r
185 }\r
186 }, $fz.isPrivate = true, $fz));\r
187 Clazz.defineMethod (c$, "getDirectoryTable", \r
188 ($fz = function () {\r
189 var thisSID = this.header.SID_DIR_start;\r
190 var thisEntry;\r
191 this.rootEntry = null;\r
192 try {\r
193 while (thisSID > 0) {\r
194 this.gotoSector (thisSID);\r
195 for (var j = this.nDirEntriesperSector; --j >= 0; ) {\r
196 thisEntry =  new javajs.util.CompoundDocDirEntry (this);\r
197 thisEntry.readData ();\r
198 if (thisEntry.lenStream > 0) {\r
199 this.directory.addLast (thisEntry);\r
200 }if (thisEntry.entryType == 5) this.rootEntry = thisEntry;\r
201 }\r
202 thisSID = this.SAT[thisSID];\r
203 }\r
204 } catch (e) {\r
205 if (Clazz.exceptionOf (e, Exception)) {\r
206 System.out.println (e.toString ());\r
207 } else {\r
208 throw e;\r
209 }\r
210 }\r
211 }, $fz.isPrivate = true, $fz));\r
212 Clazz.defineMethod (c$, "getEntryAsString", \r
213 ($fz = function (thisEntry, asBinaryString) {\r
214 if (thisEntry.isEmpty) return  new javajs.util.SB ();\r
215 return (thisEntry.isStandard ? this.getStandardStringData (thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString) : this.getShortStringData (thisEntry.SIDfirstSector, thisEntry.lenStream, asBinaryString));\r
216 }, $fz.isPrivate = true, $fz), "javajs.util.CompoundDocDirEntry,~B");\r
217 Clazz.defineMethod (c$, "getStandardStringData", \r
218 ($fz = function (thisSID, nBytes, asBinaryString) {\r
219 var data =  new javajs.util.SB ();\r
220 var byteBuf =  Clazz.newByteArray (this.sectorSize, 0);\r
221 var gzipData =  new javajs.util.ZipData (nBytes);\r
222 try {\r
223 while (thisSID > 0 && nBytes > 0) {\r
224 this.gotoSector (thisSID);\r
225 nBytes = this.getSectorData (data, byteBuf, this.sectorSize, nBytes, asBinaryString, gzipData);\r
226 thisSID = this.SAT[thisSID];\r
227 }\r
228 if (nBytes == -9999) return  new javajs.util.SB ();\r
229 } catch (e) {\r
230 if (Clazz.exceptionOf (e, Exception)) {\r
231 System.out.println (e.toString ());\r
232 } else {\r
233 throw e;\r
234 }\r
235 }\r
236 if (gzipData.isEnabled) gzipData.addTo (this.jzt, data);\r
237 return data;\r
238 }, $fz.isPrivate = true, $fz), "~N,~N,~B");\r
239 Clazz.defineMethod (c$, "getSectorData", \r
240 ($fz = function (data, byteBuf, nSectorBytes, nBytes, asBinaryString, gzipData) {\r
241 this.readByteArray (byteBuf, 0, byteBuf.length);\r
242 var n = gzipData.addBytes (byteBuf, nSectorBytes, nBytes);\r
243 if (n >= 0) return n;\r
244 if (asBinaryString) {\r
245 for (var i = 0; i < nSectorBytes; i++) {\r
246 data.append (Integer.toHexString (byteBuf[i] & 0xFF)).appendC (' ');\r
247 if (--nBytes < 1) break;\r
248 }\r
249 } else {\r
250 for (var i = 0; i < nSectorBytes; i++) {\r
251 if (byteBuf[i] == 0) return -9999;\r
252 data.appendC (String.fromCharCode (byteBuf[i]));\r
253 if (--nBytes < 1) break;\r
254 }\r
255 }return nBytes;\r
256 }, $fz.isPrivate = true, $fz), "javajs.util.SB,~A,~N,~N,~B,javajs.util.ZipData");\r
257 Clazz.defineMethod (c$, "getShortStringData", \r
258 ($fz = function (shortSID, nBytes, asBinaryString) {\r
259 var data =  new javajs.util.SB ();\r
260 if (this.rootEntry == null) return data;\r
261 var thisSID = this.rootEntry.SIDfirstSector;\r
262 var ptShort = 0;\r
263 var byteBuf =  Clazz.newByteArray (this.shortSectorSize, 0);\r
264 var gzipData =  new javajs.util.ZipData (nBytes);\r
265 try {\r
266 while (thisSID >= 0 && shortSID >= 0 && nBytes > 0) {\r
267 while (shortSID - ptShort >= this.nShortSectorsPerStandardSector) {\r
268 ptShort += this.nShortSectorsPerStandardSector;\r
269 thisSID = this.SAT[thisSID];\r
270 }\r
271 this.seek (this.getOffset (thisSID) + (shortSID - ptShort) * this.shortSectorSize);\r
272 nBytes = this.getSectorData (data, byteBuf, this.shortSectorSize, nBytes, asBinaryString, gzipData);\r
273 shortSID = this.SSAT[shortSID];\r
274 }\r
275 } catch (e) {\r
276 if (Clazz.exceptionOf (e, Exception)) {\r
277 System.out.println (data.toString ());\r
278 System.out.println ("reader error in CompoundDocument " + e.toString ());\r
279 } else {\r
280 throw e;\r
281 }\r
282 }\r
283 if (gzipData.isEnabled) gzipData.addTo (this.jzt, data);\r
284 return data;\r
285 }, $fz.isPrivate = true, $fz), "~N,~N,~B");\r
286 });\r