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

The following examples show how to use java.util.zip.Inflater#end() . 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: PacketChunkInfo.java    From LookingGlass with GNU General Public License v3.0 6 votes vote down vote up
private byte[] inflateChunkData(ByteBuf in, int compressedsize, int uncompressedsize) {
	if (inflatearray == null || inflatearray.length < compressedsize) {
		inflatearray = new byte[compressedsize];
	}
	in.readBytes(inflatearray, 0, compressedsize);
	byte[] chunkData = new byte[uncompressedsize];
	Inflater inflater = new Inflater();
	inflater.setInput(inflatearray, 0, compressedsize);

	try {
		inflater.inflate(chunkData);
	} catch (DataFormatException e) {
		return null;
	} finally {
		inflater.end();
	}
	return chunkData;
}
 
Example 2
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 3
Source File: SVGAParser.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
private byte[] inflate(byte[] bytes) {
    try {
        Inflater inflater = new Inflater();
        inflater.setInput(bytes, 0, bytes.length);
        byte[] inflaterBytes = new byte[2048];
        ByteArrayOutputStream inflatedOutputStream = new ByteArrayOutputStream();
        while (true) {
            int count = inflater.inflate(inflaterBytes, 0, 2048);
            if (count <= 0) {
                break;
            } else {
                inflatedOutputStream.write(inflaterBytes, 0, count);
            }
        }
        inflater.end();
        return inflatedOutputStream.toByteArray();
    } catch (Exception e) {
    }
    return null;

}
 
Example 4
Source File: BiliDanmukuCompressionTools.java    From HeroVideo-master with Apache License 2.0 6 votes vote down vote up
public static byte[] decompress(byte[] value) throws DataFormatException
{

    ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);

    Inflater decompressor = new Inflater();

    try
    {
        decompressor.setInput(value);

        final byte[] buf = new byte[1024];
        while (!decompressor.finished())
        {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }
    } finally
    {
        decompressor.end();
    }

    return bos.toByteArray();
}
 
Example 5
Source File: BinaryCasSerDes4.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void closeDataInputs() {
  for (DataInputStream is : dataInputs) {
    if (null != is){
      try {
        is.close();
      } catch (IOException e) {
      }
    }
  }
  // release any space inflater holding on to
  for (Inflater inflater : inflaters) {
    if (null != inflater) {
      inflater.end();
    }
  }
}
 
Example 6
Source File: Zlib.java    From krpc with Apache License 2.0 6 votes vote down vote up
public byte[] unzip(byte[] input) throws IOException {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
        byte[] buff = tlBuff.get();
        Inflater infl = new Inflater();
        infl.setInput(input);
        while (!infl.finished()) {
            int len = infl.inflate(buff);
            if (len == 0) {
                break;
            }
            bos.write(buff, 0, len);
        }
        infl.end();
        bos.flush();
        return bos.toByteArray();
    } catch (DataFormatException e) {
        throw new IOException("unzip error", e);
    }

}
 
Example 7
Source File: Compression.java    From xyz-hub with Apache License 2.0 6 votes vote down vote up
/**
 * Decompress a byte array which was compressed using Deflate.
 * @param bytearray non-null byte array to be decompressed
 * @return the decompressed payload or an empty array in case of bytearray is null
 * @throws DataFormatException in case the payload cannot be decompressed
 */
public static byte[] decompressUsingInflate(byte[] bytearray) throws DataFormatException {
  if (bytearray == null) return new byte[0];

  try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    final Inflater inflater = new Inflater();
    final byte[] buff = new byte[1024];

    inflater.setInput(bytearray);

    while (!inflater.finished()) {
      int count = inflater.inflate(buff);
      bos.write(buff, 0, count);
    }

    inflater.end();
    bos.flush();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new DataFormatException(e.getMessage());
  }
}
 
Example 8
Source File: KaitaiStream.java    From kaitai_struct_java_runtime with MIT License 6 votes vote down vote up
/**
 * Performs an unpacking ("inflation") of zlib-compressed data with usual zlib headers.
 * @param data data to unpack
 * @return unpacked data
 * @throws RuntimeException if data can't be decoded
 */
public static byte[] processZlib(byte[] data) {
    Inflater ifl = new Inflater();
    ifl.setInput(data);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buf[] = new byte[ZLIB_BUF_SIZE];
    while (!ifl.finished()) {
        try {
            int decBytes = ifl.inflate(buf);
            baos.write(buf, 0, decBytes);
        } catch (DataFormatException e) {
            throw new RuntimeException(e);
        }
    }
    ifl.end();
    return baos.toByteArray();
}
 
Example 9
Source File: ZlibCodec.java    From hive-dwrf with Apache License 2.0 6 votes vote down vote up
@Override
public void decompress(ByteBuffer in, ByteBuffer out) throws IOException {
  Inflater inflater = new Inflater(true);
  inflater.setInput(in.array(), in.arrayOffset() + in.position(),
                    in.remaining());
  while (!(inflater.finished() || inflater.needsDictionary() ||
           inflater.needsInput())) {
    try {
      int count = inflater.inflate(out.array(),
                                   out.arrayOffset() + out.position(),
                                   out.remaining());
      out.position(count + out.position());
    } catch (DataFormatException dfe) {
      throw new IOException("Bad compression data", dfe);
    }
  }
  out.flip();
  inflater.end();
  in.position(in.limit());
}
 
Example 10
Source File: TransferUtil.java    From Tatala-RPC with Apache License 2.0 6 votes vote down vote up
private static byte[] getInputByCompress(byte[] toByteArray) throws DataFormatException{
    TransferInputStream fis = new TransferInputStream(toByteArray);
    int unCompressedLength = fis.readInt();
    int compressedLength = fis.readInt();

    byte[] input = new byte[compressedLength];
    fis.readFully(input, 0, compressedLength);
    byte[] output = new byte[unCompressedLength];
    
    Inflater decompresser = new Inflater();
 decompresser.setInput(input);
 decompresser.inflate(output);
 decompresser.end();

    return getInputByNormal(output);
}
 
Example 11
Source File: TezCommonUtils.java    From tez with Apache License 2.0 5 votes vote down vote up
@Private
public static byte[] decompressByteStringToByteArray(ByteString byteString) throws IOException {
  Inflater inflater = newInflater();
  try {
    return decompressByteStringToByteArray(byteString, inflater);
  } finally {
    inflater.end();
  }
}
 
Example 12
Source File: Slice.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public ByteBuffer getData() {
    try {
        byte[] input = new byte[1024];
        byte[] output = new byte[1024];
        FileInputStream fin = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Inflater inflater = new Inflater(true);

        while (true) {
            int numRead = fin.read(input);
            if (numRead != -1) {
                inflater.setInput(input, 0, numRead);
            }

            int numDecompressed;
            while ((numDecompressed = inflater.inflate(output, 0, output.length)) != 0) {
                bos.write(output, 0, numDecompressed);
            }

            if (inflater.finished()) {
                break;
            }
            else if (inflater.needsInput()) {
                continue;
            }
        }

        inflater.end();
        ByteBuffer result = ByteBuffer.wrap(bos.toByteArray(), 0, bos.size());

        bos.close();
        fin.close();

        return result;
    } catch (Exception e) {
        FileLog.e(e);
    }

    return null;
}
 
Example 13
Source File: ShortClientSession.java    From Tatala-RPC with Apache License 2.0 5 votes vote down vote up
private Object doInCompress(InputStream is) throws IOException,	DataFormatException, SocketExecuteException {
	// in
	int unCompressedLength = TransferUtil.readInt(is);
	int compressedLength = TransferUtil.readInt(is);
	byte[] input = new byte[compressedLength];
	is.read(input);
	byte[] output = new byte[unCompressedLength];
	Inflater decompresser = new Inflater();
	decompresser.setInput(input);
	decompresser.inflate(output);
	decompresser.end();

	InputStream istemp = new ByteArrayInputStream(output);
	return TransferObject.convertReturnInputStream(istemp);
}
 
Example 14
Source File: ZipFileSystem.java    From openjdk-jdk8u 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: 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 16
Source File: PNGDecoder.java    From opsu-dance with GNU General Public License v3.0 4 votes vote down vote up
public void decode(ByteBuffer buffer, int stride, Format fmt) throws IOException {
    final int offset = buffer.position();
    final int lineSize = ((width * bitdepth + 7) / 8) * bytesPerPixel;
    byte[] curLine = new byte[lineSize+1];
    byte[] prevLine = new byte[lineSize+1];
    byte[] palLine = (bitdepth < 8) ? new byte[width+1] : null;
    
    final Inflater inflater = new Inflater();
    try {
        for(int y=0 ; y<height ; y++) {
            readChunkUnzip(inflater, curLine, 0, curLine.length);
            unfilter(curLine, prevLine);

            buffer.position(offset + y*stride);

            switch (colorType) {
            case COLOR_TRUECOLOR:
                if (fmt == ABGR) {
                	copyRGBtoABGR(buffer, curLine); 
                }
                else if (fmt == RGBA) {
                	copyRGBtoRGBA(buffer, curLine); 
                }
                else if (fmt == BGRA) {
                	copyRGBtoBGRA(buffer, curLine); 
                }
                else if (fmt == RGB) {
                	copy(buffer, curLine); 
                } else {
                	throw new UnsupportedOperationException("Unsupported format for this image");
                }
                break;
            case COLOR_TRUEALPHA:
                if (fmt == ABGR) {
                	copyRGBAtoABGR(buffer, curLine);
                } else if (fmt == RGBA) { 
                	copy(buffer, curLine);
                } else if (fmt == BGRA) {
                	copyRGBAtoBGRA(buffer, curLine); break;
                } else if (fmt == RGB) {
                	copyRGBAtoRGB(buffer, curLine); break;
                } else {
                    throw new UnsupportedOperationException("Unsupported format for this image");
                }
                break;
            case COLOR_GREYSCALE:
                if ((fmt == LUMINANCE) || (fmt == ALPHA)) {
                	copy(buffer, curLine);
                } else {
                	throw new UnsupportedOperationException("Unsupported format for this image");
                }
                break;
            case COLOR_GREYALPHA:
                if (fmt == LUMINANCE_ALPHA) {
                	copy(buffer, curLine);
                } else {
                	throw new UnsupportedOperationException("Unsupported format for this image");
                }
                break;
            case COLOR_INDEXED:
                switch(bitdepth) {
                    case 8: palLine = curLine; break;
                    case 4: expand4(curLine, palLine); break;
                    case 2: expand2(curLine, palLine); break;
                    case 1: expand1(curLine, palLine); break;
                    default: throw new UnsupportedOperationException("Unsupported bitdepth for this image");
                }
                if (fmt == ABGR) {
                	copyPALtoABGR(buffer, palLine);
                } else if (fmt == RGBA) {
                	copyPALtoRGBA(buffer, palLine);
                } else if (fmt == BGRA) {
                	copyPALtoBGRA(buffer, palLine);
                } else if (fmt == RGB) {
                    // yugecin 13 jan 2019: added this else if branch
                	copyPALtoRGB(buffer, palLine);
                } else {
                	throw new UnsupportedOperationException("Unsupported format for this image");
                }
                break;
            default:
                throw new UnsupportedOperationException("Not yet implemented");
            }

            byte[] tmp = curLine;
            curLine = prevLine;
            prevLine = tmp;
        }
    } finally {
        inflater.end();
    }
}
 
Example 17
Source File: ZipFileSystem.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    beginWrite();
    try {
        if (!isOpen)
            return;
        isOpen = false;             // set closed
    } finally {
        endWrite();
    }
    if (!streams.isEmpty()) {       // unlock and close all remaining streams
        Set<InputStream> copy = new HashSet<>(streams);
        for (InputStream is: copy)
            is.close();
    }
    beginWrite();                   // lock and sync
    try {
        sync();
        ch.close();                 // close the ch just in case no update
    } finally {                     // and sync dose not close the ch
        endWrite();
    }

    synchronized (inflaters) {
        for (Inflater inf : inflaters)
            inf.end();
    }
    synchronized (deflaters) {
        for (Deflater def : deflaters)
            def.end();
    }

    beginWrite();                // lock and sync
    try {
        // Clear the map so that its keys & values can be garbage collected
        inodes = null;
    } finally {
        endWrite();
    }

    IOException ioe = null;
    synchronized (tmppaths) {
        for (Path p: tmppaths) {
            try {
                Files.deleteIfExists(p);
            } catch (IOException x) {
                if (ioe == null)
                    ioe = x;
                else
                    ioe.addSuppressed(x);
            }
        }
    }
    provider.removeFileSystem(zfpath, this);
    if (ioe != null)
       throw ioe;
}
 
Example 18
Source File: ZipHelper.java    From AcgClub with MIT License 4 votes vote down vote up
/**
 * zlib decompress 2 byte
 */
public static byte[] decompressForZlib(byte[] bytesToDecompress) {
  byte[] returnValues = null;

  Inflater inflater = new Inflater();

  int numberOfBytesToDecompress = bytesToDecompress.length;

  inflater.setInput
      (
          bytesToDecompress,
          0,
          numberOfBytesToDecompress
      );

  int bufferSizeInBytes = numberOfBytesToDecompress;

  int numberOfBytesDecompressedSoFar = 0;
  List<Byte> bytesDecompressedSoFar = new ArrayList<Byte>();

  try {
    while (inflater.needsInput() == false) {
      byte[] bytesDecompressedBuffer = new byte[bufferSizeInBytes];

      int numberOfBytesDecompressedThisTime = inflater.inflate
          (
              bytesDecompressedBuffer
          );

      numberOfBytesDecompressedSoFar += numberOfBytesDecompressedThisTime;

      for (int b = 0; b < numberOfBytesDecompressedThisTime; b++) {
        bytesDecompressedSoFar.add(bytesDecompressedBuffer[b]);
      }
    }

    returnValues = new byte[bytesDecompressedSoFar.size()];
    for (int b = 0; b < returnValues.length; b++) {
      returnValues[b] = (byte) (bytesDecompressedSoFar.get(b));
    }

  } catch (DataFormatException dfe) {
    dfe.printStackTrace();
  }

  inflater.end();

  return returnValues;
}
 
Example 19
Source File: ZipFileSystem.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    beginWrite();
    try {
        if (!isOpen)
            return;
        isOpen = false;             // set closed
    } finally {
        endWrite();
    }
    if (!streams.isEmpty()) {       // unlock and close all remaining streams
        Set<InputStream> copy = new HashSet<>(streams);
        for (InputStream is: copy)
            is.close();
    }
    beginWrite();                   // lock and sync
    try {
        sync();
        ch.close();                 // close the ch just in case no update
    } finally {                     // and sync dose not close the ch
        endWrite();
    }

    synchronized (inflaters) {
        for (Inflater inf : inflaters)
            inf.end();
    }
    synchronized (deflaters) {
        for (Deflater def : deflaters)
            def.end();
    }

    IOException ioe = null;
    synchronized (tmppaths) {
        for (Path p: tmppaths) {
            try {
                Files.deleteIfExists(p);
            } catch (IOException x) {
                if (ioe == null)
                    ioe = x;
                else
                    ioe.addSuppressed(x);
            }
        }
    }
    provider.removeFileSystem(zfpath, this);
    if (ioe != null)
       throw ioe;
}