Skip to content

Commit

Permalink
Merge pull request #533 from bpow/bzip2-fixes
Browse files Browse the repository at this point in the history
Bzip2 fixes in cram.io.ExternalCompression
  • Loading branch information
cmnbroad committed May 26, 2016
2 parents 5560c3d + 00344d1 commit 67714e0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ dependencies {
compile "org.xerial.snappy:snappy-java:1.0.3-rc3"
compile "org.apache.commons:commons-compress:1.4.1"
compile "org.tukaani:xz:1.5"
compile "org.apache.ant:ant:1.8.2"
compile "gov.nih.nlm.ncbi:ngs-java:1.2.2"

testCompile "org.testng:testng:6.9.9"
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/htsjdk/samtools/cram/io/ExternalCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import htsjdk.samtools.cram.encoding.rans.RANS;
import htsjdk.samtools.util.IOUtil;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
import org.apache.tools.bzip2.CBZip2InputStream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -58,7 +58,11 @@ public static byte[] gunzip(final byte[] data) throws IOException {
* @return compressed blob
*/
public static byte[] bzip2(final byte[] data) throws IOException {
return InputStreamUtils.readFully(new BZip2CompressorInputStream(new ByteArrayInputStream(data)));
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final BZip2CompressorOutputStream bos = new BZip2CompressorOutputStream(byteArrayOutputStream);
IOUtil.copyStream(new ByteArrayInputStream(data), bos);
bos.close();
return byteArrayOutputStream.toByteArray();
}

/**
Expand All @@ -71,10 +75,7 @@ public static byte[] bzip2(final byte[] data) throws IOException {
@SuppressWarnings("ResultOfMethodCallIgnored")
public static byte[] unbzip2(final byte[] data) throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
// hello, apache!
byteArrayInputStream.read();
byteArrayInputStream.read();
return InputStreamUtils.readFully(new CBZip2InputStream(byteArrayInputStream));
return InputStreamUtils.readFully(new BZip2CompressorInputStream(byteArrayInputStream));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package htsjdk.samtools.cram.io;

import org.apache.commons.compress.utils.IOUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;

public class ExternalCompressionTest {
public static final File BZIP2_FILE = new File("testdata/htsjdk/samtools/io/bzip2-test.bz2");
public static final byte [] TEST_BYTES = "This is a simple string to test BZip2".getBytes();

@Test
public void testBZip2Decompression() throws IOException {
final byte [] input = Files.readAllBytes(BZIP2_FILE.toPath());
final byte [] output = ExternalCompression.unbzip2(input);
Assert.assertEquals(output, "BZip2 worked".getBytes());
}

@Test
public void testBZip2Roundtrip() throws IOException {
final byte [] compressed = ExternalCompression.bzip2(TEST_BYTES);
final byte [] restored = ExternalCompression.unbzip2(compressed);
Assert.assertEquals(TEST_BYTES, restored);
}
}
Binary file added testdata/htsjdk/samtools/io/bzip2-test.bz2
Binary file not shown.

0 comments on commit 67714e0

Please sign in to comment.