Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / j2s / JU / Rdr.js
1 Clazz.declarePackage ("JU");
2 Clazz.load (["javajs.api.GenericLineReader"], "JU.Rdr", ["java.io.BufferedInputStream", "$.BufferedReader", "$.ByteArrayInputStream", "$.InputStreamReader", "$.StringReader", "JU.AU", "$.Base64", "$.Encoding", "$.SB"], function () {
3 c$ = Clazz.decorateAsClass (function () {
4 this.reader = null;
5 Clazz.instantialize (this, arguments);
6 }, JU, "Rdr", null, javajs.api.GenericLineReader);
7 Clazz.makeConstructor (c$, 
8 function (reader) {
9 this.reader = reader;
10 }, "java.io.BufferedReader");
11 Clazz.overrideMethod (c$, "readNextLine", 
12 function () {
13 return this.reader.readLine ();
14 });
15 c$.readCifData = Clazz.defineMethod (c$, "readCifData", 
16 function (parser, br) {
17 return parser.set (null, br).getAllCifData ();
18 }, "javajs.api.GenericCifDataParser,java.io.BufferedReader");
19 c$.fixUTF = Clazz.defineMethod (c$, "fixUTF", 
20 function (bytes) {
21 var encoding = JU.Rdr.getUTFEncoding (bytes);
22 if (encoding !== JU.Encoding.NONE) try {
23 var s =  String.instantialize (bytes, encoding.name ().$replace ('_', '-'));
24 switch (encoding) {
25 case JU.Encoding.UTF8:
26 case JU.Encoding.UTF_16BE:
27 case JU.Encoding.UTF_16LE:
28 s = s.substring (1);
29 break;
30 default:
31 break;
32 }
33 return s;
34 } catch (e) {
35 if (Clazz.exceptionOf (e, java.io.UnsupportedEncodingException)) {
36 System.out.println (e);
37 } else {
38 throw e;
39 }
40 }
41 return  String.instantialize (bytes);
42 }, "~A");
43 c$.getUTFEncoding = Clazz.defineMethod (c$, "getUTFEncoding", 
44  function (bytes) {
45 if (bytes.length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) return JU.Encoding.UTF8;
46 if (bytes.length >= 4 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 0xFE && bytes[3] == 0xFF) return JU.Encoding.UTF_32BE;
47 if (bytes.length >= 4 && bytes[0] == 0xFF && bytes[1] == 0xFE && bytes[2] == 0 && bytes[3] == 0) return JU.Encoding.UTF_32LE;
48 if (bytes.length >= 2 && bytes[0] == 0xFF && bytes[1] == 0xFE) return JU.Encoding.UTF_16LE;
49 if (bytes.length >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) return JU.Encoding.UTF_16BE;
50 return JU.Encoding.NONE;
51 }, "~A");
52 c$.getUTFEncodingForStream = Clazz.defineMethod (c$, "getUTFEncodingForStream", 
53  function (is) {
54 {
55 is.resetStream();
56 }var abMagic =  Clazz.newByteArray (4, 0);
57 abMagic[3] = 1;
58 try {
59 is.mark (5);
60 } catch (e) {
61 if (Clazz.exceptionOf (e, Exception)) {
62 return JU.Encoding.NONE;
63 } else {
64 throw e;
65 }
66 }
67 is.read (abMagic, 0, 4);
68 is.reset ();
69 return JU.Rdr.getUTFEncoding (abMagic);
70 }, "java.io.BufferedInputStream");
71 c$.isBase64 = Clazz.defineMethod (c$, "isBase64", 
72 function (sb) {
73 return (sb.indexOf (";base64,") == 0);
74 }, "JU.SB");
75 c$.isCompoundDocumentS = Clazz.defineMethod (c$, "isCompoundDocumentS", 
76 function (is) {
77 return JU.Rdr.isCompoundDocumentB (JU.Rdr.getMagic (is, 8));
78 }, "java.io.InputStream");
79 c$.isCompoundDocumentB = Clazz.defineMethod (c$, "isCompoundDocumentB", 
80 function (bytes) {
81 return (bytes.length >= 8 && bytes[0] == 0xD0 && bytes[1] == 0xCF && bytes[2] == 0x11 && bytes[3] == 0xE0 && bytes[4] == 0xA1 && bytes[5] == 0xB1 && bytes[6] == 0x1A && bytes[7] == 0xE1);
82 }, "~A");
83 c$.isGzipS = Clazz.defineMethod (c$, "isGzipS", 
84 function (is) {
85 return JU.Rdr.isGzipB (JU.Rdr.getMagic (is, 2));
86 }, "java.io.InputStream");
87 c$.isGzipB = Clazz.defineMethod (c$, "isGzipB", 
88 function (bytes) {
89 return (bytes != null && bytes.length >= 2 && bytes[0] == 0x1F && bytes[1] == 0x8B);
90 }, "~A");
91 c$.isPickleS = Clazz.defineMethod (c$, "isPickleS", 
92 function (is) {
93 return JU.Rdr.isPickleB (JU.Rdr.getMagic (is, 2));
94 }, "java.io.InputStream");
95 c$.isPickleB = Clazz.defineMethod (c$, "isPickleB", 
96 function (bytes) {
97 return (bytes != null && bytes.length >= 2 && bytes[0] == 0x7D && bytes[1] == 0x71);
98 }, "~A");
99 c$.isPngZipStream = Clazz.defineMethod (c$, "isPngZipStream", 
100 function (is) {
101 return JU.Rdr.isPngZipB (JU.Rdr.getMagic (is, 55));
102 }, "java.io.InputStream");
103 c$.isPngZipB = Clazz.defineMethod (c$, "isPngZipB", 
104 function (bytes) {
105 return (bytes[50] == 0 && bytes[51] == 0x50 && bytes[52] == 0x4E && bytes[53] == 0x47 && bytes[54] == 0x4A);
106 }, "~A");
107 c$.isZipS = Clazz.defineMethod (c$, "isZipS", 
108 function (is) {
109 return JU.Rdr.isZipB (JU.Rdr.getMagic (is, 4));
110 }, "java.io.InputStream");
111 c$.isZipB = Clazz.defineMethod (c$, "isZipB", 
112 function (bytes) {
113 return (bytes.length >= 4 && bytes[0] == 0x50 && bytes[1] == 0x4B && bytes[2] == 0x03 && bytes[3] == 0x04);
114 }, "~A");
115 c$.getMagic = Clazz.defineMethod (c$, "getMagic", 
116  function (is, n) {
117 var abMagic =  Clazz.newByteArray (n, 0);
118 {
119 is.resetStream();
120 }try {
121 is.mark (n + 1);
122 is.read (abMagic, 0, n);
123 } catch (e) {
124 if (Clazz.exceptionOf (e, java.io.IOException)) {
125 } else {
126 throw e;
127 }
128 }
129 try {
130 is.reset ();
131 } catch (e) {
132 if (Clazz.exceptionOf (e, java.io.IOException)) {
133 } else {
134 throw e;
135 }
136 }
137 return abMagic;
138 }, "java.io.InputStream,~N");
139 c$.getBIS = Clazz.defineMethod (c$, "getBIS", 
140 function (bytes) {
141 return  new java.io.BufferedInputStream ( new java.io.ByteArrayInputStream (bytes));
142 }, "~A");
143 c$.getBR = Clazz.defineMethod (c$, "getBR", 
144 function (string) {
145 return  new java.io.BufferedReader ( new java.io.StringReader (string));
146 }, "~S");
147 c$.getUnzippedInputStream = Clazz.defineMethod (c$, "getUnzippedInputStream", 
148 function (jzt, bis) {
149 while (JU.Rdr.isGzipS (bis)) bis =  new java.io.BufferedInputStream (JU.Rdr.newGZIPInputStream (jzt, bis));
150
151 return bis;
152 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream");
153 c$.getBytesFromSB = Clazz.defineMethod (c$, "getBytesFromSB", 
154 function (sb) {
155 return (JU.Rdr.isBase64 (sb) ? JU.Base64.decodeBase64 (sb.substring (8)) : sb.toBytes (0, -1));
156 }, "JU.SB");
157 c$.getStreamAsBytes = Clazz.defineMethod (c$, "getStreamAsBytes", 
158 function (bis, out) {
159 var buf =  Clazz.newByteArray (1024, 0);
160 var bytes = (out == null ?  Clazz.newByteArray (4096, 0) : null);
161 var len = 0;
162 var totalLen = 0;
163 while ((len = bis.read (buf, 0, 1024)) > 0) {
164 totalLen += len;
165 if (out == null) {
166 if (totalLen >= bytes.length) bytes = JU.AU.ensureLengthByte (bytes, totalLen * 2);
167 System.arraycopy (buf, 0, bytes, totalLen - len, len);
168 } else {
169 out.write (buf, 0, len);
170 }}
171 bis.close ();
172 if (out == null) {
173 return JU.AU.arrayCopyByte (bytes, totalLen);
174 }return totalLen + " bytes";
175 }, "java.io.BufferedInputStream,JU.OC");
176 c$.getBufferedReader = Clazz.defineMethod (c$, "getBufferedReader", 
177 function (bis, charSet) {
178 if (JU.Rdr.getUTFEncodingForStream (bis) === JU.Encoding.NONE) return  new java.io.BufferedReader ( new java.io.InputStreamReader (bis, (charSet == null ? "UTF-8" : charSet)));
179 var bytes = JU.Rdr.getLimitedStreamBytes (bis, -1);
180 bis.close ();
181 return JU.Rdr.getBR (charSet == null ? JU.Rdr.fixUTF (bytes) :  String.instantialize (bytes, charSet));
182 }, "java.io.BufferedInputStream,~S");
183 c$.getLimitedStreamBytes = Clazz.defineMethod (c$, "getLimitedStreamBytes", 
184 function (is, n) {
185 var buflen = (n > 0 && n < 1024 ? n : 1024);
186 var buf =  Clazz.newByteArray (buflen, 0);
187 var bytes =  Clazz.newByteArray (n < 0 ? 4096 : n, 0);
188 var len = 0;
189 var totalLen = 0;
190 if (n < 0) n = 2147483647;
191 while (totalLen < n && (len = is.read (buf, 0, buflen)) > 0) {
192 totalLen += len;
193 if (totalLen > bytes.length) bytes = JU.AU.ensureLengthByte (bytes, totalLen * 2);
194 System.arraycopy (buf, 0, bytes, totalLen - len, len);
195 if (n != 2147483647 && totalLen + buflen > bytes.length) buflen = bytes.length - totalLen;
196 }
197 if (totalLen == bytes.length) return bytes;
198 buf =  Clazz.newByteArray (totalLen, 0);
199 System.arraycopy (bytes, 0, buf, 0, totalLen);
200 return buf;
201 }, "java.io.InputStream,~N");
202 c$.StreamToUTF8String = Clazz.defineMethod (c$, "StreamToUTF8String", 
203 function (bis) {
204 var data =  new Array (1);
205 try {
206 JU.Rdr.readAllAsString (JU.Rdr.getBufferedReader (bis, "UTF-8"), -1, true, data, 0);
207 } catch (e) {
208 if (Clazz.exceptionOf (e, java.io.IOException)) {
209 } else {
210 throw e;
211 }
212 }
213 return data[0];
214 }, "java.io.BufferedInputStream");
215 c$.readAllAsString = Clazz.defineMethod (c$, "readAllAsString", 
216 function (br, nBytesMax, allowBinary, data, i) {
217 try {
218 var sb = JU.SB.newN (8192);
219 var line;
220 if (nBytesMax < 0) {
221 line = br.readLine ();
222 if (allowBinary || line != null && line.indexOf ('\0') < 0 && (line.length != 4 || line.charCodeAt (0) != 65533 || line.indexOf ("PNG") != 1)) {
223 sb.append (line).appendC ('\n');
224 while ((line = br.readLine ()) != null) sb.append (line).appendC ('\n');
225
226 }} else {
227 var n = 0;
228 var len;
229 while (n < nBytesMax && (line = br.readLine ()) != null) {
230 if (nBytesMax - n < (len = line.length) + 1) line = line.substring (0, nBytesMax - n - 1);
231 sb.append (line).appendC ('\n');
232 n += len + 1;
233 }
234 }br.close ();
235 data[i] = sb.toString ();
236 return true;
237 } catch (ioe) {
238 if (Clazz.exceptionOf (ioe, Exception)) {
239 data[i] = ioe.toString ();
240 return false;
241 } else {
242 throw ioe;
243 }
244 }
245 }, "java.io.BufferedReader,~N,~B,~A,~N");
246 c$.getPngZipPointAndCount = Clazz.defineMethod (c$, "getPngZipPointAndCount", 
247 function (bis, pt_count) {
248 bis.mark (75);
249 try {
250 var data = JU.Rdr.getLimitedStreamBytes (bis, 74);
251 bis.reset ();
252 var pt = 0;
253 for (var i = 64, f = 1; --i > 54; f *= 10) pt += (data[i] - 48) * f;
254
255 var n = 0;
256 for (var i = 74, f = 1; --i > 64; f *= 10) n += (data[i] - 48) * f;
257
258 pt_count[0] = pt;
259 pt_count[1] = n;
260 } catch (e) {
261 pt_count[1] = 0;
262 }
263 }, "java.io.BufferedInputStream,~A");
264 c$.getPngZipStream = Clazz.defineMethod (c$, "getPngZipStream", 
265 function (bis, asNewStream) {
266 if (!JU.Rdr.isPngZipStream (bis)) return bis;
267 var data =  Clazz.newByteArray (0, 0);
268 bis.mark (75);
269 try {
270 var pt_count =  Clazz.newIntArray (2, 0);
271 JU.Rdr.getPngZipPointAndCount (bis, pt_count);
272 if (pt_count[1] != 0) {
273 var pt = pt_count[0];
274 while (pt > 0) pt -= bis.skip (pt);
275
276 if (!asNewStream) return bis;
277 data = JU.Rdr.getLimitedStreamBytes (bis, pt_count[1]);
278 }} catch (e) {
279 } finally {
280 try {
281 if (asNewStream) bis.close ();
282 } catch (e) {
283 if (Clazz.exceptionOf (e, Exception)) {
284 } else {
285 throw e;
286 }
287 }
288 }
289 return JU.Rdr.getBIS (data);
290 }, "java.io.BufferedInputStream,~B");
291 c$.readFileAsMap = Clazz.defineMethod (c$, "readFileAsMap", 
292 function (jzt, is, bdata, name) {
293 jzt.readFileAsMap (is, bdata, name);
294 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,java.util.Map,~S");
295 c$.getZipDirectoryAsStringAndClose = Clazz.defineMethod (c$, "getZipDirectoryAsStringAndClose", 
296 function (jzt, t) {
297 return jzt.getZipDirectoryAsStringAndClose (t);
298 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream");
299 c$.newGZIPInputStream = Clazz.defineMethod (c$, "newGZIPInputStream", 
300 function (jzt, bis) {
301 return jzt.newGZIPInputStream (bis);
302 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream");
303 c$.newZipInputStream = Clazz.defineMethod (c$, "newZipInputStream", 
304 function (jzt, $in) {
305 return jzt.newZipInputStream ($in);
306 }, "javajs.api.GenericZipTools,java.io.InputStream");
307 c$.getZipFileDirectory = Clazz.defineMethod (c$, "getZipFileDirectory", 
308 function (jzt, bis, subFileList, listPtr, asBufferedInputStream) {
309 return jzt.getZipFileDirectory (jzt, bis, subFileList, listPtr, asBufferedInputStream);
310 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~A,~N,~B");
311 c$.getZipDirectoryAndClose = Clazz.defineMethod (c$, "getZipDirectoryAndClose", 
312 function (jzt, t, manifestID) {
313 return jzt.getZipDirectoryAndClose (t, manifestID);
314 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~S");
315 c$.getAllZipData = Clazz.defineMethod (c$, "getAllZipData", 
316 function (jzt, bis, subFileList, replace, string, fileData) {
317 jzt.getAllZipData (bis, subFileList, replace, string, fileData);
318 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~A,~S,~S,java.util.Map");
319 c$.getZipFileContentsAsBytes = Clazz.defineMethod (c$, "getZipFileContentsAsBytes", 
320 function (jzt, bis, subFileList, i) {
321 return jzt.getZipFileContentsAsBytes (bis, subFileList, i);
322 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream,~A,~N");
323 c$.addZipEntry = Clazz.defineMethod (c$, "addZipEntry", 
324 function (jzt, zos, fileName) {
325 jzt.addZipEntry (zos, fileName);
326 }, "javajs.api.GenericZipTools,~O,~S");
327 c$.closeZipEntry = Clazz.defineMethod (c$, "closeZipEntry", 
328 function (jzt, zos) {
329 jzt.closeZipEntry (zos);
330 }, "javajs.api.GenericZipTools,~O");
331 c$.getZipOutputStream = Clazz.defineMethod (c$, "getZipOutputStream", 
332 function (jzt, bos) {
333 return jzt.getZipOutputStream (bos);
334 }, "javajs.api.GenericZipTools,~O");
335 c$.getCrcValue = Clazz.defineMethod (c$, "getCrcValue", 
336 function (jzt, bytes) {
337 return jzt.getCrcValue (bytes);
338 }, "javajs.api.GenericZipTools,~A");
339 c$.getZipRoot = Clazz.defineMethod (c$, "getZipRoot", 
340 function (fileName) {
341 var pt = fileName.indexOf ("|");
342 return (pt < 0 ? fileName : fileName.substring (0, pt));
343 }, "~S");
344 });