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

The following examples show how to use java.util.zip.Inflater#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: SaveAreaIO.java    From Cubes with MIT License 6 votes vote down vote up
public static Area read(Save save, int x, int z) {
  FileHandle file = file(save, x, z);

  if (!file.exists()) {
    //Log.warning("Area does not exist");
    return null;
  }

  Inflater inflater = inflaterThreadLocal.get();

  try {
    inflater.reset();
    InputStream inputStream = file.read(8192);
    InflaterInputStream inflaterInputStream = new InflaterInputStream(inputStream, inflater);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inflaterInputStream);
    DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
    Area area = new Area(x, z);
    area.read(dataInputStream);
    dataInputStream.close();
    return area;
  } catch (Exception e) {
    Log.error("Failed to read area " + x + "," + z, e);
    return null;
  }
}
 
Example 2
Source File: ZlibThreadLocal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] inflate(byte[] data, int maxSize) throws IOException {
    Inflater inflater = INFLATER.get();
    inflater.reset();
    inflater.setInput(data);
    inflater.finished();
    FastByteArrayOutputStream bos = ThreadCache.fbaos.get();
    bos.reset();

    byte[] buffer = BUFFER.get();
    try {
        int length = 0;
        while (!inflater.finished()) {
            int i = inflater.inflate(buffer);
            length += i;
            if (maxSize > 0 && length >= maxSize) {
                throw new IOException("Inflated data exceeds maximum size");
            }
            bos.write(buffer, 0, i);
        }
        return bos.toByteArray();
    } catch (DataFormatException e) {
        throw new IOException("Unable to inflate zlib stream", e);
    }
}
 
Example 3
Source File: InflaterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.Inflater#setInput(byte[], int, int)
 */
public void test_setInput$BII() {
    // test method of java.util.zip.inflater.setInput(byte,int,int)
    byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
    int offSet = 6;
    int length = 6;
    Inflater inflate = new Inflater();
    inflate.setInput(byteArray, offSet, length);
    assertEquals(
            "setInputBII did not deliver the right number of bytes to the input buffer",
            length, inflate.getRemaining());
    // boundary check
    inflate.reset();
    int r = 0;
    try {
        inflate.setInput(byteArray, 100, 100);
    } catch (ArrayIndexOutOfBoundsException e) {
        r = 1;
    }
    inflate.end();
    assertEquals("boundary check is not present for setInput", 1, r);
}
 
Example 4
Source File: InflaterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.zip.Inflater#needsInput()
 */
public void test_needsInput() {
    // test method of java.util.zip.inflater.needsInput()
    Inflater inflate = new Inflater();
    assertTrue(
            "needsInput give the wrong boolean value as a result of no input buffer",
            inflate.needsInput());

    byte byteArray[] = { 2, 3, 4, 't', 'y', 'u', 'e', 'w', 7, 6, 5, 9 };
    inflate.setInput(byteArray);
    assertFalse(
            "methodNeedsInput returned true when the input buffer is full",
            inflate.needsInput());

    inflate.reset();
    byte byteArrayEmpty[] = new byte[0];
    inflate.setInput(byteArrayEmpty);
    assertTrue(
            "needsInput give wrong boolean value as a result of an empty input buffer",
            inflate.needsInput());
    inflate.end();
}
 
Example 5
Source File: ZipFileIndex.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private int inflate(byte[] src, byte[] dest) {
    Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());

    // construct the inflater object or reuse an existing one
    if (inflater == null)
        inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));

    inflater.reset();
    inflater.setInput(src);
    try {
        return inflater.inflate(dest);
    } catch (DataFormatException ex) {
        return -1;
    }
}
 
Example 6
Source File: ZipFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void releaseInflater(Inflater inf) {
    synchronized (inflaters) {
        if (inflaters.size() < MAX_FLATER) {
            inf.reset();
            inflaters.add(inf);
        } else {
            inf.end();
        }
    }
}
 
Example 7
Source File: ZipFileSystem.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void releaseInflater(Inflater inf) {
    synchronized (inflaters) {
        if (inflaters.size() < MAX_FLATER) {
            inf.reset();
            inflaters.add(inf);
        } else {
            inf.end();
        }
    }
}
 
Example 8
Source File: ZipFileSystem.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseInflater(Inflater inf) {
    synchronized (inflaters) {
        if (inflaters.size() < MAX_FLATER) {
            inf.reset();
            inflaters.add(inf);
        } else {
            inf.end();
        }
    }
}
 
Example 9
Source File: ZipFileSystem.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void releaseInflater(Inflater inf) {
    synchronized (inflaters) {
        if (inflaters.size() < MAX_FLATER) {
            inf.reset();
            inflaters.add(inf);
        } else {
            inf.end();
        }
    }
}
 
Example 10
Source File: ZipFileIndex.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private int inflate(byte[] src, byte[] dest) {
    Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());

    // construct the inflater object or reuse an existing one
    if (inflater == null)
        inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));

    inflater.reset();
    inflater.setInput(src);
    try {
        return inflater.inflate(dest);
    } catch (DataFormatException ex) {
        return -1;
    }
}
 
Example 11
Source File: InflaterInputStreamTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testInflaterInputStreamWithExternalInflater_3ArgConstructor() throws Exception {
    InputStream base = new ByteArrayInputStream(new byte[]{'h', 'i'});
    Inflater inf = new Inflater();

    InflaterInputStream iis = new InflaterInputStream(base, inf, 512);
    iis.close();
    try {
        inf.reset();
        fail();
    } catch (NullPointerException expected) {
        // Expected because the inflater should've been closed when the stream was.
    }
}
 
Example 12
Source File: ZipFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseInflater(Inflater inf) {
    synchronized (inflaters) {
        if (inflaters.size() < MAX_FLATER) {
            inf.reset();
            inflaters.add(inf);
        } else {
            inf.end();
        }
    }
}
 
Example 13
Source File: ZipFileIndex.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private int inflate(byte[] src, byte[] dest) {
    Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());

    // construct the inflater object or reuse an existing one
    if (inflater == null)
        inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));

    inflater.reset();
    inflater.setInput(src);
    try {
        return inflater.inflate(dest);
    } catch (DataFormatException ex) {
        return -1;
    }
}
 
Example 14
Source File: ZipFileSystem.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void releaseInflater(Inflater inf) {
    synchronized (inflaters) {
        if (inflaters.size() < MAX_FLATER) {
            inf.reset();
            inflaters.add(inf);
        } else {
            inf.end();
        }
    }
}
 
Example 15
Source File: ZipFileIndex.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private int inflate(byte[] src, byte[] dest) {
    Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());

    // construct the inflater object or reuse an existing one
    if (inflater == null)
        inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));

    inflater.reset();
    inflater.setInput(src);
    try {
        return inflater.inflate(dest);
    } catch (DataFormatException ex) {
        return -1;
    }
}
 
Example 16
Source File: CompressionCodecZLib.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PulsarByteBufAllocator.DEFAULT.heapBuffer(uncompressedLength, uncompressedLength);

    int len = encoded.readableBytes();

    byte[] array;
    int offset;
    if (encoded.hasArray()) {
        array = encoded.array();
        offset = encoded.arrayOffset() + encoded.readerIndex();
    } else {
        array = new byte[len];
        encoded.getBytes(encoded.readerIndex(), array);
        offset = 0;
    }

    int resultLength;
    Inflater inflater = this.inflater.get();
    inflater.reset();
    inflater.setInput(array, offset, len);

    try {
        resultLength = inflater.inflate(uncompressed.array(), uncompressed.arrayOffset(), uncompressedLength);
    } catch (DataFormatException e) {
        throw new IOException(e);
    }

    checkArgument(resultLength == uncompressedLength);

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}
 
Example 17
Source File: ZipFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseInflater(Inflater inf) {
    synchronized (inflaters) {
        if (inflaters.size() < MAX_FLATER) {
            inf.reset();
            inflaters.add(inf);
        } else {
            inf.end();
        }
    }
}
 
Example 18
Source File: GzipProcessorUtils.java    From datakernel with Apache License 2.0 4 votes vote down vote up
private static void moveDecompressorToPool(Inflater decompressor) {
	decompressor.reset();
	decompressors.push(decompressor);
}
 
Example 19
Source File: Fysatiosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Array readCompressedZlib(ucar.nc2.Variable v2, long dataPos, int nx, int ny, int[] origin, int[] shape,
    int[] stride) throws IOException, InvalidRangeException {

  long length = raf.length();

  raf.seek(dataPos);

  int data_size = (int) (length - dataPos); // or 5120 as read buffer size
  byte[] data = new byte[data_size];
  raf.readFully(data);

  // decompress the bytes
  int resultLength;
  int result = 0;
  byte[] tmp;
  int uncompLen; /* length of decompress space */
  byte[] uncomp = new byte[nx * (ny + 1) + 4000];
  Inflater inflater = new Inflater(false);

  inflater.setInput(data, 0, data_size);
  int offset = 0;
  int limit = nx * ny + nx;

  while (inflater.getRemaining() > 0) {
    try {
      resultLength = inflater.inflate(uncomp, offset, 4000);

    } catch (DataFormatException ex) {
      ex.printStackTrace();
      throw new IOException(ex.getMessage());
    }

    offset = offset + resultLength;
    result = result + resultLength;
    if ((result) > limit) {
      // when uncomp data larger then limit, the uncomp need to increase size
      tmp = new byte[result];
      System.arraycopy(uncomp, 0, tmp, 0, result);
      uncompLen = result + 4000;
      uncomp = new byte[uncompLen];
      System.arraycopy(tmp, 0, uncomp, 0, result);
    }

    if (resultLength == 0) {
      int tt = inflater.getRemaining();
      byte[] b2 = new byte[2];
      System.arraycopy(data, data_size - tt, b2, 0, 2);
      if (isZlibHed(b2) == 0) {
        System.arraycopy(data, data_size - tt, uncomp, result, tt);
        break;
      }
      inflater.reset();
      inflater.setInput(data, data_size - tt, tt);
    }

  }

  inflater.end();

  byte[] inflateData = new byte[nx * ny];
  System.arraycopy(uncomp, 0, inflateData, 0, nx * ny);
  Array array = Array.factory(DataType.BYTE, v2.getShape(), inflateData);
  if (array.getSize() < Variable.defaultSizeToCache)
    v2.setCachedData(array, false);
  return array.sectionNoReduce(origin, shape, stride);
}
 
Example 20
Source File: Giniiosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Array readCompressedZlib(ucar.nc2.Variable v2, long dataPos, int nx, int ny, List<Range> ranges, int[] levels)
    throws IOException, InvalidRangeException {
  // Get to the proper offset and read in the rest of the compressed data
  raf.seek(dataPos);
  int data_size = (int) (raf.length() - dataPos); // or 5120 as read buffer size
  byte[] data = new byte[data_size];
  raf.readFully(data);

  // Buffer for decompressing data
  byte[] uncomp = new byte[nx * ny];
  int offset = 0;

  // Set-up zlib decompression (inflation)
  Inflater inflater = new Inflater(false);
  inflater.setInput(data);

  // Loop while the inflater has data and we have space in final buffer
  // This will end up ignoring the last few compressed bytes, which
  // correspond to the end of file marker, which is a single row of pixels
  // of alternating 0/255.
  while (inflater.getRemaining() > 0 && offset < uncomp.length) {
    // Try to decompress what's left, which ends up decompressing one block
    try {
      offset += inflater.inflate(uncomp, offset, uncomp.length - offset);
    } catch (DataFormatException ex) {
      throw new IOException(ex);
    }

    // If the last block finished...
    if (inflater.finished()) {
      // See if anything's left
      int bytesLeft = inflater.getRemaining();
      if (bytesLeft > 0) {
        // Figure out where we are in the input data
        int inputOffset = data_size - bytesLeft;

        // Check if remaining data are zlib--if not copy out and bail
        byte[] b2 = new byte[2];
        System.arraycopy(data, inputOffset, b2, 0, b2.length);
        if (!isZlibHed(b2)) {
          System.arraycopy(data, inputOffset, uncomp, offset, bytesLeft);
          break;
        }

        // Otherwise, set up for decompressing next block
        inflater.reset();
        inflater.setInput(data, inputOffset, bytesLeft);
      }
    }
  }
  inflater.end();

  // Turn the decompressed data into an array, caching as appropriate
  Array array = makeArray(uncomp, levels, v2.getShape());
  if (levels == null && array.getSize() < Variable.defaultSizeToCache)
    v2.setCachedData(array, false);
  return array.sectionNoReduce(ranges);
}