Java Code Examples for org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream#read()

The following examples show how to use org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream#read() . 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: Bzip2Compress.java    From compress with MIT License 6 votes vote down vote up
@Override
public byte[] uncompress(byte[] data) throws IOException {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	ByteArrayInputStream in = new ByteArrayInputStream(data);

	try {
		@SuppressWarnings("resource")
		BZip2CompressorInputStream ungzip = new BZip2CompressorInputStream(in);
		byte[] buffer = new byte[2048];
		int n;
		while ((n = ungzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

	return out.toByteArray();
}
 
Example 2
Source File: TestLineRecordReader.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public String[] readRecordsDirectly(URL testFileUrl, boolean bzip)
    throws IOException {
  int MAX_DATA_SIZE = 1024 * 1024;
  byte[] data = new byte[MAX_DATA_SIZE];
  FileInputStream fis = new FileInputStream(testFileUrl.getFile());
  int count;
  if (bzip) {
    BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis);
    count = bzIn.read(data);
    bzIn.close();
  } else {
    count = fis.read(data);
  }
  fis.close();
  assertTrue("Test file data too big for buffer", count < data.length);
  return new String(data, 0, count, "UTF-8").split("\n");
}
 
Example 3
Source File: TestLineRecordReader.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public String[] readRecordsDirectly(URL testFileUrl, boolean bzip)
    throws IOException {
  int MAX_DATA_SIZE = 1024 * 1024;
  byte[] data = new byte[MAX_DATA_SIZE];
  FileInputStream fis = new FileInputStream(testFileUrl.getFile());
  int count;
  if (bzip) {
    BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis);
    count = bzIn.read(data);
    bzIn.close();
  } else {
    count = fis.read(data);
  }
  fis.close();
  assertTrue("Test file data too big for buffer", count < data.length);
  return new String(data, 0, count, "UTF-8").split("\n");
}
 
Example 4
Source File: TestLineRecordReader.java    From big-c with Apache License 2.0 6 votes vote down vote up
public String[] readRecordsDirectly(URL testFileUrl, boolean bzip)
    throws IOException {
  int MAX_DATA_SIZE = 1024 * 1024;
  byte[] data = new byte[MAX_DATA_SIZE];
  FileInputStream fis = new FileInputStream(testFileUrl.getFile());
  int count;
  if (bzip) {
    BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis);
    count = bzIn.read(data);
    bzIn.close();
  } else {
    count = fis.read(data);
  }
  fis.close();
  assertTrue("Test file data too big for buffer", count < data.length);
  return new String(data, 0, count, "UTF-8").split("\n");
}
 
Example 5
Source File: TestLineRecordReader.java    From big-c with Apache License 2.0 6 votes vote down vote up
public String[] readRecordsDirectly(URL testFileUrl, boolean bzip)
    throws IOException {
  int MAX_DATA_SIZE = 1024 * 1024;
  byte[] data = new byte[MAX_DATA_SIZE];
  FileInputStream fis = new FileInputStream(testFileUrl.getFile());
  int count;
  if (bzip) {
    BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis);
    count = bzIn.read(data);
    bzIn.close();
  } else {
    count = fis.read(data);
  }
  fis.close();
  assertTrue("Test file data too big for buffer", count < data.length);
  return new String(data, 0, count, "UTF-8").split("\n");
}
 
Example 6
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 7
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 8
Source File: BZip2.java    From rscplus with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] decompress(byte data[], int offset, int length, int uncompressedLength) {
  byte uncompressedData[] = new byte[uncompressedLength];
  try {
    BZip2CompressorInputStream in =
        new BZip2CompressorInputStream(new ByteArrayInputStream(data, 2, length + 4));
    in.read(uncompressedData);
    in.close();
  } catch (Exception e) {
    return null;
  }
  return uncompressedData;
}