Java Code Examples for java.util.zip.Deflater#reset()

The following examples show how to use java.util.zip.Deflater#reset() . 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: TezCommonUtils.java    From tez with Apache License 2.0 6 votes vote down vote up
@Private
public static ByteString compressByteArrayToByteString(byte[] inBytes, Deflater deflater) throws IOException {
  deflater.reset();
  ByteString.Output os = ByteString.newOutput();
  DeflaterOutputStream compressOs = null;
  try {
    compressOs = new DeflaterOutputStream(os, deflater);
    compressOs.write(inBytes);
    compressOs.finish();
    ByteString byteString = os.toByteString();
    return byteString;
  } finally {
    if (compressOs != null) {
      compressOs.close();
    }
  }
}
 
Example 2
Source File: ZlibThreadLocal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] deflate(byte[] data, int level) throws Exception {
    Deflater deflater = getDef(level);
    if (deflater == null) throw new IllegalArgumentException("No deflate for level " + level + " !");
    deflater.reset();
    deflater.setInput(data);
    deflater.finish();
    FastByteArrayOutputStream bos = ThreadCache.fbaos.get();
    bos.reset();
    byte[] buffer = buf.get();
    while (!deflater.finished()) {
        int i = deflater.deflate(buffer);
        bos.write(buffer, 0, i);
    }
    //Deflater::end is called the time when the process exits.
    return bos.toByteArray();
}
 
Example 3
Source File: ZlibThreadLocal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] deflate(byte[] data, int level) throws IOException {
    Deflater deflater = DEFLATER.get();
    deflater.reset();
    deflater.setLevel(level);
    deflater.setInput(data);
    deflater.finish();
    FastByteArrayOutputStream bos = ThreadCache.fbaos.get();
    bos.reset();
    byte[] buffer = BUFFER.get();
    while (!deflater.finished()) {
        int i = deflater.deflate(buffer);
        bos.write(buffer, 0, i);
    }
    //Deflater::end is called the time when the process exits.
    return bos.toByteArray();
}
 
Example 4
Source File: Zlib.java    From Nemisys with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] deflate(byte[] data, int level) throws Exception {
    Deflater deflater = new Deflater(level);
    deflater.reset();
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    byte[] buf = new byte[1024];
    try {
        while (!deflater.finished()) {
            int i = deflater.deflate(buf);
            bos.write(buf, 0, i);
        }
    } finally {
        deflater.end();
    }
    return bos.toByteArray();
}
 
Example 5
Source File: ZipFileSystem.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 6
Source File: ZipFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 7
Source File: SaveAreaIO.java    From Cubes with MIT License 5 votes vote down vote up
public static boolean write(Save save, Area area) {
  if (save.readOnly) return false;
  if (!area.isReady()) return false;

  AreaMap map = area.areaMap();
  DataGroup[] dataGroups;
  if (map == null || map.world == null || map.world.entities == null) {
    dataGroups = new DataGroup[0];
  } else {
    dataGroups = map.world.entities.getEntitiesForSave(area.areaX, area.areaZ);
  }
  if (!area.modifiedSinceSave(dataGroups)) return false;
  area.saveModCount();

  Deflater deflater = deflaterThreadLocal.get();

  FileHandle file = file(save, area.areaX, area.areaZ);
  try {
    deflater.reset();
    OutputStream stream = file.write(false, 8192);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(stream, deflater);
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(deflaterStream);
    DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
    area.writeSave(dataOutputStream, dataGroups);
    bufferedOutputStream.flush();
    deflaterStream.finish();
    stream.close();
  } catch (Exception e) {
    Log.error("Failed to write area " + area.areaX + "," + area.areaZ, e);
    return false;
  }

  return true;
}
 
Example 8
Source File: CompressionCodecZLib.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf encode(ByteBuf source) {
    byte[] array;
    int length = source.readableBytes();

    int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14;
    ByteBuf compressed = PulsarByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate);

    int offset = 0;
    if (source.hasArray()) {
        array = source.array();
        offset = source.arrayOffset() + source.readerIndex();
    } else {
        // If it's a direct buffer, we need to copy it
        array = new byte[length];
        source.getBytes(source.readerIndex(), array);
    }

    Deflater deflater = this.deflater.get();
    deflater.reset();
    deflater.setInput(array, offset, length);
    while (!deflater.needsInput()) {
        deflate(deflater, compressed);
    }

    return compressed;
}
 
Example 9
Source File: ZipFileSystem.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 10
Source File: ZipFileSystem.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 11
Source File: MatrixZoomDataPP.java    From Juicebox with MIT License 5 votes vote down vote up
/**
 * todo should this be synchronized?
 *
 * @param data
 * @param compressor
 * @return
 */
protected byte[] compress(byte[] data, Deflater compressor) {

    // Give the compressor the data to compress
    compressor.reset();
    compressor.setInput(data);
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
        System.err.println("Error clossing ByteArrayOutputStream");
        e.printStackTrace();
    }

    return bos.toByteArray();
}
 
Example 12
Source File: ZipFileSystem.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 13
Source File: ZipFileSystem.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 14
Source File: ZipFileSystem.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 15
Source File: ZipFileSystem.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 16
Source File: BinaryCasSerDes4.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Method:
 *   write with deflation into a single byte array stream
 *     skip if not worth deflating
 *     skip the Slot_Control stream
 *     record in the Slot_Control stream, for each deflated stream:
 *       the Slot index
 *       the number of compressed bytes
 *       the number of uncompressed bytes
 *   add to header:  
 *     nbr of compressed entries
 *     the Slot_Control stream size
 *     the Slot_Control stream
 *     all the zipped streams
 *
 * @throws IOException passthru
 */
private void collectAndZip() throws IOException {
  ByteArrayOutputStream baosZipped = new ByteArrayOutputStream(4096);
  Deflater deflater = new Deflater(compressLevel.lvl, true);
  deflater.setStrategy(compressStrategy.strat);
  int nbrEntries = 0;
  
  List<Integer> idxAndLen = new ArrayList<>();

  for (int i = 0; i < baosZipSources.length; i++) {
    ByteArrayOutputStream baos = baosZipSources[i];
    if (baos != null) {
      nbrEntries ++;
      dosZipSources[i].close();
      long startTime = System.currentTimeMillis();
      int zipBufSize = Math.max(1024, baos.size() / 100);
      deflater.reset();
      DeflaterOutputStream cds = new DeflaterOutputStream(baosZipped, deflater, zipBufSize);       
      baos.writeTo(cds);
      cds.close();
      idxAndLen.add(i);
      if (doMeasurement) {
        idxAndLen.add((int) (sm.statDetails[i].afterZip = deflater.getBytesWritten()));
        idxAndLen.add((int) (sm.statDetails[i].beforeZip = deflater.getBytesRead()));
        sm.statDetails[i].zipTime = System.currentTimeMillis() - startTime;
      } else {
        idxAndLen.add((int) deflater.getBytesWritten());
        idxAndLen.add((int) deflater.getBytesRead());
      }
    } 
  }
  serializedOut.writeInt(nbrEntries);                     // write number of entries
  for (int i = 0; i < idxAndLen.size();) {
    serializedOut.write(idxAndLen.get(i++));
    serializedOut.writeInt(idxAndLen.get(i++));
    serializedOut.writeInt(idxAndLen.get(i++));
  }
  baosZipped.writeTo(serializedOut);                      // write Compressed info
}
 
Example 17
Source File: ZipFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 18
Source File: ZipReaderTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Test public void testRawFileData() throws IOException {
  CRC32 crc = new CRC32();
  Deflater deflator = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
  try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {
    ZipEntry foo = new ZipEntry("foo");
    foo.setComment("foo comment.");
    foo.setMethod(ZipEntry.DEFLATED);
    zout.putNextEntry(foo);
    zout.write("foo".getBytes(UTF_8));
    zout.closeEntry();

    ZipEntry bar = new ZipEntry("bar");
    bar.setComment("bar comment.");
    bar.setMethod(ZipEntry.STORED);
    bar.setSize("bar".length());
    bar.setCompressedSize("bar".length());
    crc.reset();
    crc.update("bar".getBytes(UTF_8));
    bar.setCrc(crc.getValue());
    zout.putNextEntry(bar);
    zout.write("bar".getBytes(UTF_8));
    zout.closeEntry();
  }

  try (ZipReader reader = new ZipReader(test, UTF_8)) {
    ZipFileEntry fooEntry = reader.getEntry("foo");
    InputStream fooIn = reader.getRawInputStream(fooEntry);
    byte[] fooData = new byte[10];
    fooIn.read(fooData);
    byte[] expectedFooData = new byte[10];
    deflator.reset();
    deflator.setInput("foo".getBytes(UTF_8));
    deflator.finish();
    deflator.deflate(expectedFooData);
    assertThat(fooData).isEqualTo(expectedFooData);

    ZipFileEntry barEntry = reader.getEntry("bar");
    InputStream barIn = reader.getRawInputStream(barEntry);
    byte[] barData = new byte[3];
    barIn.read(barData);
    byte[] expectedBarData = "bar".getBytes(UTF_8);
    assertThat(barData).isEqualTo(expectedBarData);

    assertThat(barIn.read()).isEqualTo(-1);
    assertThat(barIn.read(barData)).isEqualTo(-1);
    assertThat(barIn.read(barData, 0, 3)).isEqualTo(-1);

    thrown.expect(IOException.class);
    thrown.expectMessage("Reset is not supported on this type of stream.");
    barIn.reset();
  }
}
 
Example 19
Source File: ZipReaderTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
@Test public void testZipEntryFields() throws IOException {
  CRC32 crc = new CRC32();
  Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
  long date = 791784306000L; // 2/3/1995 04:05:06
  byte[] extra = new ExtraData((short) 0xaa, new byte[] { (byte) 0xbb, (byte) 0xcd }).getBytes();
  byte[] tmp = new byte[128];
  try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) {

    ZipEntry foo = new ZipEntry("foo");
    foo.setComment("foo comment.");
    foo.setMethod(ZipEntry.DEFLATED);
    foo.setTime(date);
    foo.setExtra(extra);
    zout.putNextEntry(foo);
    zout.write("foo".getBytes(UTF_8));
    zout.closeEntry();

    ZipEntry bar = new ZipEntry("bar");
    bar.setComment("bar comment.");
    bar.setMethod(ZipEntry.STORED);
    bar.setSize("bar".length());
    bar.setCompressedSize("bar".length());
    crc.reset();
    crc.update("bar".getBytes(UTF_8));
    bar.setCrc(crc.getValue());
    zout.putNextEntry(bar);
    zout.write("bar".getBytes(UTF_8));
    zout.closeEntry();
  }

  try (ZipReader reader = new ZipReader(test, UTF_8)) {
    ZipFileEntry fooEntry = reader.getEntry("foo");
    assertThat(fooEntry.getName()).isEqualTo("foo");
    assertThat(fooEntry.getComment()).isEqualTo("foo comment.");
    assertThat(fooEntry.getMethod()).isEqualTo(Compression.DEFLATED);
    assertThat(fooEntry.getVersion()).isEqualTo(Compression.DEFLATED.getMinVersion());
    assertThat(fooEntry.getTime()).isEqualTo(date);
    assertThat(fooEntry.getSize()).isEqualTo("foo".length());
    deflater.reset();
    deflater.setInput("foo".getBytes(UTF_8));
    deflater.finish();
    assertThat(fooEntry.getCompressedSize()).isEqualTo(deflater.deflate(tmp));
    crc.reset();
    crc.update("foo".getBytes(UTF_8));
    assertThat(fooEntry.getCrc()).isEqualTo(crc.getValue());
    assertThat(fooEntry.getExtra().getBytes()).isEqualTo(extra);

    ZipFileEntry barEntry = reader.getEntry("bar");
    assertThat(barEntry.getName()).isEqualTo("bar");
    assertThat(barEntry.getComment()).isEqualTo("bar comment.");
    assertThat(barEntry.getMethod()).isEqualTo(Compression.STORED);
    assertThat(barEntry.getVersion()).isEqualTo(Compression.STORED.getMinVersion());
    assertDateAboutNow(new Date(barEntry.getTime()));
    assertThat(barEntry.getSize()).isEqualTo("bar".length());
    assertThat(barEntry.getCompressedSize()).isEqualTo("bar".length());
    crc.reset();
    crc.update("bar".getBytes(UTF_8));
    assertThat(barEntry.getCrc()).isEqualTo(crc.getValue());
    assertThat(barEntry.getExtra().getBytes()).isEqualTo(new byte[] {});
  }
}
 
Example 20
Source File: UnorderedPartitionedKVWriter.java    From tez with Apache License 2.0 4 votes vote down vote up
@Override
public Deflater get() {
  Deflater deflater = super.get();
  deflater.reset();
  return deflater;
}