/** * Copy input stream to output stream without closing streams. Flushes output stream when done. * * @param is input stream * @param os output stream * @throws IOException for any error */ public static void copyStream(InputStream is, OutputStream os) throws IOException { copyStream(is, os, DEFAULT_BUFFER_SIZE); }
/** * Copy input stream to output stream and close them both * * @param is input stream * @param os output stream * @param bufferSize the buffer size to use * @throws IOException for any error */ public static void copyStreamAndClose(InputStream is, OutputStream os, int bufferSize) throws IOException { try { copyStream(is, os, bufferSize); // throw an exception if the close fails since some data might be lost is.close(); os.close(); } finally { // ...but still guarantee that they're both closed safeClose(is); safeClose(os); } }
/** * Create a file within this temporary directory, prepopulating the file from the given input stream. * * @param relativePath the relative path name * @param sourceData the source input stream to use * @return the file * @throws IOException if the directory was closed at the time of this invocation or an error occurs */ public File createFile(String relativePath, InputStream sourceData) throws IOException { final File tempFile = getFile(relativePath); boolean ok = false; try { final FileOutputStream fos = new FileOutputStream(tempFile); try { VFSUtils.copyStream(sourceData, fos); fos.close(); sourceData.close(); ok = true; return tempFile; } finally { VFSUtils.safeClose(fos); } } finally { VFSUtils.safeClose(sourceData); if (!ok) { tempFile.delete(); } } }
final FileOutputStream os = new FileOutputStream(current); try { VFSUtils.copyStream(is, os);
@Override public URL getStreamURL() throws IOException { if (vfsFile.isFile() == true) return vfsFile.toURL(); synchronized (this) { if (streamFile == null) { streamDir = tmpProvider.createTempDir("urlstream"); streamFile = streamDir.getFile(getName()); JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(streamFile)); VirtualJarInputStream jarIn = (VirtualJarInputStream) vfsFile.openStream(); ZipEntry nextEntry = jarIn.getNextEntry(); while (nextEntry != null) { jarOut.putNextEntry(nextEntry); VFSUtils.copyStream(jarIn, jarOut); nextEntry = jarIn.getNextEntry(); } jarOut.close(); jarIn.close(); } } return streamFile.toURI().toURL(); }
try { VFSUtils.copyStream(zipData, os); zipData.close(); os.close();