/** * 在InputStream与OutputStream间复制内容 * * @see {@link ByteStreams#copy} */ public static long copy(final InputStream input, final OutputStream output) throws IOException { return ByteStreams.copy(input, output); }
/** * 在InputStream与OutputStream间复制内容 * * @see {@link ByteStreams#copy} */ public static long copy(final InputStream input, final OutputStream output) throws IOException { return ByteStreams.copy(input, output); }
private void pushTaskFile(Path path, File logFile) throws IOException { final FileSystem fs = path.getFileSystem(hadoopConfig); try ( final InputStream in = new FileInputStream(logFile); final OutputStream out = fs.create(path, true) ) { ByteStreams.copy(in, out); } }
private static StreamingOutput wrapWithStreamingOutput(final InputStream inputStream) { return os -> { OutputStream wrappedOutputStream = os; if (!(os instanceof BufferedOutputStream)) { wrappedOutputStream = new BufferedOutputStream(os); } ByteStreams.copy(inputStream, wrappedOutputStream); wrappedOutputStream.flush(); }; }
@Override public void writeTo(WritableByteChannel channel) throws IOException { flush(); ch.position(0); try { ByteStreams.copy(ch, channel); } finally { ch.position(ch.size()); } }
private static void skipRequestBody(HttpServletRequest request) throws IOException { // If we send the challenge without consuming the body of the request, // the server will close the connection after sending the response. // The client may interpret this as a failed request and not resend the // request with the authentication header. We can avoid this behavior // in the client by reading and discarding the entire body of the // unauthenticated request before sending the response. try (InputStream inputStream = request.getInputStream()) { copy(inputStream, nullOutputStream()); } } }
/** * Writes all the bytes from the given {@code InputStream} to this sink. Does not close {@code * input}. * * @return the number of bytes written * @throws IOException if an I/O occurs while reading from {@code input} or writing to this sink */ @CanIgnoreReturnValue public long writeFrom(InputStream input) throws IOException { checkNotNull(input); Closer closer = Closer.create(); try { OutputStream out = closer.register(openStream()); long written = ByteStreams.copy(input, out); out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 return written; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Copies the contents of this byte source to the given {@code OutputStream}. Does not close * {@code output}. * * @return the number of bytes copied * @throws IOException if an I/O error occurs while reading from this source or writing to {@code * output} */ @CanIgnoreReturnValue public long copyTo(OutputStream output) throws IOException { checkNotNull(output); Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); return ByteStreams.copy(in, output); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Copies the contents of this byte source to the given {@code ByteSink}. * * @return the number of bytes copied * @throws IOException if an I/O error occurs while reading from this source or writing to {@code * sink} */ @CanIgnoreReturnValue public long copyTo(ByteSink sink) throws IOException { checkNotNull(sink); Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); OutputStream out = closer.register(sink.openStream()); return ByteStreams.copy(in, out); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void testCopyChannel() throws IOException { byte[] expected = newPreFilledByteArray(100); ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); ReadableByteChannel inChannel = Channels.newChannel(new ByteArrayInputStream(expected)); ByteStreams.copy(inChannel, outChannel); assertEquals(expected, out.toByteArray()); }
public void testOpenBufferedStream() throws IOException { OutputStream out = sink.openBufferedStream(); try { ByteStreams.copy(new ByteArrayInputStream(data), out); } finally { out.close(); } assertContainsExpectedBytes(); }
public void testOpenStream() throws IOException { OutputStream out = sink.openStream(); try { ByteStreams.copy(new ByteArrayInputStream(data), out); } finally { out.close(); } assertContainsExpectedBytes(); }
public static void assertGoodDataStream(InputStream stream) throws IOException { try (final ByteArrayOutputStream bos = new ByteArrayOutputStream(expected.length)) { ByteStreams.copy(stream, bos); Assert.assertArrayEquals(expected, bos.toByteArray()); } }
public void testCopyFileChannel() throws IOException { final int chunkSize = 14407; // Random prime, unlikely to match any internal chunk size ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); File testFile = createTempFile(); FileOutputStream fos = new FileOutputStream(testFile); byte[] dummyData = newPreFilledByteArray(chunkSize); try { for (int i = 0; i < 500; i++) { fos.write(dummyData); } } finally { fos.close(); } ReadableByteChannel inChannel = new RandomAccessFile(testFile, "r").getChannel(); try { ByteStreams.copy(inChannel, outChannel); } finally { inChannel.close(); } byte[] actual = out.toByteArray(); for (int i = 0; i < 500 * chunkSize; i += chunkSize) { assertEquals(dummyData, Arrays.copyOfRange(actual, i, i + chunkSize)); } }
@Test public void testDecompressZip() throws IOException { final File tmpDir = temporaryFolder.newFolder("testDecompressZip"); final File zipFile = new File(tmpDir, testFile.getName() + ".zip"); Assert.assertFalse(zipFile.exists()); try (final ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) { out.putNextEntry(new ZipEntry("cool.file")); ByteStreams.copy(new FileInputStream(testFile), out); out.closeEntry(); } try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(zipFile), zipFile.getName())) { assertGoodDataStream(inputStream); } }
@Test public void testDecompressZstd() throws IOException { final File tmpDir = temporaryFolder.newFolder("testDecompressZstd"); final File zstdFile = new File(tmpDir, testFile.getName() + ".zst"); Assert.assertFalse(zstdFile.exists()); try (final OutputStream out = new ZstdCompressorOutputStream(new FileOutputStream(zstdFile))) { ByteStreams.copy(new FileInputStream(testFile), out); } try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(zstdFile), zstdFile.getName())) { assertGoodDataStream(inputStream); } }
@Test public void testDecompressSnappy() throws IOException { final File tmpDir = temporaryFolder.newFolder("testDecompressSnappy"); final File snappyFile = new File(tmpDir, testFile.getName() + ".sz"); Assert.assertFalse(snappyFile.exists()); try (final OutputStream out = new FramedSnappyCompressorOutputStream(new FileOutputStream(snappyFile))) { ByteStreams.copy(new FileInputStream(testFile), out); } try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(snappyFile), snappyFile.getName())) { assertGoodDataStream(inputStream); } }
@Test public void testDecompressBzip2() throws IOException { final File tmpDir = temporaryFolder.newFolder("testDecompressBzip2"); final File bzFile = new File(tmpDir, testFile.getName() + ".bz2"); Assert.assertFalse(bzFile.exists()); try (final OutputStream out = new BZip2CompressorOutputStream(new FileOutputStream(bzFile))) { ByteStreams.copy(new FileInputStream(testFile), out); } try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(bzFile), bzFile.getName())) { assertGoodDataStream(inputStream); } }
@Test public void testDecompressXz() throws IOException { final File tmpDir = temporaryFolder.newFolder("testDecompressXz"); final File xzFile = new File(tmpDir, testFile.getName() + ".xz"); Assert.assertFalse(xzFile.exists()); try (final OutputStream out = new XZCompressorOutputStream(new FileOutputStream(xzFile))) { ByteStreams.copy(new FileInputStream(testFile), out); } try (final InputStream inputStream = CompressionUtils.decompress(new FileInputStream(xzFile), xzFile.getName())) { assertGoodDataStream(inputStream); } }
public void testOpenBufferedStream() throws IOException { InputStream in = source.openBufferedStream(); assertTrue(source.wasStreamOpened()); assertFalse(source.wasStreamClosed()); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(in, out); in.close(); out.close(); assertTrue(source.wasStreamClosed()); assertArrayEquals(bytes, out.toByteArray()); }