/** * Reads the input stream until end-of-input and writes all data to the output stream. Closes none of the two * streams. * * @return The number of bytes copied */ public static long copy(InputStream inputStream, OutputStream outputStream) throws IOException { return IoUtil.copy(inputStream, outputStream, Long.MAX_VALUE); }
/** * Equivalent with {@link #outputFilePrintWriter(File, Charset, ConsumerWhichThrows, boolean) * printToFile(<var>file</var>, <var>charset</var>, <var>printer</var>, false)}. */ public static <EX extends Throwable> void outputFilePrintWriter( File file, Charset charset, ConsumerWhichThrows<? super PrintWriter, ? extends EX> printer ) throws IOException, EX { IoUtil.outputFilePrintWriter(file, charset, printer, false); }
/** * Reads data from the input stream and writes it to the output stream. Closes none of the two streams. Returns * when no data is {@link InputStream#available() available} on the <var>inputStream</var>. * * @return The number of bytes copied */ public static long copyAvailable(InputStream inputStream, OutputStream outputStream) throws IOException { return IoUtil.copyAvailable(inputStream, outputStream, Long.MAX_VALUE); }
copy(File inputFile, File outputFile, CollisionStrategy collisionStrategy) throws IOException { if (!outputFile.exists()) return IoUtil.copy(inputFile, outputFile); return IoUtil.copy(inputFile, outputFile); if (!IoUtil.isContentIdentical(inputFile, outputFile)) { throw new IOException("File \"" + outputFile + "\" already exists with non-identical content");
@Override public void writeEntry( final ArchiveOutputStream archiveOutputStream, final String name, final ConsumerWhichThrows<? super OutputStream, ? extends IOException> writeContents ) throws IOException { if (!(archiveOutputStream instanceof ArArchiveOutputStream)) { throw new IllegalArgumentException(archiveOutputStream.getClass().getName()); } // Copy the contents to a temporary storage in order to count the bytes. InputOutputStreams ios = PipeUtil.asInputOutputStreams(PipeFactory.elasticPipe()); try { long count = IoUtil.writeAndCount(writeContents, ios.getOutputStream()); ios.getOutputStream().close(); archiveOutputStream.putArchiveEntry(new ArArchiveEntry(name, count)); IoUtil.copy(ios.getInputStream(), archiveOutputStream); ios.getInputStream().close(); archiveOutputStream.closeArchiveEntry(); } finally { try { ios.getOutputStream().close(); } catch (Exception e) {} try { ios.getInputStream().close(); } catch (Exception e) {} } }
private static void copySubtree(Map<String, URL> rs, String namePrefix, File destinationDirectory, CollisionStrategy collisionStrategy) throws IOException { assert rs.get(namePrefix) != null; if (!destinationDirectory.mkdir()) { throw new IOException("Could not create destination directory \"" + destinationDirectory + "\""); } for (Entry<String, URL> e : rs.entrySet()) { String name = e.getKey(); URL location = e.getValue(); if (!name.startsWith(namePrefix)) continue; int npl = namePrefix.length(); if (name.length() == npl) continue; int idx = name.indexOf('/', npl); if (idx == -1) { IoUtil.copy( location.openStream(), // inputStream true, // closeInputStream new File(destinationDirectory, name.substring(npl)) // outputFile ); } else if (idx == name.length() - 1) { IoUtil.copySubtree(rs, name, new File(destinationDirectory, name.substring(npl)), collisionStrategy); } } }
@Override public Void call() throws Exception { // Run the task. try { task.consume(IoUtil.unclosableOutputStream(previousTarget)); } catch (Exception ex) { throw ex; } catch (Error er) { // SUPPRESS CHECKSTYLE IllegalCatch throw er; } catch (Throwable th) { // SUPPRESS CHECKSTYLE IllegalCatch throw new Exception(th); } synchronized (pos) { // Copy the buffered products of the FOLLOWING tasks to the output. IoUtil.copyAvailable(ios.getInputStream(), previousTarget); // Redirect FUTURE output of the FOLLOWING tasks directly to MY output. pos.setDelegate(previousTarget); buffer.close(); } return null; } });
/** * Copies the contents a resource to the given <var>toFile</var>. * <p> * The resource is addressed by the <var>classLoader</var> and the <var>resourceName</var>, as described for * {@link ClassLoader#getResourceAsStream(String)}. * </p> * * @param createMissingParentDirectories Whether to create any missing parent directories for the <var>toFile</var> */ public static void copyResource( final ClassLoader classLoader, final String resourceName, File toFile, boolean createMissingParentDirectories ) throws IOException { IoUtil.outputFileOutputStream( toFile, new ConsumerWhichThrows<OutputStream, IOException>() { @Override public void consume(OutputStream outputStream) throws IOException { IoUtil.copyResource(classLoader, resourceName, outputStream, false); } }, createMissingParentDirectories ); }
@Override public void handleConnection( InputStream clientIn, OutputStream clientOut, InputStream serverIn, OutputStream serverOut, InetSocketAddress clientLocalSocketAddress, InetSocketAddress clientRemoteSocketAddress, InetSocketAddress serverLocalSocketAddress, InetSocketAddress serverRemoteSocketAddress, Stoppable stoppable ) throws IOException { ThreadUtil.parallel( IoUtil.copyRunnable(clientIn, serverOut), IoUtil.copyRunnable(serverIn, clientOut), stoppable ); } }
@Override public void handleConnection( InputStream clientIn, OutputStream clientOut, InputStream serverIn, OutputStream serverOut, InetSocketAddress clientLocalSocketAddress, InetSocketAddress clientRemoteSocketAddress, InetSocketAddress serverLocalSocketAddress, InetSocketAddress serverRemoteSocketAddress, Stoppable stoppable ) throws IOException { ThreadUtil.parallel( IoUtil.copyRunnable(clientIn, IoUtil.tee( serverOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "> ")) )), IoUtil.copyRunnable(serverIn, IoUtil.tee( clientOut, new HexOutputStream(LogUtil.logWriter(LOGGER, FINER, "< ")) )), stoppable ); } }
@Override public void consume(OutputStream outputStream) throws IOException { IoUtil.copyResource(classLoader, resourceName, outputStream, false); } },
/** * @return Whether the byte sequences produced by the <var>stream</var> is identical with the contents of the * <var>file</var> */ private static boolean isContentIdentical(InputStream stream, File file) throws FileNotFoundException, IOException { InputStream fis = new FileInputStream(file); try { boolean result = IoUtil.isContentIdentical(stream, fis); fis.close(); return result; } finally { try { fis.close(); } catch (Exception e) {} } }
) throws IOException, EX { if (createMissingParentDirectories) IoUtil.createMissingParentDirectoriesFor(file);
IoUtil.outputFile( file, new ConsumerWhichThrows<File, EX>() {
copy(File inputFile, File outputFile, CollisionStrategy collisionStrategy) throws IOException { if (!outputFile.exists()) return IoUtil.copy(inputFile, outputFile); return IoUtil.copy(inputFile, outputFile); if (!IoUtil.isContentIdentical(inputFile, outputFile)) { throw new IOException("File \"" + outputFile + "\" already exists with non-identical content");
long count = IoUtil.writeAndCount(writeContents, ios.getOutputStream()); ios.getOutputStream().close(); archiveOutputStream.putArchiveEntry(naae); IoUtil.copy(ios.getInputStream(), archiveOutputStream); ios.getInputStream().close();
/** * Copies a resource tree to a directory in the file system. * <p> * Iff the <var>source</var> is a "content resource", then {@link IoUtil#copy(InputStream, boolean, File)} is * called. * </p> * <p> * Otherwise, the <var>destination</var> is created as a directory (if it did not exist), and all members * of the <var>source</var> directory are recursively copied to the <var>destination</var> directory. * </p> * <p> * The parent directory for the <var>destination</var> must already exist. * </p> */ public static void copyTree(URL source, File destination, CollisionStrategy collisionStrategy) throws IOException { Map<String, URL> rs = ClassLoaders.getSubresourcesOf(source, "", true); if (rs.isEmpty()) return; if (rs.size() == 1) { IoUtil.copy( rs.values().iterator().next().openStream(), // inputStream true, // closeInputStream destination, // outputFile collisionStrategy // collisionStrategy ); return; } IoUtil.copySubtree(rs, "", destination, collisionStrategy); }
/** * Copies the contents a resource to the given <var>toFile</var>. * <p> * The resource is addressed by the <var>clasS</var> and the <var>resourceName</var>, as described for {@link * Class#getResourceAsStream(String)}. * </p> * * @param resourceName Must not start with a "/" * @param createMissingParentDirectories Whether to create any missing parent directories for the <var>toFile</var> */ public static void copyResource( final Class<?> clasS, final String resourceName, File toFile, boolean createMissingParentDirectories ) throws IOException { IoUtil.outputFileOutputStream( toFile, new ConsumerWhichThrows<OutputStream, IOException>() { @Override public void consume(OutputStream outputStream) throws IOException { IoUtil.copyResource(clasS, resourceName, outputStream, false); } }, createMissingParentDirectories ); }