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

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

本文整理了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

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

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

/**
 * Copies the content of a InputStream into an OutputStream.
 * Uses a default buffer size of 8024 bytes.
 *
 * @param input
 *            the InputStream to copy
 * @param output
 *            the target Stream
 * @return the number of bytes copied
 * @throws IOException
 *             if an error occurs
 */
public static long copy(final InputStream input, final OutputStream output) throws IOException {
  return copy(input, output, COPY_BUF_SIZE);
}

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

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

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

public static byte[] compress(byte[] bytes) throws IOException
{
  InputStream is = new ByteArrayInputStream(bytes);
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  try (OutputStream os = new GZIPOutputStream(bout))
  {
    IOUtils.copy(is, os);
  }
  
  return bout.toByteArray();
}

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

public static byte[] decompress(byte[] bytes, int len) throws IOException
  {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    
    try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes, 0, len)))
    {
      IOUtils.copy(is, os);
    }

    return os.toByteArray();
  }
}

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

/**
 * Gets the contents of an <code>InputStream</code> as a <code>byte[]</code>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 *
 * @param input  the <code>InputStream</code> to read from
 * @return the requested byte array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since 1.5
 */
public static byte[] toByteArray(final InputStream input) throws IOException {
  final ByteArrayOutputStream output = new ByteArrayOutputStream();
  copy(input, output);
  return output.toByteArray();
}

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

/**
 * {@inheritDoc}
 */
@Override
public void copyFileFromContainer(String containerPath, String destinationPath) {
  copyFileFromContainer(containerPath, inputStream -> {
    try(FileOutputStream output = new FileOutputStream(destinationPath)) {
      IOUtils.copy(inputStream, output);
      return null;
    }
  });
}

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

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

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

public static byte[] decompress(byte[] bytes, int len) throws IOException
  {
    byte[] data = new byte[len + BZIP_HEADER.length];

    // add header
    System.arraycopy(BZIP_HEADER, 0, data, 0, BZIP_HEADER.length);
    System.arraycopy(bytes, 0, data, BZIP_HEADER.length, len);

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try (InputStream is = new BZip2CompressorInputStream(new ByteArrayInputStream(data)))
    {
      IOUtils.copy(is, os);
    }

    return os.toByteArray();
  }
}

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

public static byte[] compress(byte[] bytes) throws IOException
{
  InputStream is = new ByteArrayInputStream(bytes);
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  try (OutputStream os = new BZip2CompressorOutputStream(bout, 1))
  {
    IOUtils.copy(is, os);
  }
  byte[] out = bout.toByteArray();
  assert BZIP_HEADER[0] == out[0];
  assert BZIP_HEADER[1] == out[1];
  assert BZIP_HEADER[2] == out[2];
  assert BZIP_HEADER[3] == out[3];
  return Arrays.copyOfRange(out, BZIP_HEADER.length, out.length); // remove header..
}

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

/**
 * Decompresses a file.
 *
 * @param inputStream the compressed file
 * @param outputFile the location to write the decompressed file
 * @throws ArchiveExtractionException thrown if there is an exception
 * decompressing the file
 */
private void decompressFile(CompressorInputStream inputStream, File outputFile) throws ArchiveExtractionException {
  LOGGER.debug("Decompressing '{}'", outputFile.getPath());
  try (FileOutputStream out = new FileOutputStream(outputFile)) {
    IOUtils.copy(inputStream, out);
  } catch (IOException ex) {
    LOGGER.debug("", ex);
    throw new ArchiveExtractionException(ex);
  }
}

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

@Override
public ByteBuffer decompress(ByteBuffer compressedData) throws IOException {
  ByteArrayOutputStream baos = getOutputBuffer(compressedData.remaining());
  InputStream bytesIn = new ByteArrayInputStream(
   compressedData.array(),
   computeOffset(compressedData),
   compressedData.remaining());
  try (InputStream ios = new ZstdCompressorInputStream(bytesIn)) {
    IOUtils.copy(ios, baos);
  }
  return ByteBuffer.wrap(baos.toByteArray());
}

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

@Override
public ByteBuffer decompress(ByteBuffer data) throws IOException {
 ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
 InputStream bytesIn = new ByteArrayInputStream(
  data.array(),
  data.arrayOffset() + data.position(),
  data.remaining());
 InputStream ios = new XZCompressorInputStream(bytesIn);
 try {
  IOUtils.copy(ios, baos);
 } finally {
  ios.close();
 }
 return ByteBuffer.wrap(baos.toByteArray());
}

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

@Override
public ByteBuffer decompress(ByteBuffer data) throws IOException {
 ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
 InputStream bytesIn = new ByteArrayInputStream(
  data.array(),
  computeOffset(data),
  data.remaining());
 try (InputStream ios = new XZCompressorInputStream(bytesIn)) {
  IOUtils.copy(ios, baos);
 }
 return ByteBuffer.wrap(baos.toByteArray());
}

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

public void accept(File source, ArchiveEntry e) throws IOException {
    target.putArchiveEntry(e);
    if (!e.isDirectory()) {
      try (InputStream in = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
        IOUtils.copy(in, target);
      }
    }
    target.closeArchiveEntry();
  }
}, new Finisher() {

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

private void movePcapFileFromCacheToStorage() {
 File inputFile = new File(mPcapFileName);
 InputStream in = null;
 OutputStream out = null;
 try {
  in = new FileInputStream(inputFile);
  out = new FileOutputStream(new File(System.getStoragePath(),new File(mPcapFileName).getName()));
  IOUtils.copy(in, out);
 } catch (IOException e) {
  System.errorLogging(e);
 } finally {
  IOUtils.closeQuietly(in);
  IOUtils.closeQuietly(out);
  inputFile.delete();
 }
}

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

public static void zip(String parentDir, String[] inputFiles, String outputFile)
  throws IOException {
 ZipOutputStream output = null;
 try {
  output = new ZipOutputStream(new FileOutputStream(new File(parentDir, outputFile)));
  for (int i = 0; i < inputFiles.length; i++) {
   File f = new File(parentDir, inputFiles[i]);
   FileInputStream input = new FileInputStream(f);
   output.putNextEntry(new ZipEntry(inputFiles[i]));
   try {
    IOUtils.copy(input, output);
   } finally {
    input.close();
   }
  }
 } finally {
  org.apache.hadoop.io.IOUtils.closeStream(output);
 }
}

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

/**
 * Helper method for {@link #tar(FileSystem, FileSystem, Path, Path)} that adds a file entry to a given
 * {@link TarArchiveOutputStream} and copies the contents of the file to the new entry.
 */
private static void fileToTarArchiveOutputStream(FileStatus fileStatus, FSDataInputStream fsDataInputStream,
  Path destFile, TarArchiveOutputStream tarArchiveOutputStream) throws IOException {
 TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(formatPathToFile(destFile));
 tarArchiveEntry.setSize(fileStatus.getLen());
 tarArchiveEntry.setModTime(System.currentTimeMillis());
 tarArchiveOutputStream.putArchiveEntry(tarArchiveEntry);
 try {
  IOUtils.copy(fsDataInputStream, tarArchiveOutputStream);
 } finally {
  tarArchiveOutputStream.closeArchiveEntry();
 }
}

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

private static File decompressTarBZ2(InputStream is) throws IOException {
 File outputDir = Files.createTempDir();
 try (TarArchiveInputStream tais = new TarArchiveInputStream(
   new BZip2CompressorInputStream(is))) {
  TarArchiveEntry entry;
  while ((entry = tais.getNextTarEntry()) != null) {
   if (entry.isDirectory()) {
    continue;
   }
   File outputFile = new File(outputDir, entry.getName());
   File parent = outputFile.getParentFile();
   if (!parent.exists()) {
    parent.mkdirs();
   }
   try (FileOutputStream os = new FileOutputStream(outputFile)) {
    IOUtils.copy(tais, os);
   }
  }
  return outputDir;
 }
}

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

/**
 * @param command the move command to execute
 * @param writeType the write type to use for the moved file
 * @param fileSystem the Alluxio file system
 */
private static void move(MoveCommand command, WritePType writeType, FileSystem fileSystem)
  throws Exception {
 String source = command.getSource();
 String destination = command.getDestination();
 LOG.debug("Moving {} to {}", source, destination);
 CreateFilePOptions createOptions =
   CreateFilePOptions.newBuilder().setWriteType(writeType).build();
 try (FileOutStream out = fileSystem.createFile(new AlluxioURI(destination), createOptions)) {
  try (FileInStream in = fileSystem.openFile(new AlluxioURI(source))) {
   IOUtils.copy(in, out);
  } catch (Throwable t) {
   try {
    out.cancel();
   } catch (Throwable t2) {
    t.addSuppressed(t2);
   }
   throw t;
  }
 }
 fileSystem.delete(new AlluxioURI(source));
}

相关文章