org.apache.commons.compress.utils.IOUtils.copy()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(318)

本文整理了Java中org.apache.commons.compress.utils.IOUtils.copy()方法的一些代码示例,展示了IOUtils.copy()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.copy()方法的具体详情如下:
包路径:org.apache.commons.compress.utils.IOUtils
类名称:IOUtils
方法名:copy

IOUtils.copy介绍

[英]Copies the content of a InputStream into an OutputStream. Uses a default buffer size of 8024 bytes.
[中]将InputStream的内容复制到OutputStream。使用默认缓冲区大小8024字节。

代码示例

代码示例来源:origin: org.apache.commons/commons-compress

  1. @Override
  2. public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
  3. IOUtils.copy(archive, out);
  4. }
  5. }, targetDirectory);

代码示例来源:origin: org.apache.commons/commons-compress

  1. /**
  2. * Copies the content of a InputStream into an OutputStream.
  3. * Uses a default buffer size of 8024 bytes.
  4. *
  5. * @param input
  6. * the InputStream to copy
  7. * @param output
  8. * the target Stream
  9. * @return the number of bytes copied
  10. * @throws IOException
  11. * if an error occurs
  12. */
  13. public static long copy(final InputStream input, final OutputStream output) throws IOException {
  14. return copy(input, output, COPY_BUF_SIZE);
  15. }

代码示例来源:origin: apache/storm

  1. private File loadExample(File file, String example) {
  2. try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(example)) {
  3. file = File.createTempFile("pmml-example", ".tmp");
  4. IOUtils.copy(stream, new FileOutputStream(file));
  5. } catch (IOException e) {
  6. throw new RuntimeException("Error loading example " + example, e);
  7. }
  8. return file;
  9. }

代码示例来源:origin: runelite/runelite

  1. public static byte[] compress(byte[] bytes) throws IOException
  2. {
  3. InputStream is = new ByteArrayInputStream(bytes);
  4. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  5. try (OutputStream os = new GZIPOutputStream(bout))
  6. {
  7. IOUtils.copy(is, os);
  8. }
  9. return bout.toByteArray();
  10. }

代码示例来源:origin: runelite/runelite

  1. public static byte[] decompress(byte[] bytes, int len) throws IOException
  2. {
  3. ByteArrayOutputStream os = new ByteArrayOutputStream();
  4. try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes, 0, len)))
  5. {
  6. IOUtils.copy(is, os);
  7. }
  8. return os.toByteArray();
  9. }
  10. }

代码示例来源:origin: org.apache.commons/commons-compress

  1. /**
  2. * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>.
  3. * <p>
  4. * This method buffers the input internally, so there is no need to use a
  5. * <code>BufferedInputStream</code>.
  6. *
  7. * @param input the <code>InputStream</code> to read from
  8. * @return the requested byte array
  9. * @throws NullPointerException if the input is null
  10. * @throws IOException if an I/O error occurs
  11. * @since 1.5
  12. */
  13. public static byte[] toByteArray(final InputStream input) throws IOException {
  14. final ByteArrayOutputStream output = new ByteArrayOutputStream();
  15. copy(input, output);
  16. return output.toByteArray();
  17. }

代码示例来源:origin: testcontainers/testcontainers-java

  1. /**
  2. * {@inheritDoc}
  3. */
  4. @Override
  5. public void copyFileFromContainer(String containerPath, String destinationPath) {
  6. copyFileFromContainer(containerPath, inputStream -> {
  7. try(FileOutputStream output = new FileOutputStream(destinationPath)) {
  8. IOUtils.copy(inputStream, output);
  9. return null;
  10. }
  11. });
  12. }

代码示例来源:origin: org.apache.commons/commons-compress

  1. @Override
  2. public void writeEntryDataTo(ArchiveEntry entry, OutputStream out) throws IOException {
  3. try (InputStream in = archive.getInputStream((ZipArchiveEntry) entry)) {
  4. IOUtils.copy(in, out);
  5. }
  6. }
  7. }, targetDirectory);

代码示例来源:origin: runelite/runelite

  1. public static byte[] decompress(byte[] bytes, int len) throws IOException
  2. {
  3. byte[] data = new byte[len + BZIP_HEADER.length];
  4. // add header
  5. System.arraycopy(BZIP_HEADER, 0, data, 0, BZIP_HEADER.length);
  6. System.arraycopy(bytes, 0, data, BZIP_HEADER.length, len);
  7. ByteArrayOutputStream os = new ByteArrayOutputStream();
  8. try (InputStream is = new BZip2CompressorInputStream(new ByteArrayInputStream(data)))
  9. {
  10. IOUtils.copy(is, os);
  11. }
  12. return os.toByteArray();
  13. }
  14. }

代码示例来源:origin: runelite/runelite

  1. public static byte[] compress(byte[] bytes) throws IOException
  2. {
  3. InputStream is = new ByteArrayInputStream(bytes);
  4. ByteArrayOutputStream bout = new ByteArrayOutputStream();
  5. try (OutputStream os = new BZip2CompressorOutputStream(bout, 1))
  6. {
  7. IOUtils.copy(is, os);
  8. }
  9. byte[] out = bout.toByteArray();
  10. assert BZIP_HEADER[0] == out[0];
  11. assert BZIP_HEADER[1] == out[1];
  12. assert BZIP_HEADER[2] == out[2];
  13. assert BZIP_HEADER[3] == out[3];
  14. return Arrays.copyOfRange(out, BZIP_HEADER.length, out.length); // remove header..
  15. }

代码示例来源:origin: jeremylong/DependencyCheck

  1. /**
  2. * Decompresses a file.
  3. *
  4. * @param inputStream the compressed file
  5. * @param outputFile the location to write the decompressed file
  6. * @throws ArchiveExtractionException thrown if there is an exception
  7. * decompressing the file
  8. */
  9. private void decompressFile(CompressorInputStream inputStream, File outputFile) throws ArchiveExtractionException {
  10. LOGGER.debug("Decompressing '{}'", outputFile.getPath());
  11. try (FileOutputStream out = new FileOutputStream(outputFile)) {
  12. IOUtils.copy(inputStream, out);
  13. } catch (IOException ex) {
  14. LOGGER.debug("", ex);
  15. throw new ArchiveExtractionException(ex);
  16. }
  17. }

代码示例来源:origin: apache/avro

  1. @Override
  2. public ByteBuffer decompress(ByteBuffer compressedData) throws IOException {
  3. ByteArrayOutputStream baos = getOutputBuffer(compressedData.remaining());
  4. InputStream bytesIn = new ByteArrayInputStream(
  5. compressedData.array(),
  6. computeOffset(compressedData),
  7. compressedData.remaining());
  8. try (InputStream ios = new ZstdCompressorInputStream(bytesIn)) {
  9. IOUtils.copy(ios, baos);
  10. }
  11. return ByteBuffer.wrap(baos.toByteArray());
  12. }

代码示例来源:origin: org.apache.avro/avro

  1. @Override
  2. public ByteBuffer decompress(ByteBuffer data) throws IOException {
  3. ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
  4. InputStream bytesIn = new ByteArrayInputStream(
  5. data.array(),
  6. data.arrayOffset() + data.position(),
  7. data.remaining());
  8. InputStream ios = new XZCompressorInputStream(bytesIn);
  9. try {
  10. IOUtils.copy(ios, baos);
  11. } finally {
  12. ios.close();
  13. }
  14. return ByteBuffer.wrap(baos.toByteArray());
  15. }

代码示例来源:origin: apache/avro

  1. @Override
  2. public ByteBuffer decompress(ByteBuffer data) throws IOException {
  3. ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
  4. InputStream bytesIn = new ByteArrayInputStream(
  5. data.array(),
  6. computeOffset(data),
  7. data.remaining());
  8. try (InputStream ios = new XZCompressorInputStream(bytesIn)) {
  9. IOUtils.copy(ios, baos);
  10. }
  11. return ByteBuffer.wrap(baos.toByteArray());
  12. }

代码示例来源:origin: org.apache.commons/commons-compress

  1. public void accept(File source, ArchiveEntry e) throws IOException {
  2. target.putArchiveEntry(e);
  3. if (!e.isDirectory()) {
  4. try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
  5. IOUtils.copy(in, target);
  6. }
  7. }
  8. target.closeArchiveEntry();
  9. }
  10. }, new Finisher() {

代码示例来源:origin: cSploit/android

  1. private void movePcapFileFromCacheToStorage() {
  2. File inputFile = new File(mPcapFileName);
  3. InputStream in = null;
  4. OutputStream out = null;
  5. try {
  6. in = new FileInputStream(inputFile);
  7. out = new FileOutputStream(new File(System.getStoragePath(),new File(mPcapFileName).getName()));
  8. IOUtils.copy(in, out);
  9. } catch (IOException e) {
  10. System.errorLogging(e);
  11. } finally {
  12. IOUtils.closeQuietly(in);
  13. IOUtils.closeQuietly(out);
  14. inputFile.delete();
  15. }
  16. }

代码示例来源:origin: apache/hive

  1. public static void zip(String parentDir, String[] inputFiles, String outputFile)
  2. throws IOException {
  3. ZipOutputStream output = null;
  4. try {
  5. output = new ZipOutputStream(new FileOutputStream(new File(parentDir, outputFile)));
  6. for (int i = 0; i < inputFiles.length; i++) {
  7. File f = new File(parentDir, inputFiles[i]);
  8. FileInputStream input = new FileInputStream(f);
  9. output.putNextEntry(new ZipEntry(inputFiles[i]));
  10. try {
  11. IOUtils.copy(input, output);
  12. } finally {
  13. input.close();
  14. }
  15. }
  16. } finally {
  17. org.apache.hadoop.io.IOUtils.closeStream(output);
  18. }
  19. }

代码示例来源:origin: apache/incubator-gobblin

  1. /**
  2. * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that adds a file entry to a given
  3. * {@link TarArchiveOutputStream} and copies the contents of the file to the new entry.
  4. */
  5. private static void fileToTarArchiveOutputStream(FileStatus fileStatus, FSDataInputStream fsDataInputStream,
  6. Path destFile, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {
  7. TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(formatPathToFile(destFile));
  8. tarArchiveEntry.setSize(fileStatus.getLen());
  9. tarArchiveEntry.setModTime(System.currentTimeMillis());
  10. tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);
  11. try {
  12. IOUtils.copy(fsDataInputStream, tarArchiveOutputStream);
  13. } finally {
  14. tarArchiveOutputStream.closeArchiveEntry();
  15. }
  16. }

代码示例来源:origin: azkaban/azkaban

  1. private static File decompressTarBZ2(InputStream is) throws IOException {
  2. File outputDir = Files.createTempDir();
  3. try (TarArchiveInputStream tais = new TarArchiveInputStream(
  4. new BZip2CompressorInputStream(is))) {
  5. TarArchiveEntry entry;
  6. while ((entry = tais.getNextTarEntry()) != null) {
  7. if (entry.isDirectory()) {
  8. continue;
  9. }
  10. File outputFile = new File(outputDir, entry.getName());
  11. File parent = outputFile.getParentFile();
  12. if (!parent.exists()) {
  13. parent.mkdirs();
  14. }
  15. try (FileOutputStream os = new FileOutputStream(outputFile)) {
  16. IOUtils.copy(tais, os);
  17. }
  18. }
  19. return outputDir;
  20. }
  21. }

代码示例来源:origin: Alluxio/alluxio

  1. /**
  2. * @param command the move command to execute
  3. * @param writeType the write type to use for the moved file
  4. * @param fileSystem the Alluxio file system
  5. */
  6. private static void move(MoveCommand command, WritePType writeType, FileSystem fileSystem)
  7. throws Exception {
  8. String source = command.getSource();
  9. String destination = command.getDestination();
  10. LOG.debug("Moving {} to {}", source, destination);
  11. CreateFilePOptions createOptions =
  12. CreateFilePOptions.newBuilder().setWriteType(writeType).build();
  13. try (FileOutStream out = fileSystem.createFile(new AlluxioURI(destination), createOptions)) {
  14. try (FileInStream in = fileSystem.openFile(new AlluxioURI(source))) {
  15. IOUtils.copy(in, out);
  16. } catch (Throwable t) {
  17. try {
  18. out.cancel();
  19. } catch (Throwable t2) {
  20. t.addSuppressed(t2);
  21. }
  22. throw t;
  23. }
  24. }
  25. fileSystem.delete(new AlluxioURI(source));
  26. }

相关文章