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 () {
5 Clazz.instantialize (this, arguments);
6 }, JU, "Rdr", null, javajs.api.GenericLineReader);
7 Clazz.makeConstructor (c$,
10 }, "java.io.BufferedReader");
11 Clazz.overrideMethod (c$, "readNextLine",
13 return this.reader.readLine ();
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",
21 var encoding = JU.Rdr.getUTFEncoding (bytes);
22 if (encoding !== JU.Encoding.NONE) try {
23 var s = String.instantialize (bytes, encoding.name ().$replace ('_', '-'));
25 case JU.Encoding.UTF8:
26 case JU.Encoding.UTF_16BE:
27 case JU.Encoding.UTF_16LE:
35 if (Clazz.exceptionOf (e, java.io.UnsupportedEncodingException)) {
36 System.out.println (e);
41 return String.instantialize (bytes);
43 c$.getUTFEncoding = Clazz.defineMethod (c$, "getUTFEncoding",
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;
52 c$.getUTFEncodingForStream = Clazz.defineMethod (c$, "getUTFEncodingForStream",
56 }var abMagic = Clazz.newByteArray (4, 0);
61 if (Clazz.exceptionOf (e, Exception)) {
62 return JU.Encoding.NONE;
67 is.read (abMagic, 0, 4);
69 return JU.Rdr.getUTFEncoding (abMagic);
70 }, "java.io.BufferedInputStream");
71 c$.isBase64 = Clazz.defineMethod (c$, "isBase64",
73 return (sb.indexOf (";base64,") == 0);
75 c$.isCompoundDocumentS = Clazz.defineMethod (c$, "isCompoundDocumentS",
77 return JU.Rdr.isCompoundDocumentB (JU.Rdr.getMagic (is, 8));
78 }, "java.io.InputStream");
79 c$.isCompoundDocumentB = Clazz.defineMethod (c$, "isCompoundDocumentB",
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);
83 c$.isGzipS = Clazz.defineMethod (c$, "isGzipS",
85 return JU.Rdr.isGzipB (JU.Rdr.getMagic (is, 2));
86 }, "java.io.InputStream");
87 c$.isGzipB = Clazz.defineMethod (c$, "isGzipB",
89 return (bytes != null && bytes.length >= 2 && bytes[0] == 0x1F && bytes[1] == 0x8B);
91 c$.isPickleS = Clazz.defineMethod (c$, "isPickleS",
93 return JU.Rdr.isPickleB (JU.Rdr.getMagic (is, 2));
94 }, "java.io.InputStream");
95 c$.isPickleB = Clazz.defineMethod (c$, "isPickleB",
97 return (bytes != null && bytes.length >= 2 && bytes[0] == 0x7D && bytes[1] == 0x71);
99 c$.isPngZipStream = Clazz.defineMethod (c$, "isPngZipStream",
101 return JU.Rdr.isPngZipB (JU.Rdr.getMagic (is, 55));
102 }, "java.io.InputStream");
103 c$.isPngZipB = Clazz.defineMethod (c$, "isPngZipB",
105 return (bytes[50] == 0 && bytes[51] == 0x50 && bytes[52] == 0x4E && bytes[53] == 0x47 && bytes[54] == 0x4A);
107 c$.isZipS = Clazz.defineMethod (c$, "isZipS",
109 return JU.Rdr.isZipB (JU.Rdr.getMagic (is, 4));
110 }, "java.io.InputStream");
111 c$.isZipB = Clazz.defineMethod (c$, "isZipB",
113 return (bytes.length >= 4 && bytes[0] == 0x50 && bytes[1] == 0x4B && bytes[2] == 0x03 && bytes[3] == 0x04);
115 c$.getMagic = Clazz.defineMethod (c$, "getMagic",
117 var abMagic = Clazz.newByteArray (n, 0);
122 is.read (abMagic, 0, n);
124 if (Clazz.exceptionOf (e, java.io.IOException)) {
132 if (Clazz.exceptionOf (e, java.io.IOException)) {
138 }, "java.io.InputStream,~N");
139 c$.getBIS = Clazz.defineMethod (c$, "getBIS",
141 return new java.io.BufferedInputStream ( new java.io.ByteArrayInputStream (bytes));
143 c$.getBR = Clazz.defineMethod (c$, "getBR",
145 return new java.io.BufferedReader ( new java.io.StringReader (string));
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));
152 }, "javajs.api.GenericZipTools,java.io.BufferedInputStream");
153 c$.getBytesFromSB = Clazz.defineMethod (c$, "getBytesFromSB",
155 return (JU.Rdr.isBase64 (sb) ? JU.Base64.decodeBase64 (sb.substring (8)) : sb.toBytes (0, -1));
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);
163 while ((len = bis.read (buf, 0, 1024)) > 0) {
166 if (totalLen >= bytes.length) bytes = JU.AU.ensureLengthByte (bytes, totalLen * 2);
167 System.arraycopy (buf, 0, bytes, totalLen - len, len);
169 out.write (buf, 0, len);
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);
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",
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);
190 if (n < 0) n = 2147483647;
191 while (totalLen < n && (len = is.read (buf, 0, buflen)) > 0) {
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;
197 if (totalLen == bytes.length) return bytes;
198 buf = Clazz.newByteArray (totalLen, 0);
199 System.arraycopy (bytes, 0, buf, 0, totalLen);
201 }, "java.io.InputStream,~N");
202 c$.StreamToUTF8String = Clazz.defineMethod (c$, "StreamToUTF8String",
204 var data = new Array (1);
206 JU.Rdr.readAllAsString (JU.Rdr.getBufferedReader (bis, "UTF-8"), -1, true, data, 0);
208 if (Clazz.exceptionOf (e, java.io.IOException)) {
214 }, "java.io.BufferedInputStream");
215 c$.readAllAsString = Clazz.defineMethod (c$, "readAllAsString",
216 function (br, nBytesMax, allowBinary, data, i) {
218 var sb = JU.SB.newN (8192);
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');
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');
235 data[i] = sb.toString ();
238 if (Clazz.exceptionOf (ioe, Exception)) {
239 data[i] = ioe.toString ();
245 }, "java.io.BufferedReader,~N,~B,~A,~N");
246 c$.getPngZipPointAndCount = Clazz.defineMethod (c$, "getPngZipPointAndCount",
247 function (bis, pt_count) {
250 var data = JU.Rdr.getLimitedStreamBytes (bis, 74);
253 for (var i = 64, f = 1; --i > 54; f *= 10) pt += (data[i] - 48) * f;
256 for (var i = 74, f = 1; --i > 64; f *= 10) n += (data[i] - 48) * f;
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);
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);
276 if (!asNewStream) return bis;
277 data = JU.Rdr.getLimitedStreamBytes (bis, pt_count[1]);
281 if (asNewStream) bis.close ();
283 if (Clazz.exceptionOf (e, Exception)) {
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",
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));