org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream Java Examples

The following examples show how to use org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompressingTempFileStore.java    From nexus-repository-apt with Eclipse Public License 1.0 7 votes vote down vote up
public Writer openOutput(String key) throws UncheckedIOException {
  try {
    if (holdersByKey.containsKey(key)) {
      throw new IllegalStateException("Output already opened");
    }
    FileHolder holder = new FileHolder();
    holdersByKey.put(key, holder);
    return new OutputStreamWriter(new TeeOutputStream(
        new TeeOutputStream(new GZIPOutputStream(Files.newOutputStream(holder.gzTempFile)),
            new BZip2CompressorOutputStream(Files.newOutputStream(holder.bzTempFile))),
        Files.newOutputStream(holder.plainTempFile)), Charsets.UTF_8);
  }
  catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example #2
Source File: S3TransportBuffer.java    From bender with Apache License 2.0 7 votes vote down vote up
public S3TransportBuffer(long maxBytes, boolean useCompression, S3TransportSerializer serializer)
    throws TransportException {
  this.maxBytes = maxBytes;
  this.serializer = serializer;

  baos = new ByteArrayOutputStream();
  cos = new CountingOutputStream(baos);

  if (useCompression) {
    this.isCompressed = true;
    try {
      os = new BZip2CompressorOutputStream(cos);
    } catch (IOException e) {
      throw new TransportException("unable to create BZip2CompressorOutputStream", e);
    }
  } else {
    this.isCompressed = false;
    os = cos;
  }
}
 
Example #3
Source File: BZip2CommonsCompressor.java    From yosegi with Apache License 2.0 6 votes vote down vote up
private int getCompressLevel( final CompressionPolicy compressionPolicy ) {
  switch ( compressionPolicy ) {
    case BEST_SPEED:
      return BZip2CompressorOutputStream.MIN_BLOCKSIZE;
    case SPEED:
      return 3;
    case DEFAULT:
      return 6;
    case BEST_COMPRESSION:
      return BZip2CompressorOutputStream.MAX_BLOCKSIZE;
    default:
      return 6;
  }
}
 
Example #4
Source File: Tar.java    From writelatex-git-bridge with MIT License 6 votes vote down vote up
public static InputStream zip(
        File fileOrDir,
        long[] sizePtr
) throws IOException {
    File tmp = File.createTempFile(fileOrDir.getName(), ".tar.bz2");
    tmp.deleteOnExit();
    OutputStream target = new FileOutputStream(tmp);
    /* Closes target */
    try (OutputStream bzip2 = new BZip2CompressorOutputStream(target)) {
        tarTo(fileOrDir, bzip2);
    }
    if (sizePtr != null) {
        sizePtr[0] = tmp.length();
    }
    return new DeletingFileInputStream(tmp);
}
 
Example #5
Source File: BZip2PayloadCoding.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public OutputStream createOutputStream ( final OutputStream out, final Optional<String> optionalFlags ) throws IOException
{
    final String flags;

    final int blockSize;

    if ( optionalFlags.isPresent () && ( flags = optionalFlags.get () ).length () > 0 )
    {
        blockSize = Integer.parseInt ( flags.substring ( 0, 1 ) );
    }
    else
    {
        blockSize = BZip2CompressorOutputStream.MAX_BLOCKSIZE;
    }

    return new BZip2CompressorOutputStream ( out, blockSize );
}
 
Example #6
Source File: CompressingTempFileStore.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
public Writer openOutput(final String key) {
  try {
    if (holdersByKey.containsKey(key)) {
      throw new IllegalStateException("Output already opened");
    }
    FileHolder holder = new FileHolder();
    holdersByKey.put(key, holder);
    return new OutputStreamWriter(new TeeOutputStream(
        new TeeOutputStream(new GZIPOutputStream(Files.newOutputStream(holder.gzTempFile)),
            new BZip2CompressorOutputStream(Files.newOutputStream(holder.bzTempFile))),
        Files.newOutputStream(holder.plainTempFile)), Charsets.UTF_8);
  }
  catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example #7
Source File: CompressUtils.java    From spring-boot-doma2-sample with Apache License 2.0 6 votes vote down vote up
/**
 * 入力したバイト配列をBZip2で圧縮して返します。
 * 
 * @param input
 * @return
 */
public static byte[] compress(byte[] input) {
    ByteArrayOutputStream ref = null;

    try (val bais = new ByteArrayInputStream(input);
            val baos = new ByteArrayOutputStream(input.length);
            val bzip2cos = new BZip2CompressorOutputStream(baos)) {
        IOUtils.copy(bais, bzip2cos);
        ref = baos;
    } catch (IOException e) {
        log.error("failed to encode.", e);
        throw new RuntimeException(e);
    }

    return ref.toByteArray();
}
 
Example #8
Source File: BZip2DecompressorTest.java    From feast with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecompressBZip2Stream() throws IOException {
  BZip2Decompressor<String> decompressor =
      new BZip2Decompressor<>(
          inputStream -> {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String output = reader.readLine();
            reader.close();
            return output;
          });

  String originalString = "abc";
  ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
  try (BZip2CompressorOutputStream bzip2Output =
      new BZip2CompressorOutputStream(compressedStream)) {
    bzip2Output.write(originalString.getBytes());
  }

  String decompressedString = decompressor.decompress(compressedStream.toByteArray());
  assertEquals(originalString, decompressedString);
}
 
Example #9
Source File: CompressedSourceTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Get a compressing stream for a given compression mode. */
private OutputStream getOutputStreamForMode(CompressionMode mode, OutputStream stream)
    throws IOException {
  switch (mode) {
    case GZIP:
      return new GzipCompressorOutputStream(stream);
    case BZIP2:
      return new BZip2CompressorOutputStream(stream);
    case ZIP:
      return new TestZipOutputStream(stream);
    case ZSTD:
      return new ZstdCompressorOutputStream(stream);
    case DEFLATE:
      return new DeflateCompressorOutputStream(stream);
    case LZO:
      return LzoCompression.createLzoOutputStream(stream);
    case LZOP:
      return LzoCompression.createLzopOutputStream(stream);
    default:
      throw new RuntimeException("Unexpected compression mode");
  }
}
 
Example #10
Source File: TestGribCompressByBit.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
byte[] compress(byte[] bdata) {
  ByteArrayOutputStream out = new ByteArrayOutputStream(bdata.length);
  try (BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(out)) {
    bzOut.write(bdata, 0, bdata.length);
    bzOut.finish();

  } catch (Exception e) {
    e.printStackTrace();
  }

  return out.toByteArray();
}
 
Example #11
Source File: MockStringContentFactory.java    From Wikidata-Toolkit with Apache License 2.0 5 votes vote down vote up
/**
 * Turns a string into a sequence of bytes, possibly compressed. In any
 * case, the character encoding used for converting the string into bytes is
 * UTF8.
 *
 * @param string
 * @param compressionType
 * @return
 * @throws IOException
 */
public static byte[] getBytesFromString(String string,
		CompressionType compressionType) throws IOException {
	switch (compressionType) {
	case NONE:
		return string.getBytes(StandardCharsets.UTF_8);
	case BZ2:
	case GZIP:
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		OutputStreamWriter ow;
		if (compressionType == CompressionType.GZIP) {
			ow = new OutputStreamWriter(
					new GzipCompressorOutputStream(out),
					StandardCharsets.UTF_8);
		} else {
			ow = new OutputStreamWriter(
					new BZip2CompressorOutputStream(out),
					StandardCharsets.UTF_8);
		}

		ow.write(string);
		ow.close();
		return out.toByteArray();
	default:
		throw new RuntimeException("Unknown compression type "
				+ compressionType);
	}
}
 
Example #12
Source File: DirectoryManagerTest.java    From Wikidata-Toolkit with Apache License 2.0 5 votes vote down vote up
@Test
public void getCompressionInputStreamBz2() throws IOException {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	OutputStreamWriter ow = new OutputStreamWriter(
			new BZip2CompressorOutputStream(out), StandardCharsets.UTF_8);
	ow.write("Test data");
	ow.close();

	ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
	InputStream cin = dm.getCompressorInputStream(in, CompressionType.BZ2);

	assertEquals("Test data",
			new BufferedReader(new InputStreamReader(cin)).readLine());
}
 
Example #13
Source File: TarBzStripper.java    From reproducible-build-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
protected TarArchiveOutputStream createOutputStream(File out) throws FileNotFoundException, IOException
{
    final TarArchiveOutputStream stream = new TarArchiveOutputStream(
            new BZip2CompressorOutputStream(new FileOutputStream(out)));
    stream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    return stream;
}
 
Example #14
Source File: BZip2CommonsCompressor.java    From yosegi with Apache License 2.0 5 votes vote down vote up
@Override
public OutputStream createOutputStream(
    final OutputStream out ,
    final long decompressSize,
    final CompressResult compressResult ) throws IOException {
  int level = getCompressLevel( compressResult.getCompressionPolicy() );
  int optLevel = compressResult.getCurrentLevel();
  if ( ( level - optLevel ) < BZip2CompressorOutputStream.MIN_BLOCKSIZE ) {
    compressResult.setEnd();
    optLevel = compressResult.getCurrentLevel();
  }
  return new BZip2CompressorOutputStream( out , level - optLevel );
}
 
Example #15
Source File: RepoBuilder.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private byte[] compressBzip2 ( final byte[] data ) throws IOException
{
    final ByteArrayOutputStream bos = new ByteArrayOutputStream ();
    final BZip2CompressorOutputStream b2os = new BZip2CompressorOutputStream ( bos );

    b2os.write ( data );

    b2os.close ();
    return bos.toByteArray ();
}
 
Example #16
Source File: Bzip2DecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected byte[] compress(byte[] data) throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bZip2Os = new BZip2CompressorOutputStream(os, MIN_BLOCK_SIZE);
    bZip2Os.write(data);
    bZip2Os.close();

    return os.toByteArray();
}
 
Example #17
Source File: BZip2Compressor.java    From feast with Apache License 2.0 5 votes vote down vote up
/**
 * Compress pipeline option using BZip2
 *
 * @param option Pipeline option value
 * @return BZip2 compressed option value
 * @throws IOException
 */
@Override
public byte[] compress(T option) throws IOException {
  ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
  try (BZip2CompressorOutputStream bzip2Output =
      new BZip2CompressorOutputStream(compressedStream)) {
    bzip2Output.write(byteConverter.toByte(option));
  }

  return compressedStream.toByteArray();
}
 
Example #18
Source File: XmlFilenameWriter.java    From metafacture-core with Apache License 2.0 5 votes vote down vote up
private OutputStream createBZip2Compressor(OutputStream stream) {
    try {
        return new BZip2CompressorOutputStream(stream);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example #19
Source File: ArchivePrinter.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * It reads data from a an input file, then write and compress to a bzip2 file using BZip2CompressorOutputStream
 * If a special input file with repeating inputs is provided, the vulnerability in BZip2CompressorOutputStream will result in endless writing and consuming lots of resources
 * Please refer to https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2012-2098
 * @param _in
 * @param _out
 * @throws Exception
 */
public static void compressExploitability(Path _in, Path _out) throws Exception {
	FileInputStream fin = new FileInputStream(_in.toString());
	BufferedInputStream in = new BufferedInputStream(fin);
	BZip2CompressorOutputStream out = new BZip2CompressorOutputStream(new FileOutputStream(_out.toString()));
	BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
	final byte[] buffer = new byte[1024*10];
	int n = 0;
	while (-1 != (n = bzIn.read(buffer))) {
	    out.write(buffer, 0, n);
	}
	out.close();
	bzIn.close();
}
 
Example #20
Source File: CompressTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Fuzz
public void bzip2(byte @Size(min=100, max=100)[] bytes){
    OutputStream o = new ByteArrayOutputStream();
    try {
        BZip2CompressorOutputStream bo = new BZip2CompressorOutputStream(o);
        bo.write(bytes);
        bo.finish();
    } catch (IOException e){
        Assume.assumeNoException(e);
    }

}
 
Example #21
Source File: Bzip2Compress.java    From compress with MIT License 5 votes vote down vote up
@Override
public byte[] compress(byte[] data) throws IOException {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	BZip2CompressorOutputStream bcos = new BZip2CompressorOutputStream(out);
	bcos.write(data);
	bcos.close();

	return out.toByteArray();
}
 
Example #22
Source File: ArchivePrinter.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * It reads data from a an input file, then write and compress to a bzip2 file using BZip2CompressorOutputStream
 * If a special input file with repeating inputs is provided, the vulnerability in BZip2CompressorOutputStream will result in endless writing and consuming lots of resources
 * Please refer to https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2012-2098
 * @param _in
 * @param _out
 * @throws Exception
 */
public static void compressExploitability(Path _in, Path _out) throws Exception {
	FileInputStream fin = new FileInputStream(_in.toString());
	BufferedInputStream in = new BufferedInputStream(fin);
	BZip2CompressorOutputStream out = new BZip2CompressorOutputStream(new FileOutputStream(_out.toString()));
	BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
	final byte[] buffer = new byte[1024*10];
	int n = 0;
	while (-1 != (n = bzIn.read(buffer))) {
	    out.write(buffer, 0, n);
	}
	out.close();
	bzIn.close();
}
 
Example #23
Source File: PBzip2OutputStream.java    From jphp with Apache License 2.0 4 votes vote down vote up
@Signature
public void __construct(Environment env, OutputStream outputStream, int blockSize)
        throws IOException {
    this.outputStream = new BZip2CompressorOutputStream(outputStream, blockSize);
}
 
Example #24
Source File: PBzip2OutputStream.java    From jphp with Apache License 2.0 4 votes vote down vote up
public PBzip2OutputStream(Environment env, BZip2CompressorOutputStream outputStream) {
    super(env, outputStream);
}
 
Example #25
Source File: Bzip2FileObject.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
    return new BZip2CompressorOutputStream(getContainer().getContent().getOutputStream(false));
}
 
Example #26
Source File: Util.java    From LSH_DeepLearning with Apache License 2.0 4 votes vote down vote up
public static BufferedWriter writerBZ2(final String path) throws IOException
{
    return new BufferedWriter(new OutputStreamWriter(new BZip2CompressorOutputStream(new BufferedOutputStream(new FileOutputStream(path)))));
}
 
Example #27
Source File: Bzip2FileDataSource.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected OutputStream getCompressedOutputStream(OutputStream os) throws IOException {
    return new BZip2CompressorOutputStream(new BufferedOutputStream(os));
}
 
Example #28
Source File: BZip2CommonsCompressor.java    From multiple-dimension-spread with Apache License 2.0 4 votes vote down vote up
@Override
public OutputStream createOutputStream( final OutputStream out , final DataType dataType ) throws IOException{
  return new BZip2CompressorOutputStream( out );
}