import javajs.api.Interface;
-import javajs.J2SIgnoreImport;
import javajs.api.GenericCifDataParser;
import javajs.api.GenericLineReader;
import javajs.api.GenericZipTools;
*
*
*/
-@J2SIgnoreImport(BufferedWriter.class)
public class Rdr implements GenericLineReader {
+ public static class StreamReader extends BufferedReader {
+
+ private BufferedInputStream stream;
+
+ public StreamReader(BufferedInputStream bis, String charSet) throws UnsupportedEncodingException {
+ super(new InputStreamReader(bis, (charSet == null ? "UTF-8" : charSet)));
+ stream = bis;
+ }
+
+ public BufferedInputStream getStream() {
+ try {
+ stream.reset();
+ } catch (IOException e) {
+ // ignore
+ }
+ return stream;
+ }
+
+ }
+
BufferedReader reader;
public Rdr(BufferedReader reader) {
* Read a UTF-8 byte array fully, converting it to a String.
* Called by Jmol's XMLReaders
*
- * @param bis
+ * @param bytes
* @return a UTF-8 string
*/
public static String bytesToUTF8String(byte[] bytes) {
throws IOException {
// could also just make sure we have a buffered input stream here.
if (getUTFEncodingForStream(bis) == Encoding.NONE)
- return new BufferedReader(new InputStreamReader(bis, (charSet == null ? "UTF-8" : charSet)));
+ return new StreamReader(bis, (charSet == null ? "UTF-8" : charSet));
byte[] bytes = getLimitedStreamBytes(bis, -1);
bis.close();
return getBR(charSet == null ? fixUTF(bytes) : new String(bytes, charSet));
* This method is specifically for strings that are marked for UTF 8 or 16.
*
* @param bytes
- * @return
+ * @return UTF-decoded bytes
*/
public static String fixUTF(byte[] bytes) {
Encoding encoding = getUTFEncoding(bytes);
private static Encoding getUTFEncodingForStream(BufferedInputStream is) throws IOException {
- /**
- * @j2sNative
- *
- * is.resetStream();
- *
- */
- {
- }
+// /**
+// * @j2sNative
+// *
+// * is.resetStream();
+// *
+// */
+// {
+// }
byte[] abMagic = new byte[4];
abMagic[3] = 1;
try{
&& (bytes[7] & 0xFF) == 0xE1);
}
+ public static boolean isBZip2S(InputStream is) {
+ return isBZip2B(getMagic(is, 3));
+ }
+
public static boolean isGzipS(InputStream is) {
return isGzipB(getMagic(is, 2));
}
+ public static boolean isBZip2B(byte[] bytes) {
+ return (bytes != null && bytes.length >= 3 // BZh
+ && (bytes[0] & 0xFF) == 0x42 && (bytes[1] & 0xFF) == 0x5A && (bytes[2] & 0xFF) == 0x68);
+}
+
public static boolean isGzipB(byte[] bytes) {
return (bytes != null && bytes.length >= 2
&& (bytes[0] & 0xFF) == 0x1F && (bytes[1] & 0xFF) == 0x8B);
}
public static boolean isMessagePackS(InputStream is) {
- return isMessagePackB(getMagic(is, 1));
+ return isMessagePackB(getMagic(is, 2));
}
public static boolean isMessagePackB(byte[] bytes) {
- // look for 'map' start
- return (bytes != null && bytes.length >= 1 && (bytes[0] & 0xFF) == 0xDE);
+ // look for 'map' start, but PNG files start with 0x89, which is
+ // the MessagePack start for a 9-member map, so in that case we have
+ // to check that the next byte is not "P" as in <89>PNG
+ int b;
+
+ return (bytes != null && bytes.length >= 1 && (((b = bytes[0] & 0xFF)) == 0xDE || (b & 0xE0) == 0x80 && bytes[1] != 0x50));
}
public static boolean isPngZipStream(InputStream is) {
return (bytes[50] == 0 && bytes[51] == 0x50 && bytes[52] == 0x4E && bytes[53] == 0x47 && bytes[54] == 0x4A);
}
+ /**
+ * Check for a ZIP input stream - starting with "PK<03><04>"
+ * @param is
+ * @return true if a ZIP stream
+ */
public static boolean isZipS(InputStream is) {
return isZipB(getMagic(is, 4));
}
}
public static byte[] getMagic(InputStream is, int n) {
- byte[] abMagic = new byte[n];
- /**
- * @j2sNative
- *
- * is.resetStream();
- *
- */
- {
- }
+ byte[] abMagic = new byte[n];
+// /**
+// * @j2sNative
+// *
+// * is.resetStream();
+// *
+// */
+// {
+// }
try {
is.mark(n + 1);
is.read(abMagic, 0, n);
return new BufferedReader(new StringReader(string));
}
+
+ public static BufferedInputStream toBIS(Object o) {
+ return (AU.isAB(o) ? getBIS((byte[]) o)
+ : o instanceof SB ? getBIS(Rdr.getBytesFromSB((SB) o))
+ : o instanceof String ? getBIS(((String) o).getBytes()) : null);
+ }
+
+
/**
* Drill down into a GZIP stack until no more layers.
* @param jzt
return bis;
}
+ public static BufferedInputStream getUnzippedInputStreamBZip2(GenericZipTools jzt,
+ BufferedInputStream bis) throws IOException {
+ while (isBZip2S(bis))
+ bis = new BufferedInputStream(jzt.newBZip2InputStream(bis));
+ return bis;
+ }
+
+
/**
* Allow for base64-encoding check.
*
public static BufferedWriter getBufferedWriter(OutputStream os, String charSetName) {
OutputStreamWriter osw = (OutputStreamWriter) Interface.getInstanceWithParams("java.io.OutputStreamWriter",
- new Class<?>[] { java.io.OutputStream.class, String.class }, new Object[] {os, charSetName});
+ new Class<?>[] { java.io.OutputStream.class, String.class },
+ new Object[] { os, charSetName == null ? "UTF-8" : charSetName }
+ );
/**
* @j2sNative
* return osw.getBufferedWriter();