public static void readFile(String filenameIn, int bufferSize) throws IOException { long lenIn = new File(filenameIn).length(); if (debug) System.out.println("read " + filenameIn + " len = " + lenIn); InputStream in = new FileInputStream(filenameIn); long total = 0; long start = System.currentTimeMillis(); byte[] buffer = new byte[bufferSize]; while (true) { int bytesRead = in.read(buffer); if (bytesRead == -1) break; total += buffer[0]; } double took = .001 * (System.currentTimeMillis() - start); in.close(); double rate = lenIn / took / (1000 * 1000); System.out.println( " copy (" + bufferSize + ") took = " + took + " sec; rate = " + rate + "Mb/sec total=" + total); }
public static long checksumBufferedInputStream(Path filename) throws IOException { try (InputStream in = new BufferedInputStream(Files.newInputStream(filename))) { CRC32 crc = new CRC32(); int c; while ((c = in.read()) != -1) crc.update(c); return crc.getValue(); } }
public static int pipe_stream(InputStream is, OutputStream os, boolean wantsKeepOpen) throws IOException { // U: copia de un stream al otro int cnt = 0; int n; byte[] buffer = new byte[BUFF_SZ]; while ((n = is.read(buffer)) > -1) { cnt += n; os.write(buffer, 0, n); } if (!wantsKeepOpen) { is.close(); os.close(); } return cnt; }
public static String get_stream(InputStream is, String encoding) throws IOException { if (encoding == null) { encoding = CfgEncodingDflt; } byte[] buffer = new byte[BUFF_SZ]; StringBuilder out = new StringBuilder(); logm("DBG", 9, "STREAM GET", is + ""); for (; ; ) { int rsz = is.read(buffer, 0, buffer.length); logm("DBG", 9, "STREAM GET READ", rsz); if (rsz < 0) break; out.append(new String(buffer, 0, rsz, encoding)); } String s = out.toString(); logm("DBG", 9, "STREAM GET RESULT", s); return s; }
/** * Determines from the magic number whether the given InputStream points to a DDS image. The given * InputStream must return true from markSupported() and support a minimum of four bytes of * read-ahead. * * @param in Stream to check * @return true if input stream is DDS image or false otherwise * @throws java.io.IOException if an I/O exception occurred */ public static boolean isDDSImage(InputStream in) throws IOException { if (!(in instanceof BufferedInputStream)) { in = new BufferedInputStream(in); } if (!in.markSupported()) { throw new IOException( "Can not test non-destructively whether given InputStream is a DDS image"); } in.mark(4); int magic = 0; for (int i = 0; i < 4; i++) { int tmp = in.read(); if (tmp < 0) { in.reset(); return false; } magic = ((magic >>> 8) | (tmp << 24)); } in.reset(); return (magic == MAGIC); }