JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / javajs / util / BinaryDocument.js
1 Clazz.declarePackage ("javajs.util");\r
2 Clazz.load (["javajs.api.GenericBinaryDocument", "javajs.util.BC"], "javajs.util.BinaryDocument", ["java.io.DataInputStream", "java.lang.Double"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.stream = null;\r
5 this.isRandom = false;\r
6 this.isBigEndian = true;\r
7 this.jzt = null;\r
8 this.t8 = null;\r
9 this.nBytes = 0;\r
10 this.out = null;\r
11 Clazz.instantialize (this, arguments);\r
12 }, javajs.util, "BinaryDocument", javajs.util.BC, javajs.api.GenericBinaryDocument);\r
13 Clazz.prepareFields (c$, function () {\r
14 this.t8 =  Clazz.newByteArray (8, 0);\r
15 });\r
16 Clazz.overrideMethod (c$, "close", \r
17 function () {\r
18 if (this.stream != null) try {\r
19 this.stream.close ();\r
20 } catch (e) {\r
21 if (Clazz.exceptionOf (e, Exception)) {\r
22 } else {\r
23 throw e;\r
24 }\r
25 }\r
26 if (this.out != null) this.out.closeChannel ();\r
27 });\r
28 Clazz.overrideMethod (c$, "setStream", \r
29 function (jzt, bis, isBigEndian) {\r
30 if (jzt != null) this.jzt = jzt;\r
31 if (bis != null) this.stream =  new java.io.DataInputStream (bis);\r
32 this.isBigEndian = isBigEndian;\r
33 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~B");\r
34 Clazz.overrideMethod (c$, "setStreamData", \r
35 function (stream, isBigEndian) {\r
36 if (stream != null) this.stream = stream;\r
37 this.isBigEndian = isBigEndian;\r
38 }, "java.io.DataInputStream,~B");\r
39 Clazz.defineMethod (c$, "setRandom", \r
40 function (TF) {\r
41 this.isRandom = TF;\r
42 }, "~B");\r
43 Clazz.overrideMethod (c$, "readByte", \r
44 function () {\r
45 this.nBytes++;\r
46 return this.ioReadByte ();\r
47 });\r
48 Clazz.defineMethod (c$, "ioReadByte", \r
49 ($fz = function () {\r
50 var b = this.stream.readByte ();\r
51 if (this.out != null) this.out.writeByteAsInt (b);\r
52 return b;\r
53 }, $fz.isPrivate = true, $fz));\r
54 Clazz.overrideMethod (c$, "readByteArray", \r
55 function (b, off, len) {\r
56 var n = this.ioRead (b, off, len);\r
57 this.nBytes += n;\r
58 return n;\r
59 }, "~A,~N,~N");\r
60 Clazz.defineMethod (c$, "ioRead", \r
61 ($fz = function (b, off, len) {\r
62 var m = 0;\r
63 while (len > 0) {\r
64 var n = this.stream.read (b, off, len);\r
65 m += n;\r
66 if (n > 0 && this.out != null) this.writeBytes (b, off, n);\r
67 if (n >= len) break;\r
68 off += n;\r
69 len -= n;\r
70 }\r
71 return m;\r
72 }, $fz.isPrivate = true, $fz), "~A,~N,~N");\r
73 Clazz.defineMethod (c$, "writeBytes", \r
74 function (b, off, n) {\r
75 this.out.write (b, off, n);\r
76 }, "~A,~N,~N");\r
77 Clazz.overrideMethod (c$, "readString", \r
78 function (nChar) {\r
79 var temp =  Clazz.newByteArray (nChar, 0);\r
80 var n = this.readByteArray (temp, 0, nChar);\r
81 return  String.instantialize (temp, 0, n, "UTF-8");\r
82 }, "~N");\r
83 Clazz.overrideMethod (c$, "readShort", \r
84 function () {\r
85 this.nBytes += 2;\r
86 var n = (this.isBigEndian ? this.ioReadShort () : ((this.ioReadByte () & 0xff) | (this.ioReadByte () & 0xff) << 8));\r
87 {\r
88 return (n > 0x7FFF ? n - 0x10000 : n);\r
89 }});\r
90 Clazz.defineMethod (c$, "ioReadShort", \r
91 ($fz = function () {\r
92 var b = this.stream.readShort ();\r
93 if (this.out != null) this.writeShort (b);\r
94 return b;\r
95 }, $fz.isPrivate = true, $fz));\r
96 Clazz.defineMethod (c$, "writeShort", \r
97 function (i) {\r
98 this.out.writeByteAsInt (i >> 8);\r
99 this.out.writeByteAsInt (i);\r
100 }, "~N");\r
101 Clazz.overrideMethod (c$, "readIntLE", \r
102 function () {\r
103 this.nBytes += 4;\r
104 return this.readLEInt ();\r
105 });\r
106 Clazz.overrideMethod (c$, "readInt", \r
107 function () {\r
108 this.nBytes += 4;\r
109 return (this.isBigEndian ? this.ioReadInt () : this.readLEInt ());\r
110 });\r
111 Clazz.defineMethod (c$, "ioReadInt", \r
112 ($fz = function () {\r
113 var i = this.stream.readInt ();\r
114 if (this.out != null) this.writeInt (i);\r
115 return i;\r
116 }, $fz.isPrivate = true, $fz));\r
117 Clazz.defineMethod (c$, "writeInt", \r
118 function (i) {\r
119 this.out.writeByteAsInt (i >> 24);\r
120 this.out.writeByteAsInt (i >> 16);\r
121 this.out.writeByteAsInt (i >> 8);\r
122 this.out.writeByteAsInt (i);\r
123 }, "~N");\r
124 Clazz.overrideMethod (c$, "swapBytesI", \r
125 function (n) {\r
126 return (((n >> 24) & 0xff) | ((n >> 16) & 0xff) << 8 | ((n >> 8) & 0xff) << 16 | (n & 0xff) << 24);\r
127 }, "~N");\r
128 Clazz.overrideMethod (c$, "swapBytesS", \r
129 function (n) {\r
130 return ((((n >> 8) & 0xff) | (n & 0xff) << 8));\r
131 }, "~N");\r
132 Clazz.overrideMethod (c$, "readUnsignedShort", \r
133 function () {\r
134 this.nBytes += 2;\r
135 var a = (this.ioReadByte () & 0xff);\r
136 var b = (this.ioReadByte () & 0xff);\r
137 return (this.isBigEndian ? (a << 8) + b : (b << 8) + a);\r
138 });\r
139 Clazz.overrideMethod (c$, "readLong", \r
140 function () {\r
141 this.nBytes += 8;\r
142 return (this.isBigEndian ? this.ioReadLong () : (((this.ioReadByte ()) & 0xff) | ((this.ioReadByte ()) & 0xff) << 8 | ((this.ioReadByte ()) & 0xff) << 16 | ((this.ioReadByte ()) & 0xff) << 24 | ((this.ioReadByte ()) & 0xff) << 32 | ((this.ioReadByte ()) & 0xff) << 40 | ((this.ioReadByte ()) & 0xff) << 48 | ((this.ioReadByte ()) & 0xff) << 54));\r
143 });\r
144 Clazz.defineMethod (c$, "ioReadLong", \r
145 ($fz = function () {\r
146 var b = this.stream.readLong ();\r
147 if (this.out != null) this.writeLong (b);\r
148 return b;\r
149 }, $fz.isPrivate = true, $fz));\r
150 Clazz.defineMethod (c$, "writeLong", \r
151 function (b) {\r
152 this.writeInt (((b >> 32) & 0xFFFFFFFF));\r
153 this.writeInt ((b & 0xFFFFFFFF));\r
154 }, "~N");\r
155 Clazz.defineMethod (c$, "readLEInt", \r
156 ($fz = function () {\r
157 this.ioRead (this.t8, 0, 4);\r
158 return javajs.util.BC.bytesToInt (this.t8, 0, false);\r
159 }, $fz.isPrivate = true, $fz));\r
160 Clazz.overrideMethod (c$, "readFloat", \r
161 function () {\r
162 return javajs.util.BC.intToFloat (this.readInt ());\r
163 });\r
164 Clazz.overrideMethod (c$, "readDouble", \r
165 function () {\r
166 {\r
167 this.readByteArray(this.t8, 0, 8);\r
168 return this.bytesToDoubleToFloat(this.t8, 0, this.isBigEndian);\r
169 }});\r
170 Clazz.defineMethod (c$, "ioReadDouble", \r
171 ($fz = function () {\r
172 var d = this.stream.readDouble ();\r
173 if (this.out != null) this.writeLong (Double.doubleToRawLongBits (d));\r
174 return d;\r
175 }, $fz.isPrivate = true, $fz));\r
176 Clazz.defineMethod (c$, "readLELong", \r
177 ($fz = function () {\r
178 return (((this.ioReadByte ()) & 0xff) | ((this.ioReadByte ()) & 0xff) << 8 | ((this.ioReadByte ()) & 0xff) << 16 | ((this.ioReadByte ()) & 0xff) << 24 | ((this.ioReadByte ()) & 0xff) << 32 | ((this.ioReadByte ()) & 0xff) << 40 | ((this.ioReadByte ()) & 0xff) << 48 | ((this.ioReadByte ()) & 0xff) << 56);\r
179 }, $fz.isPrivate = true, $fz));\r
180 Clazz.overrideMethod (c$, "seek", \r
181 function (offset) {\r
182 try {\r
183 if (offset == this.nBytes) return;\r
184 if (offset < this.nBytes) {\r
185 this.stream.reset ();\r
186 if (this.out != null && this.nBytes != 0) this.out.reset ();\r
187 this.nBytes = 0;\r
188 } else {\r
189 offset -= this.nBytes;\r
190 }if (this.out == null) {\r
191 this.stream.skipBytes (offset);\r
192 } else {\r
193 this.readByteArray ( Clazz.newByteArray (offset, 0), 0, offset);\r
194 }this.nBytes += offset;\r
195 } catch (e) {\r
196 if (Clazz.exceptionOf (e, Exception)) {\r
197 System.out.println (e.toString ());\r
198 } else {\r
199 throw e;\r
200 }\r
201 }\r
202 }, "~N");\r
203 Clazz.overrideMethod (c$, "getPosition", \r
204 function () {\r
205 return this.nBytes;\r
206 });\r
207 Clazz.overrideMethod (c$, "setOutputChannel", \r
208 function (out) {\r
209 this.out = out;\r
210 }, "javajs.util.OC");\r
211 Clazz.overrideMethod (c$, "getAllDataFiles", \r
212 function (binaryFileList, firstFile) {\r
213 return null;\r
214 }, "~S,~S");\r
215 Clazz.overrideMethod (c$, "getAllDataMapped", \r
216 function (replace, string, fileData) {\r
217 }, "~S,~S,java.util.Map");\r
218 });\r