public ZipRepository(final InputStream in, final MimeRegistry mimeRegistry) throws IOException { this(mimeRegistry); final ZipInputStream zipIn = new ZipInputStream(in); try { ZipEntry nextEntry = zipIn.getNextEntry(); while (nextEntry != null) { final String[] buildName = RepositoryUtilities.splitPath(nextEntry.getName(), "/"); if (nextEntry.isDirectory()) { root.updateDirectoryEntry(buildName, 0, nextEntry); } else { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Deflater def = new Deflater(nextEntry.getMethod()); try { final DeflaterOutputStream dos = new DeflaterOutputStream(bos, def); try { IOUtils.getInstance().copyStreams(zipIn, dos); dos.flush(); } finally { dos.close(); } } finally { def.end(); } root.updateEntry(buildName, 0, nextEntry, bos.toByteArray()); } zipIn.closeEntry(); nextEntry = zipIn.getNextEntry(); } } finally { zipIn.close(); } }
@Deprecated private static synchronized byte[] zipFileName(String fileName) throws IOException { Deflater deflater = new Deflater(); byte[] bytes = fileName.getBytes(); deflater.setInput(bytes); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } bos.close(); int count = 0; for (int i = 0; i < buffer.length; i++) { if (buffer[i] != 0) count++; } byte[] result = new byte[count]; for (int i = 0; i < result.length; i++) { result[i] = buffer[i]; } return result; }
@Test public void testDeflaterReader() throws Exception { String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla"; byte[] input = inputString.getBytes("UTF-8"); ByteArrayInputStream inputStream = new ByteArrayInputStream(input); AtomicLong counter = new AtomicLong(0); DeflaterReader reader = new DeflaterReader(inputStream, counter); ArrayList<Integer> zipHolder = new ArrayList<Integer>(); int b = reader.read(); while (b != -1) { zipHolder.add(b); b = reader.read(); } assertEquals(input.length, counter.get()); byte[] allCompressed = new byte[zipHolder.size()]; for (int i = 0; i < allCompressed.length; i++) { allCompressed[i] = (byte) zipHolder.get(i).intValue(); } byte[] output = new byte[30]; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); compareByteArray(allCompressed, output, compressedDataLength); reader.close(); }
@Override public void encode(ChannelBuffer compressed) { while (!compressor.needsInput()) { int numBytes = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH); compressed.writeBytes(out, 0, numBytes); } }
@Test public void testInflaterReader() throws Exception { String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla"; byte[] input = inputString.getBytes("UTF-8"); byte[] output = new byte[30]; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); byte[] zipBytes = new byte[compressedDataLength]; System.arraycopy(output, 0, zipBytes, 0, compressedDataLength); ByteArrayInputStream byteInput = new ByteArrayInputStream(zipBytes); InflaterReader inflater = new InflaterReader(byteInput); ArrayList<Integer> holder = new ArrayList<Integer>(); int read = inflater.read(); while (read != -1) { holder.add(read); read = inflater.read(); } byte[] result = new byte[holder.size()]; for (int i = 0; i < result.length; i++) { result[i] = holder.get(i).byteValue(); } String txt = new String(result); assertEquals(inputString, txt); inflater.close(); }
@Test public void testInflaterWriter() throws Exception { String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla"; byte[] input = inputString.getBytes("UTF-8"); byte[] output = new byte[30]; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); byte[] zipBytes = new byte[compressedDataLength]; System.arraycopy(output, 0, zipBytes, 0, compressedDataLength); ByteArrayInputStream byteInput = new ByteArrayInputStream(zipBytes); ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); InflaterWriter writer = new InflaterWriter(byteOutput); byte[] zipBuffer = new byte[12]; int n = byteInput.read(zipBuffer); while (n > 0) { writer.write(zipBuffer, 0, n); n = byteInput.read(zipBuffer); } writer.close(); byte[] outcome = byteOutput.toByteArray(); String outStr = new String(outcome); assertEquals(inputString, outStr); }
static long getCompressedSize(InputStream is) { DeflaterOutputStream dos = null; long size = 0L; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Deflater def = new Deflater(); def.setLevel(Deflater.BEST_COMPRESSION); dos = new DeflaterOutputStream(baos, def); byte buf[] = new byte[8192]; int n = is.read(buf); while (n > 0) { dos.write(buf); n = is.read(buf); } dos.flush(); dos.finish(); size = baos.size(); } catch (IOException ex) { Logger.getLogger(JarStat.class.getName()).log(Level.SEVERE, null, ex); } finally { try { dos.close(); } catch (IOException ex) { Logger.getLogger(JarStat.class.getName()).log(Level.SEVERE, null, ex); } } return size; }
public synchronized void finish() throws IOException { if (!_def.finished()) { _def.finish(); while (!_def.finished()) { deflate(); } } }
/** Ensures all bytes sent to the deflater are written to the stream. */ private void flushDeflater() throws IOException { if (entry.entry.getMethod() == DEFLATED) { def.finish(); while (!def.finished()) { deflate(); } } }
private byte[] compressData(byte[] rawBytes, AtomicInteger size) { deflater.reset(); deflater.setInput(rawBytes); deflater.finish(); byte[] compressedData = new byte[rawBytes.length]; size.set(deflater.deflate(compressedData)); return compressedData; }
/** * {@collect.stats} {@description.open} Finishes writing compressed data to the output stream * without closing the underlying stream. {@description.close} {@property.open} Use this method * when applying multiple filters in succession to the same output stream. {@property.close} * * @exception IOException if an I/O error has occurred */ public void finish() throws IOException { if (!def.finished()) { def.finish(); while (!def.finished()) { deflate(); } } }
public static byte[] compress(byte data[], String signature, String version) { byte header[] = ArrayUtil.mergeArrays(signature.getBytes(), version.getBytes()); header = ArrayUtil.mergeArrays(header, Byteconvert.convertBack(data.length)); byte result[] = ArrayUtil.resizeArray(header, data.length * 2); Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); int clength = deflater.deflate(result, 12, result.length - 12); return ArrayUtil.getSubArray(result, 0, clength + 12); }
/** Creates a deflater based on the Zlib arguments. */ private static Deflater createDeflater(int compressionLevel, int strategy, int encodingMode) { Deflater defl; if (encodingMode == ZlibModule.FORCE_GZIP) defl = new Deflater(compressionLevel, true); else defl = new Deflater(compressionLevel, false); defl.setStrategy(strategy); return defl; }
/** * Writes all necessary data for this entry. * * @since 1.1 */ public void closeEntry() throws IOException { if (entry == null) { return; } long realCrc = crc.getValue(); crc.reset(); if (entry.getMethod() == DEFLATED) { def.finish(); while (!def.finished()) { deflate(); } entry.setSize(def.getTotalIn()); entry.setComprSize(def.getTotalOut()); entry.setCrc(realCrc); def.reset(); written += entry.getCompressedSize(); } else if (raf == null) { if (entry.getCrc() != realCrc) { throw new SwcException.BadCRC(Long.toHexString(entry.getCrc()), Long.toHexString(realCrc)); } if (entry.getSize() != written - dataStart) { throw new SwcException.BadZipSize( entry.getName(), entry.getSize() + "", (written - dataStart) + ""); } } else { /* method is STORED and we used RandomAccessFile */ long size = written - dataStart; entry.setSize(size); entry.setComprSize(size); entry.setCrc(realCrc); } // If random access output, write the local file header containing // the correct CRC and compressed/uncompressed sizes if (raf != null) { long save = raf.getFilePointer(); raf.seek(localDataStart); writeOut((new ZipLong(entry.getCrc())).getBytes()); writeOut((new ZipLong(entry.getCompressedSize())).getBytes()); writeOut((new ZipLong(entry.getSize())).getBytes()); raf.seek(save); } writeDataDescriptor(entry); entry = null; }
private static void deflate(TemporaryBuffer.Heap tinyPack, final byte[] content) throws IOException { final Deflater deflater = new Deflater(); final byte[] buf = new byte[128]; deflater.setInput(content, 0, content.length); deflater.finish(); do { final int n = deflater.deflate(buf, 0, buf.length); if (n > 0) tinyPack.write(buf, 0, n); } while (!deflater.finished()); }
private void deflate() { Deflater deflater = new Deflater(-1); try { deflater.setInput(this.field_149278_f, 0, this.field_149278_f.length); deflater.finish(); byte[] deflated = new byte[this.field_149278_f.length]; this.field_149285_h = deflater.deflate(deflated); this.field_149281_e = deflated; } finally { deflater.end(); } }
public static byte[] zlibCompress(String what) throws UnsupportedEncodingException { byte[] input = what.getBytes("UTF-8"); // Compress the bytes byte[] output = new byte[4096]; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); compresser.deflate(output); return output; }
private void flushInputToStream(final OutputStream pStream) throws IOException { System.out.println("DeflateEncoder.flushInputToStream"); if (deflater.needsInput()) { System.out.println("Foo"); } while (!deflater.needsInput()) { int deflated = deflater.deflate(buffer, 0, buffer.length); pStream.write(buffer, 0, deflated); System.out.println("flushed " + deflated); } }
@Override public void write(byte[] b, int off, int len) throws IOException { if (_def.finished()) throw new IOException("Stream already finished"); if ((off | len | (off + len) | (b.length - (off + len))) < 0) throw new IndexOutOfBoundsException(); if (len == 0) return; if (!_def.finished()) { _def.setInput(b, off, len); while (!_def.needsInput()) { deflate(); } } }
public SpdyHeaderBlockZlibCompressor(int version, int compressionLevel) { if (version < SpdyConstants.SPDY_MIN_VERSION || version > SpdyConstants.SPDY_MAX_VERSION) { throw new IllegalArgumentException("unsupported version: " + version); } if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException( "compressionLevel: " + compressionLevel + " (expected: 0-9)"); } compressor = new Deflater(compressionLevel); if (version < 3) { compressor.setDictionary(SPDY2_DICT); } else { compressor.setDictionary(SPDY_DICT); } }
/** * Construct a new FileInput * * @param filename String * @param compression boolean */ public FileInput(String filename, boolean compression) { deflateIn = null; f = new File(filename); try { fileIn = new FileInputStream(f); if (compression) { // Compressor with highest level of compression Deflater def = new Deflater(); def.setLevel(Deflater.BEST_COMPRESSION); deflateIn = new DeflaterInputStream(fileIn, def); } } catch (FileNotFoundException e) { System.err.println("File not found: " + f); } }
@Override public byte[] compress(final byte[] data, final CodecOptions options) throws FormatException { if (data == null || data.length == 0) throw new IllegalArgumentException("No data to compress"); final Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); final byte[] buf = new byte[8192]; final ByteVector bytes = new ByteVector(); int r = 0; // compress until eof reached while ((r = deflater.deflate(buf, 0, buf.length)) > 0) { bytes.add(buf, 0, r); } return bytes.toByteArray(); }
/** * Begin writing next entry. * * @since 1.1 */ public void putNextEntry(ZipEntry ze) throws IOException { closeEntry(); entry = ze; entries.addElement(entry); if (entry.getMethod() == -1) { // not specified entry.setMethod(method); } if (entry.getTime() == -1) { // not specified entry.setTime(System.currentTimeMillis()); } // Size/CRC not required if RandomAccessFile is used if (entry.getMethod() == STORED && raf == null) { // these exceptions should never happen for us (as we always write to a file) // so they are not localized if (entry.getSize() == -1) { throw new RuntimeException( "uncompressed size is required for" + " STORED method when not writing to a" + " file"); } if (entry.getCrc() == -1) { throw new RuntimeException( "crc checksum is required for STORED" + " method when not writing to a file"); } entry.setComprSize(entry.getSize()); } if (entry.getMethod() == DEFLATED && hasCompressionLevelChanged) { def.setLevel(level); hasCompressionLevelChanged = false; } writeLocalFileHeader(entry); }
/** Writes bytes to ZIP entry. */ public void write(byte[] b, int offset, int length) throws IOException { if (entry.getMethod() == DEFLATED) { if (length > 0) { if (!def.finished()) { def.setInput(b, offset, length); while (!def.needsInput()) { deflate(); } } } } else { writeOut(b, offset, length); written += length; } crc.update(b, offset, length); }
public byte[] compress(final byte[] bytes) { try { deflater.setInput(bytes); deflater.finish(); int len = deflater.deflate(buffer); byte[] compressedBytes = new byte[len]; System.arraycopy(buffer, 0, compressedBytes, 0, len); return compressedBytes; } finally { try { deflater.reset(); } catch (Exception e) { logger.warn("deflater.reset failed", e); } } }
@Override public void close() throws IOException { if (closed) return; // This method delegates to the DeflaterSink for finishing the deflate process // but keeps responsibility for releasing the deflater's resources. This is // necessary because writeFooter needs to query the proccessed byte count which // only works when the defalter is still open. Throwable thrown = null; try { deflaterSink.finishDeflate(); writeFooter(); } catch (Throwable e) { thrown = e; } try { deflater.end(); } catch (Throwable e) { if (thrown == null) thrown = e; } try { sink.close(); } catch (Throwable e) { if (thrown == null) thrown = e; } closed = true; if (thrown != null) Util.sneakyRethrow(thrown); }
/** * Writes next block of compressed data to the output stream. * * @throws IOException on error */ protected final void deflate() throws IOException { int len = def.deflate(buf, 0, buf.length); if (len > 0) { writeOut(buf, 0, len); written += len; } }
/** * Writes new sequence part entry. * * @param accession accession number of original sequence * @param from position of first nucleotide of content of this entry in the original sequence * (e.g. 0 if this entry contains whole sequence) * @param sequence content of this entry * @throws java.io.IOException if an I/O error occurs. */ public void writeSequencePart( String accession, int from, NucleotideSequence sequence, boolean compressed) throws IOException { stream.writeByte(compressed ? SEQUENCE_PART_ENTRY_TYPE_COMPRESSED : SEQUENCE_PART_ENTRY_TYPE); stream.writeUTF(accession); stream.writeInt(from); if (compressed) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); final Deflater def = new Deflater(9, true); DeflaterOutputStream dos = new DeflaterOutputStream(bos, def); SequencesUtils.convertNSequenceToBit2Array(sequence).writeTo(new DataOutputStream(dos)); dos.close(); def.end(); stream.writeInt(bos.size()); stream.write(bos.toByteArray()); } else SequencesUtils.convertNSequenceToBit2Array(sequence).writeTo(stream); }
private static byte[] deflate(byte[] input) { Deflater compressor = new Deflater(Deflater.DEFLATED); compressor.setInput(input); compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[CHUNKSIZE]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } return bos.toByteArray(); }
/** * {@collect.stats} {@description.open} Writes remaining compressed data to the output stream and * closes the underlying stream. {@description.close} * * @exception IOException if an I/O error has occurred */ public void close() throws IOException { if (!closed) { finish(); if (usesDefaultDeflater) def.end(); out.close(); closed = true; } }