List of usage examples for org.apache.commons.compress.compressors.bzip2 BZip2CompressorInputStream read
public int read(final byte[] dest, final int offs, final int len) throws IOException
From source file:CompressTransfer.java
/** * ?/* w w w . jav a 2 s . co m*/ * * @param is * @param os * @throws Exception */ public static void bzdecompress(InputStream is, OutputStream os) throws Exception { BZip2CompressorInputStream gis = new BZip2CompressorInputStream(is); int count; byte data[] = new byte[BUFFER]; while ((count = gis.read(data, 0, BUFFER)) != -1) { os.write(data, 0, count); } gis.close(); }
From source file:com.github.koraktor.steamcondenser.servers.packets.SteamPacketFactory.java
/** * Reassembles the data of a split and/or compressed packet into a single * packet object/*w w w . jav a 2s .c o m*/ * * @param splitPackets An array of packet data * @param isCompressed whether the data of this packet is compressed * @param uncompressedSize The size of the decompressed packet data * @param packetChecksum The CRC32 checksum of the decompressed * packet data * @throws SteamCondenserException if decompressing the packet data fails * @throws PacketFormatException if the calculated CRC32 checksum does not * match the expected value * @return SteamPacket The reassembled packet * @see SteamPacketFactory#getPacketFromData */ public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed, int uncompressedSize, int packetChecksum) throws SteamCondenserException { byte[] packetData, tmpData; packetData = new byte[0]; for (byte[] splitPacket : splitPackets) { tmpData = packetData; packetData = new byte[tmpData.length + splitPacket.length]; System.arraycopy(tmpData, 0, packetData, 0, tmpData.length); System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length); } if (isCompressed) { try { ByteArrayInputStream stream = new ByteArrayInputStream(packetData); stream.read(); stream.read(); BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream); byte[] uncompressedPacketData = new byte[uncompressedSize]; bzip2.read(uncompressedPacketData, 0, uncompressedSize); CRC32 crc32 = new CRC32(); crc32.update(uncompressedPacketData); int crc32checksum = (int) crc32.getValue(); if (crc32checksum != packetChecksum) { throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data."); } packetData = uncompressedPacketData; } catch (IOException e) { throw new SteamCondenserException(e.getMessage(), e); } } tmpData = packetData; packetData = new byte[tmpData.length - 4]; System.arraycopy(tmpData, 4, packetData, 0, tmpData.length - 4); return SteamPacketFactory.getPacketFromData(packetData); }
From source file:kr.debop4j.core.compress.BZip2Compressor.java
@Override protected byte[] doDecompress(byte[] compressed) throws IOException { @Cleanup/*from ww w.java 2 s . co m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); @Cleanup ByteArrayInputStream bis = new ByteArrayInputStream(compressed); @Cleanup BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(bis); byte[] buff = new byte[BUFFER_SIZE]; int n; while ((n = bzip2.read(buff, 0, BUFFER_SIZE)) > 0) { bos.write(buff, 0, n); } return bos.toByteArray(); }
From source file:it.geosolutions.tools.compress.file.Extractor.java
/** * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it * //from w w w . j a v a 2 s .c o m */ public static void extractBz2(File in_file, File out_file) throws CompressorException { FileOutputStream out = null; BZip2CompressorInputStream zIn = null; FileInputStream fis = null; BufferedInputStream bis = null; try { out = new FileOutputStream(out_file); fis = new FileInputStream(in_file); bis = new BufferedInputStream(fis); /* * int b = bis.read(); if (b != 'B') { throw new * CompressorException("Invalid bz2 file: "+in_file.getAbsolutePath()); } b = * bis.read(); if (b != 'Z') { throw new * CompressorException("Invalid bz2 file: "+in_file.getAbsolutePath()); } */ zIn = new BZip2CompressorInputStream(bis); byte[] buffer = new byte[Conf.getBufferSize()]; int count = 0; do { out.write(buffer, 0, count); count = zIn.read(buffer, 0, buffer.length); } while (count != -1); } catch (IOException ioe) { String msg = "Problem expanding bzip2 " + ioe.getMessage(); throw new CompressorException(msg + in_file.getAbsolutePath()); } finally { try { if (bis != null) bis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (fis != null) fis.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (out != null) out.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } try { if (zIn != null) zIn.close(); } catch (IOException ioe) { throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath()); } } }
From source file:io.netty.handler.codec.compression.Bzip2EncoderTest.java
@Override protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception { InputStream is = new ByteBufInputStream(compressed); BZip2CompressorInputStream bzip2Is = new BZip2CompressorInputStream(is); byte[] decompressed = new byte[originalLength]; int remaining = originalLength; while (remaining > 0) { int read = bzip2Is.read(decompressed, originalLength - remaining, remaining); if (read > 0) { remaining -= read;/*ww w. jav a2 s. c o m*/ } else { break; } } assertEquals(-1, bzip2Is.read()); bzip2Is.close(); return Unpooled.wrappedBuffer(decompressed); }
From source file:freenet.support.compress.Bzip2Compressor.java
@Override public long decompress(InputStream is, OutputStream os, long maxLength, long maxCheckSizeBytes) throws IOException, CompressionOutputSizeException { BZip2CompressorInputStream bz2is = new BZip2CompressorInputStream(HeaderStreams.augInput(BZ_HEADER, is)); long written = 0; int bufSize = 32768; if (maxLength > 0 && maxLength < bufSize) bufSize = (int) maxLength; byte[] buffer = new byte[bufSize]; while (true) { int expectedBytesRead = (int) Math.min(buffer.length, maxLength - written); // We can over-read to determine whether we have over-read. // We enforce maximum size this way. // FIXME there is probably a better way to do this! int bytesRead = bz2is.read(buffer, 0, buffer.length); if (expectedBytesRead < bytesRead) { Logger.normal(this, "expectedBytesRead=" + expectedBytesRead + ", bytesRead=" + bytesRead + ", written=" + written + ", maxLength=" + maxLength + " throwing a CompressionOutputSizeException"); if (maxCheckSizeBytes > 0) { written += bytesRead;/*from w w w .j a v a 2 s . c o m*/ while (true) { expectedBytesRead = (int) Math.min(buffer.length, maxLength + maxCheckSizeBytes - written); bytesRead = bz2is.read(buffer, 0, expectedBytesRead); if (bytesRead <= -1) throw new CompressionOutputSizeException(written); if (bytesRead == 0) throw new IOException("Returned zero from read()"); written += bytesRead; } } throw new CompressionOutputSizeException(); } if (bytesRead <= -1) return written; if (bytesRead == 0) throw new IOException("Returned zero from read()"); os.write(buffer, 0, bytesRead); written += bytesRead; } }
From source file:net.yacy.document.parser.bzipParser.java
@Override public Document[] parse(final DigestURL location, final String mimeType, final String charset, final VocabularyScraper scraper, final int timezoneOffset, final InputStream source) throws Parser.Failure, InterruptedException { File tempFile = null;/* w w w . j a va 2 s. c om*/ Document maindoc = null; try { int read = 0; final byte[] data = new byte[1024]; // BZip2CompressorInputStream checks filecontent (magic start-bytes "BZh") and throws ioexception if no match final BZip2CompressorInputStream zippedContent = new BZip2CompressorInputStream(source); tempFile = File.createTempFile("bunzip", "tmp"); // creating a temp file to store the uncompressed data final FileOutputStream out = new FileOutputStream(tempFile); // reading bzip file and store it uncompressed while ((read = zippedContent.read(data, 0, 1024)) != -1) { out.write(data, 0, read); } zippedContent.close(); out.close(); final String filename = location.getFileName(); // create maindoc for this bzip container, register with supplied url & mime maindoc = new Document(location, mimeType, charset, this, null, null, AbstractParser.singleList( filename.isEmpty() ? location.toTokens() : MultiProtocolURL.unescape(filename)), // title null, null, null, null, 0.0d, 0.0d, (Object) null, null, null, null, false, new Date()); // creating a new parser class to parse the unzipped content final String contentfilename = BZip2Utils.getUncompressedFilename(location.getFileName()); final String mime = TextParser.mimeOf(DigestURL.getFileExtension(contentfilename)); final Document[] docs = TextParser.parseSource(location, mime, null, scraper, timezoneOffset, 999, tempFile); if (docs != null) maindoc.addSubDocuments(docs); } catch (final Exception e) { if (e instanceof InterruptedException) throw (InterruptedException) e; if (e instanceof Parser.Failure) throw (Parser.Failure) e; throw new Parser.Failure("Unexpected error while parsing bzip file. " + e.getMessage(), location); } finally { if (tempFile != null) FileUtils.deletedelete(tempFile); } return maindoc == null ? null : new Document[] { maindoc }; }
From source file:org.apache.avro.file.BZip2Codec.java
@Override public ByteBuffer decompress(ByteBuffer compressedData) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.array()); BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(bais); try {//from w ww. j av a2s .c om ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int readCount = -1; while ((readCount = inputStream.read(buffer, compressedData.position(), buffer.length)) > 0) { baos.write(buffer, 0, readCount); } ByteBuffer result = ByteBuffer.wrap(baos.toByteArray()); return result; } finally { inputStream.close(); } }
From source file:org.apache.trevni.BZip2Codec.java
@Override ByteBuffer decompress(ByteBuffer compressedData) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.array()); BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(bais); try {//from ww w . java 2 s . co m ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int readCount = -1; while ((readCount = inputStream.read(buffer, compressedData.position(), buffer.length)) > 0) { baos.write(buffer, 0, readCount); } ByteBuffer result = ByteBuffer.wrap(baos.toByteArray()); return result; } finally { inputStream.close(); } }
From source file:org.game.cs.core.condenser.steam.packets.SteamPacketFactory.java
/** * Reassembles the data of a split and/or compressed packet into a single * packet object/*from ww w.j a v a 2 s . c o m*/ * * @param splitPackets An array of packet data * @param isCompressed whether the data of this packet is compressed * @param uncompressedSize The size of the decompressed packet data * @param packetChecksum The CRC32 checksum of the decompressed * packet data * @throws SteamCondenserException if decompressing the packet data fails * @throws PacketFormatException if the calculated CRC32 checksum does not * match the expected value * @return SteamPacket The reassembled packet * @see SteamPacketFactory#getPacketFromData */ public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed, int uncompressedSize, int packetChecksum) throws SteamCondenserException { byte[] packetData, tmpData; packetData = new byte[0]; for (byte[] splitPacket : splitPackets) { tmpData = packetData; packetData = new byte[tmpData.length + splitPacket.length]; System.arraycopy(tmpData, 0, packetData, 0, tmpData.length); System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length); } if (isCompressed) { try { ByteArrayInputStream stream = new ByteArrayInputStream(packetData); stream.read(); stream.read(); BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream); byte[] uncompressedPacketData = new byte[uncompressedSize]; bzip2.read(uncompressedPacketData, 0, uncompressedSize); CRC32 crc32 = new CRC32(); crc32.update(uncompressedPacketData); int crc32checksum = (int) crc32.getValue(); if (crc32checksum != packetChecksum) { throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data."); } packetData = uncompressedPacketData; } catch (IOException e) { throw new SteamCondenserException(e.getMessage(), e); } } packetData = new String(packetData).substring(4).getBytes(); return SteamPacketFactory.getPacketFromData(packetData); }