3 * $Date: 2007-04-05 09:07:28 -0500 (Thu, 05 Apr 2007) $
6 * Copyright (C) 2003-2005 The Jmol Development Team
8 * Contact: jmol-developers@lists.sf.net
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 import java.io.BufferedInputStream;
27 import java.io.BufferedReader;
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.StringReader;
33 import java.io.UnsupportedEncodingException;
38 import javajs.api.GenericCifDataParser;
39 import javajs.api.GenericLineReader;
40 import javajs.api.GenericZipTools;
43 * A general helper class for a variety of stream and reader functionality
46 * stream and byte magic-number decoding for PNG, PNGJ, ZIP, and GZIP streams
48 * various stream/reader methods, including UTF-encoded stream reading
50 * reflection-protected access to a CIF parser and ZIP tools
56 public class Rdr implements GenericLineReader {
58 BufferedReader reader;
60 public Rdr(BufferedReader reader) {
65 public String readNextLine() throws Exception {
66 return reader.readLine();
69 public static Map<String, Object> readCifData(GenericCifDataParser parser, BufferedReader br) {
70 return parser.set(null, br).getAllCifData();
76 public static String fixUTF(byte[] bytes) {
77 Encoding encoding = getUTFEncoding(bytes);
78 if (encoding != Encoding.NONE)
80 String s = new String(bytes, encoding.name().replace('_', '-'));
85 // extra byte at beginning removed
92 } catch (UnsupportedEncodingException e) {
93 System.out.println(e);
95 return new String(bytes);
98 private static Encoding getUTFEncoding(byte[] bytes) {
99 if (bytes.length >= 3 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF)
100 return Encoding.UTF8;
101 if (bytes.length >= 4 && bytes[0] == (byte) 0 && bytes[1] == (byte) 0
102 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF)
103 return Encoding.UTF_32BE;
104 if (bytes.length >= 4 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE
105 && bytes[2] == (byte) 0 && bytes[3] == (byte) 0)
106 return Encoding.UTF_32LE;
107 if (bytes.length >= 2 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE)
108 return Encoding.UTF_16LE;
109 if (bytes.length >= 2 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF)
110 return Encoding.UTF_16BE;
111 return Encoding.NONE;
115 ////////// stream type checking //////////
118 private static Encoding getUTFEncodingForStream(BufferedInputStream is) throws IOException {
127 byte[] abMagic = new byte[4];
131 } catch (Exception e) {
132 return Encoding.NONE;
134 is.read(abMagic, 0, 4);
136 return getUTFEncoding(abMagic);
139 public static boolean isBase64(SB sb) {
140 return (sb.indexOf(";base64,") == 0);
143 public static boolean isCompoundDocumentS(InputStream is) {
144 return isCompoundDocumentB(getMagic(is, 8));
147 public static boolean isCompoundDocumentB(byte[] bytes) {
148 return (bytes.length >= 8 && bytes[0] == (byte) 0xD0
149 && bytes[1] == (byte) 0xCF && bytes[2] == (byte) 0x11
150 && bytes[3] == (byte) 0xE0 && bytes[4] == (byte) 0xA1
151 && bytes[5] == (byte) 0xB1 && bytes[6] == (byte) 0x1A
152 && bytes[7] == (byte) 0xE1);
155 public static boolean isGzipS(InputStream is) {
156 return isGzipB(getMagic(is, 2));
159 public static boolean isGzipB(byte[] bytes) {
160 return (bytes != null && bytes.length >= 2
161 && bytes[0] == (byte) 0x1F && bytes[1] == (byte) 0x8B);
164 public static boolean isPickleS(InputStream is) {
165 return Rdr.isPickleB(getMagic(is, 2));
168 public static boolean isPickleB(byte[] bytes) {
169 return (bytes != null && bytes.length >= 2
170 && bytes[0] == (byte) 0x7D && bytes[1] == (byte) 0x71);
173 public static boolean isPngZipStream(InputStream is) {
174 return isPngZipB(getMagic(is, 55));
177 public static boolean isPngZipB(byte[] bytes) {
178 // \0PNGJ starting at byte 50
179 return (bytes[50] == 0 && bytes[51] == 0x50 && bytes[52] == 0x4E && bytes[53] == 0x47 && bytes[54] == 0x4A);
182 public static boolean isZipS(InputStream is) {
183 return isZipB(getMagic(is, 4));
186 public static boolean isZipB(byte[] bytes) {
187 return (bytes.length >= 4
188 && bytes[0] == 0x50 //PK<03><04>
191 && bytes[3] == 0x04);
194 private static byte[] getMagic(InputStream is, int n) {
195 byte[] abMagic = new byte[n];
206 is.read(abMagic, 0, n);
207 } catch (IOException e) {
211 } catch (IOException e) {
216 public static String guessMimeTypeForBytes(byte[] bytes) {
217 // only options here are JPEG, PNG, GIF, and BMP
218 switch (bytes.length < 2 ? -1 : bytes[1]) {
220 return "image/jpg"; // 0xFF 0x00 ...
222 return "image/gif"; // GIF89a...
224 return "image/BMP"; // BM...
228 return "image/unknown";
233 ////////// stream/byte methods ///////////
235 public static BufferedInputStream getBIS(byte[] bytes) {
236 return new BufferedInputStream(new ByteArrayInputStream(bytes));
239 public static BufferedReader getBR(String string) {
240 return new BufferedReader(new StringReader(string));
244 * Drill down into a GZIP stack until no more layers.
248 * @return non-gzipped buffered input stream.
250 * @throws IOException
252 public static BufferedInputStream getUnzippedInputStream(GenericZipTools jzt, BufferedInputStream bis) throws IOException {
254 bis = new BufferedInputStream(jzt.newGZIPInputStream(bis));
259 * Allow for base64-encoding check.
264 public static byte[] getBytesFromSB(SB sb) {
265 return (isBase64(sb) ? Base64.decodeBase64(sb.substring(8)) : sb.toBytes(0, -1));
269 * Read a an entire BufferedInputStream for its bytes, and
270 * either return them or leave them in the designated output channel.
273 * @param out a destination output channel, or null
274 * @return byte[] (if out is null) or a message indicating length (if not)
276 * @throws IOException
278 public static Object getStreamAsBytes(BufferedInputStream bis,
279 OC out) throws IOException {
280 byte[] buf = new byte[1024];
281 byte[] bytes = (out == null ? new byte[4096] : null);
284 while ((len = bis.read(buf, 0, 1024)) > 0) {
287 if (totalLen >= bytes.length)
288 bytes = AU.ensureLengthByte(bytes, totalLen * 2);
289 System.arraycopy(buf, 0, bytes, totalLen - len, len);
291 out.write(buf, 0, len);
296 return AU.arrayCopyByte(bytes, totalLen);
298 return totalLen + " bytes";
302 * Read an input stream fully, saving a byte array, then
303 * return a buffered reader to those bytes converted to string form.
308 * @throws IOException
310 public static BufferedReader getBufferedReader(BufferedInputStream bis, String charSet)
312 // could also just make sure we have a buffered input stream here.
313 if (getUTFEncodingForStream(bis) == Encoding.NONE)
314 return new BufferedReader(new InputStreamReader(bis, (charSet == null ? "UTF-8" : charSet)));
315 byte[] bytes = getLimitedStreamBytes(bis, -1);
317 return getBR(charSet == null ? fixUTF(bytes) : new String(bytes, charSet));
321 * Read a possibly limited number of bytes (when n > 0) from a stream,
322 * leaving the stream open.
324 * @param is an input stream, not necessarily buffered.
325 * @param n the maximum number of bytes to read, or -1 for all
326 * @return the bytes read
328 * @throws IOException
330 public static byte[] getLimitedStreamBytes(InputStream is, long n)
333 //Note: You cannot use InputStream.available() to reliably read
334 // zip data from the web.
336 int buflen = (n > 0 && n < 1024 ? (int) n : 1024);
337 byte[] buf = new byte[buflen];
338 byte[] bytes = new byte[n < 0 ? 4096 : (int) n];
342 n = Integer.MAX_VALUE;
343 while (totalLen < n && (len = is.read(buf, 0, buflen)) > 0) {
345 if (totalLen > bytes.length)
346 bytes = AU.ensureLengthByte(bytes, totalLen * 2);
347 System.arraycopy(buf, 0, bytes, totalLen - len, len);
348 if (n != Integer.MAX_VALUE && totalLen + buflen > bytes.length)
349 buflen = bytes.length - totalLen;
352 if (totalLen == bytes.length)
354 buf = new byte[totalLen];
355 System.arraycopy(bytes, 0, buf, 0, totalLen);
361 * Read a UTF-8 stream fully, converting it to a String.
362 * Called by Jmol's XMLReaders
365 * @return a UTF-8 string
367 public static String StreamToUTF8String(BufferedInputStream bis) {
368 String[] data = new String[1];
370 readAllAsString(getBufferedReader(bis, "UTF-8"), -1, true, data, 0);
371 } catch (IOException e) {
377 * This method fills data[i] with string data from a file that may or may not
378 * be binary even though it is being read by a reader. It is meant to be used
379 * simple text-based files only.
386 * @return true if data[i] holds the data; false if data[i] holds an error message.
388 public static boolean readAllAsString(BufferedReader br, int nBytesMax, boolean allowBinary, String[] data, int i) {
390 SB sb = SB.newN(8192);
393 line = br.readLine();
394 if (allowBinary || line != null && line.indexOf('\0') < 0
395 && (line.length() != 4 || line.charAt(0) != 65533
396 || line.indexOf("PNG") != 1)) {
397 sb.append(line).appendC('\n');
398 while ((line = br.readLine()) != null)
399 sb.append(line).appendC('\n');
404 while (n < nBytesMax && (line = br.readLine()) != null) {
405 if (nBytesMax - n < (len = line.length()) + 1)
406 line = line.substring(0, nBytesMax - n - 1);
407 sb.append(line).appendC('\n');
412 data[i] = sb.toString();
414 } catch (Exception ioe) {
415 data[i] = ioe.toString();
421 /////////// PNGJ support /////////////
425 * Look at byte 50 for "\0PNGJxxxxxxxxx+yyyyyyyyy" where xxxxxxxxx is a byte
426 * offset to the JMOL data and yyyyyyyyy is the length of the data.
429 * @return same stream or byte stream
433 * Retrieve the two numbers in a PNG iTXt tag indicating the
434 * file pointer for the start of the ZIP data as well as its length.
439 static void getPngZipPointAndCount(BufferedInputStream bis, int[] pt_count) {
442 byte[] data = getLimitedStreamBytes(bis, 74);
445 for (int i = 64, f = 1; --i > 54; f *= 10)
446 pt += (data[i] - '0') * f;
448 for (int i = 74, f = 1; --i > 64; f *= 10)
449 n += (data[i] - '0') * f;
452 } catch (Throwable e) {
458 * Either advance a PNGJ stream to its zip file data or pull out the ZIP data
459 * bytes and create a new stream for them from which a ZIP utility can start
464 * @return new buffered ByteArrayInputStream, possibly with no data if there is an error
466 public static BufferedInputStream getPngZipStream(BufferedInputStream bis, boolean asNewStream) {
467 if (!isPngZipStream(bis))
469 byte[] data = new byte[0];
472 int pt_count[] = new int[2];
473 getPngZipPointAndCount(bis, pt_count);
474 if (pt_count[1] != 0) {
475 int pt = pt_count[0];
480 data = getLimitedStreamBytes(bis, pt_count[1]);
482 } catch (Throwable e) {
487 } catch (Exception e) {
494 /** We define a request for zip file extraction by vertical bar:
495 * zipName|interiorFileName. These may be nested if there is a
496 * zip file contained in a zip file.
499 * @return filename trimmed of interior fileName
502 public static String getZipRoot(String fileName) {
503 int pt = fileName.indexOf("|");
504 return (pt < 0 ? fileName : fileName.substring(0, pt));