33 * $Date: 2007-04-05 09:07:28 -0500 (Thu, 05 Apr 2007) $
44 * $Revision: 7326 $
55 *
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.
8- *
96 * Copyright (C) 2003-2005 The Jmol Development Team
107 *
118 * Contact: jmol-developers@lists.sf.net
@@ -79,6 +76,8 @@ public BufferedInputStream getStream() {
7976
8077 }
8178
79+ private static final int UNDERFLOW = 264 ;
80+
8281 BufferedReader reader ;
8382
8483 public Rdr (BufferedReader reader ) {
@@ -306,13 +305,14 @@ public static boolean isZipB(byte[] bytes) {
306305 }
307306
308307 public static byte [] getMagic (InputStream is , int n ) {
309- byte [] abMagic = (n > 264 ? new byte [n ] : b264 == null ? (b264 = new byte [264 ]) : b264 );
308+ byte [] abMagic = (n > 264 ? new byte [n ] : b264 == null ? (b264 = new byte [265 ]) : b264 );
310309 try {
310+ abMagic [UNDERFLOW ] = -1 ;
311311 is .mark (n + 1 );
312312 int i = is .read (abMagic , 0 , n );
313313 if (i < n ) {
314314 // ensure
315- abMagic [0 ] = abMagic [257 ] = 0 ;
315+ abMagic [0 ] = abMagic [UNDERFLOW ] = 0 ;
316316 }
317317 } catch (IOException e ) {
318318 }
@@ -518,7 +518,7 @@ public static BufferedWriter getBufferedWriter(OutputStream os, String charSetNa
518518 public static boolean isTar (BufferedInputStream bis ) {
519519 byte [] bytes = getMagic (bis , 264 );
520520 // check for ustar<00>
521- return (bytes [0 ] != 0
521+ return (bytes [UNDERFLOW ] == - 1
522522 && (bytes [257 ] & 0xFF ) == 0x75
523523 && (bytes [258 ] & 0xFF ) == 0x73
524524 && (bytes [259 ] & 0xFF ) == 0x74
@@ -527,4 +527,20 @@ public static boolean isTar(BufferedInputStream bis) {
527527 );
528528 }
529529
530+ /**
531+ * Just looking for non-printable characters.
532+ *
533+ * @param bis
534+ * @param n length to scan
535+ * @return true if non-printable characters found
536+ */
537+ public static boolean isBinary (BufferedInputStream bis , int n ) {
538+ byte [] bytes = getMagic (bis , n );
539+ if (b264 [UNDERFLOW ] == -1 )
540+ for (int i = 0 ; i < n ; i ++)
541+ if (bytes [i ] < 9 ) // TAB
542+ return true ;
543+ return false ;
544+ }
545+
530546}
0 commit comments