public DBaseFile(InputStream is) { if (is == null) { String message = Logging.getMessage("nullValue.InputStreamIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.file = null; try { this.header = this.readHeaderFromStream(is); this.fields = this.readFieldsFromBuffer(this.header.fieldsHeaderBuffer); this.records = this.readRecordsFromStream(is); } catch (Exception e) { String message = Logging.getMessage("generic.ExceptionAttemptingToReadFrom", is.toString()); Logging.logger().log(java.util.logging.Level.SEVERE, message, e); throw new WWRuntimeException(message, e); } }
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 void copyFile(String filenameIn, String filenameOut, boolean buffer) throws IOException { long lenIn = new File(filenameIn).length(); if (debug) System.out.println("read " + filenameIn + " len = " + lenIn); InputStream in = new FileInputStream(filenameIn); if (buffer) new BufferedInputStream(in, 10000); OutputStream out = new FileOutputStream(filenameOut); if (buffer) out = new BufferedOutputStream(out, 1000); long start = System.currentTimeMillis(); IO.copyB(in, out, 10000); out.flush(); double took = .001 * (System.currentTimeMillis() - start); out.close(); in.close(); long lenOut = new File(filenameOut).length(); if (debug) System.out.println(" write file= " + filenameOut + " len = " + lenOut); double rate = lenIn / took / (1000 * 1000); String name = buffer ? "buffer" : "no buffer"; System.out.println(" copy (" + name + ") took = " + took + " sec; rate = " + rate + "Mb/sec"); }
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(); } }
protected Header readHeaderFromFile(File file) throws IOException { InputStream is = null; Header header = null; try { is = new BufferedInputStream(new FileInputStream(file)); header = this.readHeaderFromStream(is); } finally { if (is != null) is.close(); } return header; }
protected List<DBaseRecord> readRecordsFromFile(File file) throws IOException { List<DBaseRecord> records; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(file)); WWIO.skipBytes(is, this.header.headerLength); // Skip over header records = this.readRecordsFromStream(is); } finally { if (is != null) is.close(); } return records; }
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 void copyFile2null(String filenameIn, int buffer) throws IOException { long lenIn = new File(filenameIn).length(); if (debug) System.out.println("read " + filenameIn + " len = " + lenIn); InputStream in = new FileInputStream(filenameIn); long start = System.currentTimeMillis(); IO.copy2null(in, buffer); double took = .001 * (System.currentTimeMillis() - start); in.close(); double rate = lenIn / took / (1000 * 1000); System.out.println( " copy (" + filenameIn + ") took = " + took + " sec; rate = " + rate + "Mb/sec"); }
/** * 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); }
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; }
public static String streamToString(InputStream is) throws IOException { if (is != null) { StringBuilder sb = new StringBuilder(); String line; try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } } finally { is.close(); } return sb.toString(); } else { return ""; } }