/** * Closes input stream if opened. */ void closeInputStreamIfOpen() { if (inputStream == null) { return; } StreamUtil.close(inputStream); inputStream = null; }
public File toZipFile() { StreamUtil.close(zos); return targetZipFile; }
/** * Implements the Unix "touch" utility. It creates a new file * with size 0 or, if the file exists already, it is opened and * closed without modifying it, but updating the file date and time. */ public static void touch(File file) throws IOException { if (!file.exists()) { StreamUtil.close(new FileOutputStream(file)); } file.setLastModified(System.currentTimeMillis()); }
/** * Loads properties from the file. Properties are appended to the existing * properties object. * * @param p properties to fill in * @param file file to read properties from */ public static void loadFromFile(Properties p, File file) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(file); p.load(fis); } finally { StreamUtil.close(fis); } }
/** * Writes properties to a file. * * @param p properties to write to file * @param file destination file * @param header optional header */ public static void writeToFile(Properties p, File file, String header) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(file); p.store(fos, header); } finally { StreamUtil.close(fos); } }
/** * De-serialize an object from byte array. */ public static Object byteArrayToObject(byte[] data) throws IOException, ClassNotFoundException { Object retObj = null; ByteArrayInputStream bais = new ByteArrayInputStream(data); ObjectInputStream ois = null; try { ois = new ObjectInputStream(bais); retObj = ois.readObject(); } finally { StreamUtil.close(ois); } return retObj; }
protected static void outBytes(File dest, byte[] data, int off, int len, boolean append) throws IOException { if (dest.exists()) { if (!dest.isFile()) { throw new IOException(MSG_NOT_A_FILE + dest); } } FileOutputStream out = null; try { out = new FileOutputStream(dest, append); out.write(data, off, len); } finally { StreamUtil.close(out); } }
protected static void outString(File dest, String data, String encoding, boolean append) throws IOException { if (dest.exists()) { if (!dest.isFile()) { throw new IOException(MSG_NOT_A_FILE + dest); } } FileOutputStream out = null; try { out = new FileOutputStream(dest, append); out.write(data.getBytes(encoding)); } finally { StreamUtil.close(out); } }
protected static void outChars(File dest, char[] data, String encoding, boolean append) throws IOException { if (dest.exists()) { if (!dest.isFile()) { throw new IOException(MSG_NOT_A_FILE + dest); } } Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest, append), encoding)); try { out.write(data); } finally { StreamUtil.close(out); } }
/** * Implements the Unix "touch" utility. It creates a new {@link File} * with size 0 or, if the {@link File} exists already, it is opened and * closed without modifying it, but updating the {@link File} date and time. */ public static void touch(final File file) throws IOException { if (!file.exists()) { StreamUtil.close(new FileOutputStream(file, false)); } file.setLastModified(System.currentTimeMillis()); }
public static void writeStream(String dest, InputStream in) throws IOException { FileOutputStream out = null; try { out = new FileOutputStream(dest); StreamUtil.copy(in, out); } finally { StreamUtil.close(out); } }
public static void writeStream(File dest, InputStream in) throws IOException { FileOutputStream out = null; try { out = new FileOutputStream(dest); StreamUtil.copy(in, out); } finally { StreamUtil.close(out); } }
public byte[] toBytes() { StreamUtil.close(zos); if (targetZipFile != null) { try { return FileUtil.readBytes(targetZipFile); } catch (IOException ignore) { return null; } } return targetBaos.toByteArray(); }
/** * Decompress gzip archive. */ public static File ungzip(File file) throws IOException { String outFileName = FileNameUtil.removeExtension(file.getAbsolutePath()); File out = new File(outFileName); out.createNewFile(); FileOutputStream fos = new FileOutputStream(out); GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(file)); try { StreamUtil.copy(gzis, fos); } finally { StreamUtil.close(fos); StreamUtil.close(gzis); } return out; }
/** * Calculates digest for a file using provided algorithm. */ public static byte[] digest(final File file, MessageDigest algorithm) throws IOException { algorithm.reset(); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); DigestInputStream dis = new DigestInputStream(bis, algorithm); try { while (dis.read() != -1) { } } finally { StreamUtil.close(fis); } return algorithm.digest(); }
public byte[] toBytes() { StreamUtil.close(zos); if (targetZipFile != null) { try { return FileUtil.readBytes(targetZipFile); } catch (IOException ignore) { return null; } } return targetBaos.toByteArray(); }
/** * Serialize an object to byte array. */ public static byte[] objectToByteArray(Object obj) throws IOException { FastByteArrayOutputStream bos = new FastByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(bos); oos.writeObject(obj); } finally { StreamUtil.close(oos); } return bos.toByteArray(); }
@Test void close_with_closeable_instance() { final MyCloseable input = new MyCloseable(); StreamUtil.close(input); // asserts assertTrue(input.closed); assertFalse(input.flushed); }