3 * $Date: 2007-04-05 09:07:28 -0500 (Thu, 05 Apr 2007) $
6 * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
7 * for use in SwingJS via transpilation into JavaScript using Java2Script.
9 * Copyright (C) 2003-2005 The Jmol Development Team
11 * Contact: jmol-developers@lists.sf.net
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 import java.io.BufferedInputStream;
30 import java.io.BufferedReader;
31 import java.io.BufferedWriter;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.OutputStream;
37 import java.io.OutputStreamWriter;
38 import java.io.StringReader;
39 import java.io.UnsupportedEncodingException;
43 import javajs.api.Interface;
46 import javajs.api.GenericCifDataParser;
47 import javajs.api.GenericLineReader;
48 import javajs.api.GenericZipTools;
51 * A general helper class for a variety of stream and reader functionality
54 * stream and byte magic-number decoding for PNG, PNGJ, ZIP, and GZIP streams
56 * various stream/reader methods, including UTF-encoded stream reading
58 * reflection-protected access to a CIF parser and ZIP tools
64 public class Rdr implements GenericLineReader {
66 public static class StreamReader extends BufferedReader {
68 private BufferedInputStream stream;
70 public StreamReader(BufferedInputStream bis, String charSet) throws UnsupportedEncodingException {
71 super(new InputStreamReader(bis, (charSet == null ? "UTF-8" : charSet)));
75 public BufferedInputStream getStream() {
78 } catch (IOException e) {
86 BufferedReader reader;
88 public Rdr(BufferedReader reader) {
93 public String readNextLine() throws Exception {
94 return reader.readLine();
97 public static Map<String, Object> readCifData(GenericCifDataParser parser, BufferedReader br) {
98 return parser.set(null, br, false).getAllCifData();
107 * Read a UTF-8 byte array fully, converting it to a String.
108 * Called by Jmol's XMLReaders
111 * @return a UTF-8 string
113 public static String bytesToUTF8String(byte[] bytes) {
114 return streamToUTF8String(new BufferedInputStream(new ByteArrayInputStream(bytes)));
119 * Read a UTF-8 stream fully, converting it to a String.
120 * Called by Jmol's XMLReaders
123 * @return a UTF-8 string
125 public static String streamToUTF8String(BufferedInputStream bis) {
126 String[] data = new String[1];
128 readAllAsString(getBufferedReader(bis, "UTF-8"), -1, true, data, 0);
129 } catch (IOException e) {
135 * Read an input stream fully, saving a byte array, then
136 * return a buffered reader to those bytes converted to string form.
141 * @throws IOException
143 public static BufferedReader getBufferedReader(BufferedInputStream bis, String charSet)
145 // could also just make sure we have a buffered input stream here.
146 if (getUTFEncodingForStream(bis) == Encoding.NONE)
147 return new StreamReader(bis, (charSet == null ? "UTF-8" : charSet));
148 byte[] bytes = getLimitedStreamBytes(bis, -1);
150 return getBR(charSet == null ? fixUTF(bytes) : new String(bytes, charSet));
154 * This method is specifically for strings that are marked for UTF 8 or 16.
157 * @return UTF-decoded bytes
159 public static String fixUTF(byte[] bytes) {
160 Encoding encoding = getUTFEncoding(bytes);
161 if (encoding != Encoding.NONE)
163 String s = new String(bytes, encoding.name().replace('_', '-'));
168 // extra byte at beginning removed
175 } catch (UnsupportedEncodingException e) {
176 System.out.println(e);
178 return new String(bytes);
181 private static Encoding getUTFEncoding(byte[] bytes) {
182 if (bytes.length >= 3 && (bytes[0] & 0xFF) == 0xEF && (bytes[1] & 0xFF) == 0xBB && (bytes[2] & 0xFF) == 0xBF)
183 return Encoding.UTF8;
184 if (bytes.length >= 4 && (bytes[0] & 0xFF) == 0 && (bytes[1] & 0xFF) == 0
185 && (bytes[2] & 0xFF) == 0xFE && (bytes[3] & 0xFF) == 0xFF)
186 return Encoding.UTF_32BE;
187 if (bytes.length >= 4 && (bytes[0] & 0xFF) == 0xFF && (bytes[1] & 0xFF) == 0xFE
188 && (bytes[2] & 0xFF) == 0 && (bytes[3] & 0xFF) == 0)
189 return Encoding.UTF_32LE;
190 if (bytes.length >= 2 && (bytes[0] & 0xFF) == 0xFF && (bytes[1] & 0xFF) == 0xFE)
191 return Encoding.UTF_16LE;
192 if (bytes.length >= 2 && (bytes[0] & 0xFF) == 0xFE && (bytes[1] & 0xFF) == 0xFF)
193 return Encoding.UTF_16BE;
194 return Encoding.NONE;
198 ////////// stream type checking //////////
201 private static Encoding getUTFEncodingForStream(BufferedInputStream is) throws IOException {
202 byte[] abMagic = new byte[4];
206 } catch (Exception e) {
207 return Encoding.NONE;
209 is.read(abMagic, 0, 4);
211 return getUTFEncoding(abMagic);
214 public static boolean isBase64(SB sb) {
215 return (sb.indexOf(";base64,") == 0);
218 public static boolean isCompoundDocumentS(InputStream is) {
219 return isCompoundDocumentB(getMagic(is, 8));
222 public static boolean isCompoundDocumentB(byte[] bytes) {
223 return (bytes.length >= 8 && (bytes[0] & 0xFF) == 0xD0
224 && (bytes[1] & 0xFF) == 0xCF && (bytes[2] & 0xFF) == 0x11
225 && (bytes[3] & 0xFF) == 0xE0 && (bytes[4] & 0xFF) == 0xA1
226 && (bytes[5] & 0xFF) == 0xB1 && (bytes[6] & 0xFF) == 0x1A
227 && (bytes[7] & 0xFF) == 0xE1);
230 public static boolean isBZip2S(InputStream is) {
231 return isBZip2B(getMagic(is, 3));
234 public static boolean isGzipS(InputStream is) {
235 return isGzipB(getMagic(is, 2));
238 public static boolean isBZip2B(byte[] bytes) {
239 return (bytes != null && bytes.length >= 3 // BZh
240 && (bytes[0] & 0xFF) == 0x42 && (bytes[1] & 0xFF) == 0x5A && (bytes[2] & 0xFF) == 0x68);
243 public static boolean isGzipB(byte[] bytes) {
244 return (bytes != null && bytes.length >= 2
245 && (bytes[0] & 0xFF) == 0x1F && (bytes[1] & 0xFF) == 0x8B);
248 public static boolean isPickleS(InputStream is) {
249 return isPickleB(getMagic(is, 2));
252 public static boolean isPickleB(byte[] bytes) {
253 return (bytes != null && bytes.length >= 2
254 && (bytes[0] & 0xFF) == 0x7D && (bytes[1] & 0xFF) == 0x71);
257 public static boolean isMessagePackS(InputStream is) {
258 return isMessagePackB(getMagic(is, 2));
261 public static boolean isMessagePackB(byte[] bytes) {
262 // look for 'map' start, but PNG files start with 0x89, which is
263 // the MessagePack start for a 9-member map, so in that case we have
264 // to check that the next byte is not "P" as in <89>PNG
267 return (bytes != null && bytes.length >= 1 && (((b = bytes[0] & 0xFF)) == 0xDE || (b & 0xE0) == 0x80 && bytes[1] != 0x50));
270 public static boolean isPngZipStream(InputStream is) {
271 return isPngZipB(getMagic(is, 55));
274 public static boolean isPngZipB(byte[] bytes) {
275 // \0PNGJ starting at byte 50
276 return (bytes[50] == 0 && bytes[51] == 0x50 && bytes[52] == 0x4E && bytes[53] == 0x47 && bytes[54] == 0x4A);
280 * Check for a ZIP input stream - starting with "PK<03><04>"
282 * @return true if a ZIP stream
284 public static boolean isZipS(InputStream is) {
285 return isZipB(getMagic(is, 4));
288 public static boolean isZipB(byte[] bytes) {
289 return (bytes.length >= 4
290 && bytes[0] == 0x50 //PK<03><04>
293 && bytes[3] == 0x04);
296 public static byte[] getMagic(InputStream is, int n) {
297 byte[] abMagic = new byte[n];
300 is.read(abMagic, 0, n);
301 } catch (IOException e) {
305 } catch (IOException e) {
310 public static String guessMimeTypeForBytes(byte[] bytes) {
311 // only options here are JPEG, PNG, GIF, and BMP
312 switch (bytes.length < 2 ? -1 : bytes[1]) {
314 return "image/jpg"; // 0xFF 0x00 ...
316 return "image/gif"; // GIF89a...
318 return "image/BMP"; // BM...
322 return "image/unknown";
327 ////////// stream/byte methods ///////////
329 public static BufferedInputStream getBIS(byte[] bytes) {
330 return new BufferedInputStream(new ByteArrayInputStream(bytes));
333 public static BufferedReader getBR(String string) {
334 return new BufferedReader(new StringReader(string));
338 public static BufferedInputStream toBIS(Object o) {
339 return (AU.isAB(o) ? getBIS((byte[]) o)
340 : o instanceof SB ? getBIS(Rdr.getBytesFromSB((SB) o))
341 : o instanceof String ? getBIS(((String) o).getBytes()) : null);
346 * Drill down into a GZIP stack until no more layers.
350 * @return non-gzipped buffered input stream.
352 * @throws IOException
354 public static BufferedInputStream getUnzippedInputStream(GenericZipTools jzt, BufferedInputStream bis) throws IOException {
356 bis = new BufferedInputStream(jzt.newGZIPInputStream(bis));
360 public static BufferedInputStream getUnzippedInputStreamBZip2(GenericZipTools jzt,
361 BufferedInputStream bis) throws IOException {
362 while (isBZip2S(bis))
363 bis = new BufferedInputStream(jzt.newBZip2InputStream(bis));
369 * Allow for base64-encoding check.
374 public static byte[] getBytesFromSB(SB sb) {
375 return (isBase64(sb) ? Base64.decodeBase64(sb.substring(8)) : sb.toBytes(0, -1));
379 * Read a an entire BufferedInputStream for its bytes, and
380 * either return them or leave them in the designated output channel.
383 * @param out a destination output channel, or null
384 * @return byte[] (if out is null) or a message indicating length (if not)
386 * @throws IOException
388 public static Object getStreamAsBytes(BufferedInputStream bis,
389 OC out) throws IOException {
390 byte[] buf = new byte[1024];
391 byte[] bytes = (out == null ? new byte[4096] : null);
394 while ((len = bis.read(buf, 0, 1024)) > 0) {
397 if (totalLen >= bytes.length)
398 bytes = AU.ensureLengthByte(bytes, totalLen * 2);
399 System.arraycopy(buf, 0, bytes, totalLen - len, len);
401 out.write(buf, 0, len);
406 return AU.arrayCopyByte(bytes, totalLen);
408 return totalLen + " bytes";
412 * Read a possibly limited number of bytes (when n > 0) from a stream,
413 * leaving the stream open.
415 * @param is an input stream, not necessarily buffered.
416 * @param n the maximum number of bytes to read, or -1 for all
417 * @return the bytes read
419 * @throws IOException
421 public static byte[] getLimitedStreamBytes(InputStream is, long n)
424 //Note: You cannot use InputStream.available() to reliably read
425 // zip data from the web.
427 int buflen = (n > 0 && n < 1024 ? (int) n : 1024);
428 byte[] buf = new byte[buflen];
429 byte[] bytes = new byte[n < 0 ? 4096 : (int) n];
433 n = Integer.MAX_VALUE;
434 while (totalLen < n && (len = is.read(buf, 0, buflen)) > 0) {
436 if (totalLen > bytes.length)
437 bytes = AU.ensureLengthByte(bytes, totalLen * 2);
438 System.arraycopy(buf, 0, bytes, totalLen - len, len);
439 if (n != Integer.MAX_VALUE && totalLen + buflen > bytes.length)
440 buflen = bytes.length - totalLen;
443 if (totalLen == bytes.length)
445 buf = new byte[totalLen];
446 System.arraycopy(bytes, 0, buf, 0, totalLen);
451 * This method fills data[i] with string data from a file that may or may not
452 * be binary even though it is being read by a reader. It is meant to be used
453 * simple text-based files only.
460 * @return true if data[i] holds the data; false if data[i] holds an error message.
462 public static boolean readAllAsString(BufferedReader br, int nBytesMax, boolean allowBinary, String[] data, int i) {
464 SB sb = SB.newN(8192);
467 line = br.readLine();
468 if (allowBinary || line != null && line.indexOf('\0') < 0
469 && (line.length() != 4 || line.charAt(0) != 65533
470 || line.indexOf("PNG") != 1)) {
471 sb.append(line).appendC('\n');
472 while ((line = br.readLine()) != null)
473 sb.append(line).appendC('\n');
478 while (n < nBytesMax && (line = br.readLine()) != null) {
479 if (nBytesMax - n < (len = line.length()) + 1)
480 line = line.substring(0, nBytesMax - n - 1);
481 sb.append(line).appendC('\n');
486 data[i] = sb.toString();
488 } catch (Exception ioe) {
489 data[i] = ioe.toString();
495 /////////// PNGJ support /////////////
499 * Look at byte 50 for "\0PNGJxxxxxxxxx+yyyyyyyyy" where xxxxxxxxx is a byte
500 * offset to the JMOL data and yyyyyyyyy is the length of the data.
503 * @return same stream or byte stream
507 * Retrieve the two numbers in a PNG iTXt tag indicating the
508 * file pointer for the start of the ZIP data as well as its length.
513 static void getPngZipPointAndCount(BufferedInputStream bis, int[] pt_count) {
516 byte[] data = getLimitedStreamBytes(bis, 74);
519 for (int i = 64, f = 1; --i > 54; f *= 10)
520 pt += (data[i] - '0') * f;
522 for (int i = 74, f = 1; --i > 64; f *= 10)
523 n += (data[i] - '0') * f;
526 } catch (Throwable e) {
532 * Either advance a PNGJ stream to its zip file data or pull out the ZIP data
533 * bytes and create a new stream for them from which a ZIP utility can start
538 * @return new buffered ByteArrayInputStream, possibly with no data if there is an error
540 public static BufferedInputStream getPngZipStream(BufferedInputStream bis, boolean asNewStream) {
541 if (!isPngZipStream(bis))
543 byte[] data = new byte[0];
546 int pt_count[] = new int[2];
547 getPngZipPointAndCount(bis, pt_count);
548 if (pt_count[1] != 0) {
549 int pt = pt_count[0];
554 data = getLimitedStreamBytes(bis, pt_count[1]);
556 } catch (Throwable e) {
561 } catch (Exception e) {
568 /** We define a request for zip file extraction by vertical bar:
569 * zipName|interiorFileName. These may be nested if there is a
570 * zip file contained in a zip file.
573 * @return filename trimmed of interior fileName
576 public static String getZipRoot(String fileName) {
577 int pt = fileName.indexOf("|");
578 return (pt < 0 ? fileName : fileName.substring(0, pt));
581 public static BufferedWriter getBufferedWriter(OutputStream os, String charSetName) {
582 OutputStreamWriter osw = (OutputStreamWriter) Interface.getInstanceWithParams("java.io.OutputStreamWriter",
583 new Class<?>[] { java.io.OutputStream.class, String.class },
584 new Object[] { os, charSetName == null ? "UTF-8" : charSetName });
586 return new BufferedWriter(osw);