public static File writeFile(String content) { try { return writeFile(File.createTempFile("h2o", null), content); } catch( IOException e ) { throw Log.errRTExcept(e); } }
public static String readConsole() { BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); try { return console.readLine(); } catch( IOException e ) { throw Log.errRTExcept(e); } }
public static File writeFile(File file, String content) { FileWriter w = null; try { w = new FileWriter(file); w.write(content); } catch(IOException e) { Log.errRTExcept(e); } finally { close(w); } return file; }
public static String readFile(File file) { FileReader r = null; try { r = new FileReader(file); char[] data = new char[(int) file.length()]; r.read(data); return new String(data); } catch(IOException e) { throw Log.errRTExcept(e); } finally { close(r); } }
public static void writeFileAndClose(File file, InputStream in) { OutputStream out = null; try { out = new FileOutputStream(file); byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len > 0) { out.write(buffer, 0, len); len = in.read(buffer); } } catch(IOException e) { throw Log.errRTExcept(e); } finally { close(in, out); } }
public static byte [] unzipBytes(byte [] bs, Compression cmp) { InputStream is = null; int off = 0; try { switch(cmp) { case NONE: // No compression return bs; case ZIP: { ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bs)); ZipEntry ze = zis.getNextEntry(); // Get the *FIRST* entry // There is at least one entry in zip file and it is not a directory. if( ze != null && !ze.isDirectory() ) { is = zis; break; } zis.close(); return bs; // Don't crash, ignore file if cannot unzip } case GZIP: is = new GZIPInputStream(new ByteArrayInputStream(bs)); break; default: assert false:"cmp = " + cmp; } // If reading from a compressed stream, estimate we can read 2x uncompressed assert( is != null ):"is is NULL, cmp = " + cmp; bs = new byte[bs.length * 2]; // Now read from the (possibly compressed) stream while( off < bs.length ) { int len = is.read(bs, off, bs.length - off); if( len < 0 ) break; off += len; if( off == bs.length ) { // Dataset is uncompressing alot! Need more space... if( bs.length >= ValueArray.CHUNK_SZ ) break; // Already got enough bs = Arrays.copyOf(bs, bs.length * 2); } } } catch( IOException ioe ) { // Stop at any io error Log.err(ioe); } finally { Utils.close(is); } return bs; }
public static void readFile(File file, OutputStream out) { BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024]; while( true ) { int count = in.read(buffer); if( count == -1 ) break; out.write(buffer, 0, count); } } catch(IOException e) { throw Log.errRTExcept(e); } finally { close(in); } }