java.util.zip.Inflater#finished ( )源码实例Demo

下面列出了java.util.zip.Inflater#finished ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: big-c   文件: PyroProxy.java
/**
 * Decompress the data bytes in the given message (in place).
 */
private void _decompressMessageData(Message msg) {
	if((msg.flags & Message.FLAGS_COMPRESSED) == 0) {
		throw new IllegalArgumentException("message data is not compressed");
	}
	Inflater decompresser = new Inflater();
	decompresser.setInput(msg.data);
	ByteArrayOutputStream bos = new ByteArrayOutputStream(msg.data.length);
	byte[] buffer = new byte[8192];
	try {
		while (!decompresser.finished()) {
			int size = decompresser.inflate(buffer);
			bos.write(buffer, 0, size);
		}
		msg.data = bos.toByteArray();
		msg.flags &= ~Message.FLAGS_COMPRESSED;
		decompresser.end();
	} catch (DataFormatException e) {
		throw new PyroException("invalid compressed data: ", e);
	}
}
 
public  String decodeByteBuff(ByteBuf buf) throws IOException, DataFormatException {
   
   	   byte[] temp = new byte[buf.readableBytes()];
   	   ByteBufInputStream bis = new ByteBufInputStream(buf);
	   bis.read(temp);
	   bis.close();
	   Inflater decompresser = new Inflater(true);
	   decompresser.setInput(temp, 0, temp.length);
	   StringBuilder sb = new StringBuilder();
	   byte[] result = new byte[1024];
	   while (!decompresser.finished()) {
			int resultLength = decompresser.inflate(result);
			sb.append(new String(result, 0, resultLength, "UTF-8"));
	   }
	   decompresser.end();
          return sb.toString();
}
 
源代码3 项目: opsu-dance   文件: PNGDecoder.java
private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException {
    try {
        do {
            int read = inflater.inflate(buffer, offset, length);
            if(read <= 0) {
                if(inflater.finished()) {
                    throw new EOFException();
                }
                if(inflater.needsInput()) {
                    refillInflater(inflater);
                } else {
                    throw new IOException("Can't inflate " + length + " bytes");
                }
            } else {
                offset += read;
                length -= read;
            }
        } while(length > 0);
    } catch (DataFormatException ex) {
        throw (IOException)(new IOException("inflate error").initCause(ex));
    }
}
 
源代码4 项目: xyz-hub   文件: Compression.java
/**
 * 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());
  }
}
 
源代码5 项目: kylin-on-parquet-v2   文件: CompressionUtils.java
public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    long startTime = System.currentTimeMillis();
    Inflater inflater = new Inflater();
    inflater.setInput(data);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    logger.debug("Original: " + data.length + " bytes. " + "Decompressed: " + output.length + " bytes. Time: " + (System.currentTimeMillis() - startTime));
    return output;
}
 
源代码6 项目: saros   文件: XMPPAccountStore.java
private static byte[] inflate(byte[] input) throws IOException {

      ByteArrayOutputStream bos;
      Inflater decompressor = new Inflater();

      decompressor.setInput(input, 0, input.length);
      bos = new ByteArrayOutputStream(input.length);

      byte[] buf = new byte[CHUNK_SIZE];

      try {
        while (!decompressor.finished()) {
          int count = decompressor.inflate(buf);
          bos.write(buf, 0, count);
        }
        return bos.toByteArray();
      } catch (DataFormatException e) {
        throw new IOException("failed to inflate data", e);
      }
    }
 
源代码7 项目: kaitai_struct_java_runtime   文件: KaitaiStream.java
/**
 * 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();
}
 
源代码8 项目: cxf   文件: CompressionUtils.java
public static InputStream inflate(byte[] deflatedToken, boolean nowrap)
    throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(deflatedToken);

    byte[] buffer = new byte[deflatedToken.length];
    int inflateLen;
    ByteArrayOutputStream inflatedToken = new ByteArrayOutputStream();
    while (!inflater.finished()) {
        inflateLen = inflater.inflate(buffer, 0, deflatedToken.length);
        if (inflateLen == 0 && !inflater.finished()) {
            if (inflater.needsInput()) {
                throw new DataFormatException("Inflater can not inflate all the token bytes");
            }
            break;
        }

        inflatedToken.write(buffer, 0, inflateLen);
    }

    return new ByteArrayInputStream(inflatedToken.toByteArray());
}
 
源代码9 项目: xDrip-plus   文件: JoH.java
public static String uncompressString(String input) {
    try {
        byte[] bytes = Base64.decode(input, Base64.NO_WRAP);
        Inflater inflater = new Inflater();
        inflater.setInput(bytes);
        inflater.finished();

        byte[] buf = new byte[10000]; // max packet size because not using stream
        int count = inflater.inflate(buf);
        inflater.end();
        Log.d(TAG, "Inflated bytes: " + count);
        return new String(buf, 0, count, "UTF-8");
    } catch (Exception e) {
        Log.e(TAG, "Got exception uncompressing string");
        return null;
    }
}
 
源代码10 项目: EosCommander   文件: PackedTransaction.java
private byte[] decompress( byte [] compressedBytes ) {
    Inflater inflater = new Inflater();
    inflater.setInput( compressedBytes );

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( compressedBytes.length);
    byte[] buffer = new byte[1024];

    try {
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
    }
    catch (DataFormatException | IOException e) {
        e.printStackTrace();
        return compressedBytes;
    }


    return outputStream.toByteArray();
}
 
源代码11 项目: tez   文件: TezUtilsInternal.java
private static byte[] uncompressBytesInflateDeflate(byte[] inBytes) throws IOException {
  Inflater inflater = new Inflater();
  inflater.setInput(inBytes);
  NonSyncByteArrayOutputStream bos = new NonSyncByteArrayOutputStream(inBytes.length);
  byte[] buffer = new byte[1024 * 8];
  while (!inflater.finished()) {
    int count;
    try {
      count = inflater.inflate(buffer);
    } catch (DataFormatException e) {
      throw new IOException(e);
    }
    bos.write(buffer, 0, count);
  }
  byte[] output = bos.toByteArray();
  return output;
}
 
源代码12 项目: EosProxyServer   文件: PackedTransaction.java
private byte[] decompress( byte [] compressedBytes ) {
    Inflater inflater = new Inflater();
    inflater.setInput( compressedBytes );

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( compressedBytes.length);
    byte[] buffer = new byte[1024];

    try {
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
    }
    catch (DataFormatException | IOException e) {
        e.printStackTrace();
        return compressedBytes;
    }


    return outputStream.toByteArray();
}
 
源代码13 项目: development   文件: RedirectEncoderTest.java
private String decodeURLBase64DeflateString(final String input)
        throws UnsupportedEncodingException, DataFormatException {
    String urlDecoded = URLDecoder.decode(input, "UTF-8");
    byte[] base64Decoded = Base64.decodeBase64(urlDecoded);

    Inflater decompresser = new Inflater(true);
    decompresser.setInput(base64Decoded);
    StringBuilder result = new StringBuilder();

    while (!decompresser.finished()) {
        byte[] outputFraction = new byte[base64Decoded.length];
        int resultLength = decompresser.inflate(outputFraction);
        result.append(new String(outputFraction, 0, resultLength, "UTF-8"));
    }

    return result.toString();
}
 
源代码14 项目: j2objc   文件: DeflaterTest.java
/**
 * java.util.zip.Deflater#deflate(byte[])
 */
public void test_deflate$B() {
    byte outPutBuf[] = new byte[50];
    byte byteArray[] = { 1, 3, 4, 7, 8 };
    byte outPutInf[] = new byte[50];
    int x = 0;

    Deflater defl = new Deflater();
    defl.setInput(byteArray);
    defl.finish();
    while (!defl.finished()) {
        x += defl.deflate(outPutBuf);
    }
    assertEquals("Deflater at end of stream, should return 0", 0, defl
            .deflate(outPutBuf));
    int totalOut = defl.getTotalOut();
    int totalIn = defl.getTotalIn();
    assertEquals(x, totalOut);
    assertEquals(byteArray.length, totalIn);
    defl.end();

    Inflater infl = new Inflater();
    try {
        infl.setInput(outPutBuf);
        while (!infl.finished()) {
            infl.inflate(outPutInf);
        }
    } catch (DataFormatException e) {
        fail("Invalid input to be decompressed");
    }
    assertEquals(totalIn, infl.getTotalOut());
    assertEquals(totalOut, infl.getTotalIn());
    for (int i = 0; i < byteArray.length; i++) {
        assertEquals(byteArray[i], outPutInf[i]);
    }
    assertEquals("Final decompressed data contained more bytes than original",
            0, outPutInf[byteArray.length]);
    infl.end();
}
 
源代码15 项目: Telegram-FOSS   文件: Slice.java
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;
}
 
源代码16 项目: moleculer-java   文件: CommonUtils.java
public static final byte[] decompress(byte[] data) throws IOException, DataFormatException {
	Inflater inflater = new Inflater(true);
	inflater.setInput(data);
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
	byte[] buffer = new byte[1024];
	while (!inflater.finished()) {
		int count = inflater.inflate(buffer);
		outputStream.write(buffer, 0, count);
	}
	return outputStream.toByteArray();
}
 
源代码17 项目: jhdf   文件: DeflatePipelineFilter.java
@Override
public byte[] decode(byte[] compressedData, int[] filterData) {
	try {
		// Setup the inflater
		final Inflater inflater = new Inflater();
		inflater.setInput(compressedData);

		// Make a guess that the decompressed data is 3 times larger than compressed.
		// This is a performance optimisation to avoid resizing of the stream byte
		// array.
		final ByteArrayOutputStream baos = new ByteArrayOutputStream(compressedData.length * 3);

		final byte[] buffer = new byte[4096];

		// Do the decompression
		while (!inflater.finished()) {
			int read = inflater.inflate(buffer);
			baos.write(buffer, 0, read);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("Decompressed chunk. Compressed size = {} bytes, Decompressed size = {}",
					inflater.getBytesRead(),
					inflater.getBytesWritten());
		}

		// Close the inflater
		inflater.end();

		return baos.toByteArray();

	} catch (DataFormatException e) {
		throw new HdfFilterException("Inflating failed", e);
	}
}
 
/**
 * Returns the non-null InputStream that should be returned to by all
 * requests to {@link #getContent()}.
 * 
 * @return a non-null InputStream
 * @throws IOException
 *             if there was a problem
 */
@Override
InputStream getDecompressingInputStream(final InputStream wrapped)
        throws IOException {
    /*
     * A zlib stream will have a header.
     * 
     * CMF | FLG [| DICTID ] | ...compressed data | ADLER32 |
     * 
     * * CMF is one byte.
     * 
     * * FLG is one byte.
     * 
     * * DICTID is four bytes, and only present if FLG.FDICT is set.
     * 
     * Sniff the content. Does it look like a zlib stream, with a CMF, etc?
     * c.f. RFC1950, section 2.2. http://tools.ietf.org/html/rfc1950#page-4
     * 
     * We need to see if it looks like a proper zlib stream, or whether it
     * is just a deflate stream. RFC2616 calls zlib streams deflate.
     * Confusing, isn't it? That's why some servers implement deflate
     * Content-Encoding using deflate streams, rather than zlib streams.
     * 
     * We could start looking at the bytes, but to be honest, someone else
     * has already read the RFCs and implemented that for us. So we'll just
     * use the JDK libraries and exception handling to do this. If that
     * proves slow, then we could potentially change this to check the first
     * byte - does it look like a CMF? What about the second byte - does it
     * look like a FLG, etc.
     */

    /* We read a small buffer to sniff the content. */
    final byte[] peeked = new byte[6];

    final PushbackInputStream pushback = new PushbackInputStream(wrapped,
            peeked.length);

    final int headerLength = pushback.read(peeked);

    if (headerLength == -1) {
        throw new IOException("Unable to read the response");
    }

    /* We try to read the first uncompressed byte. */
    final byte[] dummy = new byte[1];

    final Inflater inf = new Inflater();

    try {
        int n;
        while ((n = inf.inflate(dummy)) == 0) {
            if (inf.finished()) {

                /* Not expecting this, so fail loudly. */
                throw new IOException("Unable to read the response");
            }

            if (inf.needsDictionary()) {

                /*
                 * Need dictionary - then it must be zlib stream with DICTID
                 * part?
                 */
                break;
            }

            if (inf.needsInput()) {
                inf.setInput(peeked);
            }
        }

        if (n == -1) {
            throw new IOException("Unable to read the response");
        }

        /*
         * We read something without a problem, so it's a valid zlib stream.
         * Just need to reset and return an unused InputStream now.
         */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback);
    } catch (final DataFormatException e) {

        /*
         * Presume that it's an RFC1951 deflate stream rather than RFC1950
         * zlib stream and try again.
         */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback, new Inflater(true));
    }
}
 
源代码19 项目: stratio-cassandra   文件: CassandraServer.java
private static String uncompress(ByteBuffer query, Compression compression) throws InvalidRequestException
{
    String queryString = null;

    // Decompress the query string.
    try
    {
        switch (compression)
        {
            case GZIP:
                DataOutputBuffer decompressed = new DataOutputBuffer();
                byte[] outBuffer = new byte[1024], inBuffer = new byte[1024];

                Inflater decompressor = new Inflater();

                int lenRead = 0;
                while (true)
                {
                    if (decompressor.needsInput())
                        lenRead = query.remaining() < 1024 ? query.remaining() : 1024;
                    query.get(inBuffer, 0, lenRead);
                    decompressor.setInput(inBuffer, 0, lenRead);

                    int lenWrite = 0;
                    while ((lenWrite = decompressor.inflate(outBuffer)) != 0)
                        decompressed.write(outBuffer, 0, lenWrite);

                    if (decompressor.finished())
                        break;
                }

                decompressor.end();

                queryString = new String(decompressed.getData(), 0, decompressed.getLength(), StandardCharsets.UTF_8);
                break;
            case NONE:
                try
                {
                    queryString = ByteBufferUtil.string(query);
                }
                catch (CharacterCodingException ex)
                {
                    throw new InvalidRequestException(ex.getMessage());
                }
                break;
        }
    }
    catch (DataFormatException e)
    {
        throw new InvalidRequestException("Error deflating query string.");
    }
    return queryString;
}
 
源代码20 项目: j2objc   文件: DeflaterTest.java
/**
 * java.util.zip.Deflater#reset()
 */
public void test_reset() {
    byte outPutBuf[] = new byte[100];
    byte outPutInf[] = new byte[100];
    byte curArray[] = new byte[5];
    byte byteArray[] = { 1, 3, 4, 7, 8 };
    byte byteArray2[] = { 8, 7, 4, 3, 1 };
    int x = 0;
    int orgValue = 0;
    Deflater defl = new Deflater();

    for (int i = 0; i < 3; i++) {
        if (i == 0) {
            curArray = byteArray;
        } else if (i == 1) {
            curArray = byteArray2;
        } else {
            defl.reset();
        }

        defl.setInput(curArray);
        defl.finish();
        while (!defl.finished()) {
            x += defl.deflate(outPutBuf);
        }

        if (i == 0) {
            assertEquals(x, defl.getTotalOut());
        } else if (i == 1) {
            assertEquals(x, orgValue);
        } else {
            assertEquals(x, orgValue * 2);
        }

        if (i == 0) {
            orgValue = x;
        }

        try {
            Inflater infl = new Inflater();
            infl.setInput(outPutBuf);
            while (!infl.finished()) {
                infl.inflate(outPutInf);
            }
            infl.end();
        } catch (DataFormatException e) {
            fail("Test " + i + ": Invalid input to be decompressed");
        }

        if (i == 1) {
            curArray = byteArray;
        }

        for (int j = 0; j < curArray.length; j++) {
            assertEquals(curArray[j], outPutInf[j]);
        }
        assertEquals(0, outPutInf[curArray.length]);
    }
    defl.end();
}