java.util.zip.InflaterInputStream Java Examples

The following examples show how to use java.util.zip.InflaterInputStream. 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: DeflateUtils.java    From anthelion with Apache License 2.0 7 votes vote down vote up
/**
 * Returns an inflated copy of the input array.  
 * @throws IOException if the input cannot be properly decompressed
 */
public static final byte[] inflate(byte[] in) throws IOException {
  // decompress using InflaterInputStream 
  ByteArrayOutputStream outStream = 
    new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

  InflaterInputStream inStream = 
    new InflaterInputStream ( new ByteArrayInputStream(in) );

  byte[] buf = new byte[BUF_SIZE];
  while (true) {
    int size = inStream.read(buf);
    if (size <= 0) 
      break;
    outStream.write(buf, 0, size);
  }
  outStream.close();

  return outStream.toByteArray();
}
 
Example #2
Source File: EscherMetafileBlip.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decompresses the provided data, returning the inflated result.
 *
 * @param data the deflated picture data.
 * @return the inflated picture data.
 */
private static byte[] inflatePictureData(byte[] data) {
    try {
        InflaterInputStream in = new InflaterInputStream(
            new ByteArrayInputStream( data ) );
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int readBytes;
        while ((readBytes = in.read(buf)) > 0) {
            out.write(buf, 0, readBytes);
        }
        return out.toByteArray();
    } catch (IOException e) {
        log.log(POILogger.WARN, "Possibly corrupt compression or non-compressed data", e);
        return data;
    }
}
 
Example #3
Source File: GoogleAccountsService.java    From cas4.0.x-server-wechat with Apache License 2.0 6 votes vote down vote up
private static String zlibDeflate(final byte[] bytes) {
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final InflaterInputStream iis = new InflaterInputStream(bais);
    final byte[] buf = new byte[1024];

    try {
        int count = iis.read(buf);
        while (count != -1) {
            baos.write(buf, 0, count);
            count = iis.read(buf);
        }
        return new String(baos.toByteArray());
    } catch (final Exception e) {
        return null;
    } finally {
        IOUtils.closeQuietly(iis);
    }
}
 
Example #4
Source File: EscherPictBlip.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decompresses the provided data, returning the inflated result.
 *
 * @param data the deflated picture data.
 * @return the inflated picture data.
 */
private static byte[] inflatePictureData(byte[] data) {
    try {
        InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(data));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int readBytes;
        while ((readBytes = in.read(buf)) > 0) {
            out.write(buf, 0, readBytes);
        }
        return out.toByteArray();
    } catch (IOException e) {
        log.log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e);
        return data;
    }
}
 
Example #5
Source File: Zlib.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;

    try {
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        buffer = outputStream.toByteArray();
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }

    return buffer;
}
 
Example #6
Source File: HTTPRedirectDeflateDecoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Base64 decodes the SAML message and then decompresses the message.
 * 
 * @param message Base64 encoded, DEFALTE compressed, SAML message
 * 
 * @return the SAML message
 * 
 * @throws MessageDecodingException thrown if the message can not be decoded
 */
protected InputStream decodeMessage(String message) throws MessageDecodingException {
    log.debug("Base64 decoding and inflating SAML message");

    byte[] decodedBytes = Base64.decode(message);
    if(decodedBytes == null){
        log.error("Unable to Base64 decode incoming message");
        throw new MessageDecodingException("Unable to Base64 decode incoming message");
    }
    
    try {
        ByteArrayInputStream bytesIn = new ByteArrayInputStream(decodedBytes);
        InflaterInputStream inflater = new InflaterInputStream(bytesIn, new Inflater(true));
        return inflater;
    } catch (Exception e) {
        log.error("Unable to Base64 decode and inflate SAML message", e);
        throw new MessageDecodingException("Unable to Base64 decode and inflate SAML message", e);
    }
}
 
Example #7
Source File: PdfReader.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** A helper to FlateDecode.
 * @param in the input data
 * @param strict <CODE>true</CODE> to read a correct stream. <CODE>false</CODE>
 * to try to read a corrupted stream
 * @return the decoded data
 */
public static byte[] FlateDecode(byte in[], boolean strict) {
    ByteArrayInputStream stream = new ByteArrayInputStream(in);
    InflaterInputStream zip = new InflaterInputStream(stream);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte b[] = new byte[strict ? 4092 : 1];
    try {
        int n;
        while ((n = zip.read(b)) >= 0) {
            out.write(b, 0, n);
        }
        zip.close();
        out.close();
        return out.toByteArray();
    }
    catch (Exception e) {
        if (strict)
            return null;
        return out.toByteArray();
    }
}
 
Example #8
Source File: ZLibUtil.java    From MonitorClient with Apache License 2.0 6 votes vote down vote up
/** 
 * 解压缩 
 * @param is 输入流        
 * @return byte[] 解压缩后的数据 
 */  
public static byte[] decompress(InputStream is) {  
    InflaterInputStream iis = new InflaterInputStream(is);  
    ByteArrayOutputStream o = new ByteArrayOutputStream(1024);  
    byte[] result = null;
    try {  
        int i = 1024;  
        byte[] buf = new byte[i];  
  
        while ((i = iis.read(buf, 0, i)) > 0) {  
            o.write(buf, 0, i);  
        }  
        result = o.toByteArray();
        o.close();
        iis.close();
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return   result;
}
 
Example #9
Source File: CompressionUtils.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
/**
 * Decode the byte[] in base64 to a string.
 *
 * @param bytes the data to encode
 * @return the new string in {@link #UTF8_ENCODING}.
 */
public static String decodeByteArrayToString(final byte[] bytes) {
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final byte[] buf = new byte[bytes.length];
    try (final InflaterInputStream iis = new InflaterInputStream(bais)) {
        int count = iis.read(buf);
        while (count != -1) {
            baos.write(buf, 0, count);
            count = iis.read(buf);
        }
        return new String(baos.toByteArray(), Charset.forName(UTF8_ENCODING));
    } catch (final Exception e) {
        LOGGER.error("Base64 decoding failed", e);
        return null;
    }
}
 
Example #10
Source File: ContentCompressionTest.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void http() throws Exception {
    final WebClient client =
            WebClient.builder("http://127.0.0.1:" + dogma.serverAddress().getPort())
                     .setHttpHeader(HttpHeaderNames.AUTHORIZATION, "Bearer " + CsrfToken.ANONYMOUS)
                     .setHttpHeader(HttpHeaderNames.ACCEPT_ENCODING, "deflate")
                     .build();

    final String contentPath = HttpApiV1Constants.PROJECTS_PREFIX + '/' + PROJ +
                               HttpApiV1Constants.REPOS + '/' + REPO +
                               "/contents" + PATH;

    final AggregatedHttpResponse compressedResponse = client.get(contentPath).aggregate().join();
    assertThat(compressedResponse.status()).isEqualTo(HttpStatus.OK);

    final HttpData content = compressedResponse.content();
    try (Reader in = new InputStreamReader(new InflaterInputStream(new ByteArrayInputStream(
            content.array(), 0, content.length())), StandardCharsets.UTF_8)) {

        assertThat(CharStreams.toString(in)).contains(CONTENT);
    }
}
 
Example #11
Source File: MCAFile.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
public void streamChunk(byte[] data, RunnableVal<NBTStreamer> withStream) throws IOException {
    if (data != null) {
        try {
            FastByteArrayInputStream nbtIn = new FastByteArrayInputStream(data);
            FastByteArrayInputStream bais = new FastByteArrayInputStream(data);
            InflaterInputStream iis = new InflaterInputStream(bais, new Inflater(), 1);
            fieldBuf2.set(iis, byteStore2.get());
            BufferedInputStream bis = new BufferedInputStream(iis);
            NBTInputStream nis = new NBTInputStream(bis);
            fieldBuf3.set(nis, byteStore3.get());
            NBTStreamer streamer = new NBTStreamer(nis);
            withStream.run(streamer);
            streamer.readQuick();
        } catch (IllegalAccessException unlikely) {
            unlikely.printStackTrace();
        }
    }
}
 
Example #12
Source File: PdfReader.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * A helper to FlateDecode.
 *
 * @param in the input data
 * @param strict <CODE>true</CODE> to read a correct stream. <CODE>false</CODE> to try to read a
 *            corrupted stream
 * @return the decoded data
 */
public static byte[] FlateDecode(byte in[], boolean strict) {
	ByteArrayInputStream stream = new ByteArrayInputStream(in);
	InflaterInputStream zip = new InflaterInputStream(stream);
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	byte b[] = new byte[strict ? 4092 : 1];
	try {
		int n;
		while ((n = zip.read(b)) >= 0) {
			out.write(b, 0, n);
		}
		zip.close();
		out.close();
		return out.toByteArray();
	} catch (Exception e) {
		if (strict) {
			return null;
		}
		return out.toByteArray();
	}
}
 
Example #13
Source File: HttpServerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@ArgumentsSource(ClientAndProtocolProvider.class)
void testStrings_acceptEncodingDeflate(WebClient client) throws Exception {
    final RequestHeaders req = RequestHeaders.of(HttpMethod.GET, "/strings",
                                                 HttpHeaderNames.ACCEPT_ENCODING, "deflate");
    final CompletableFuture<AggregatedHttpResponse> f = client.execute(req).aggregate();

    final AggregatedHttpResponse res = f.get();

    assertThat(res.status()).isEqualTo(HttpStatus.OK);
    assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING)).isEqualTo("deflate");
    assertThat(res.headers().get(HttpHeaderNames.VARY)).isEqualTo("accept-encoding");

    final byte[] decoded;
    try (InflaterInputStream unzipper =
                 new InflaterInputStream(new ByteArrayInputStream(res.content().array()))) {
        decoded = ByteStreams.toByteArray(unzipper);
    }
    assertThat(new String(decoded, StandardCharsets.UTF_8)).isEqualTo("Armeria is awesome!");
}
 
Example #14
Source File: CompressionUtils.java    From rogue-cloud with Apache License 2.0 6 votes vote down vote up
private final static String decompressString(byte[] str) {
	
	// If compression is disabled, then the byte array is just a UTF-8 string
	if(!RCRuntime.ENABLE_DEFLATE_COMPRESSION) {
		return new String(str);
	}

	
	InflaterInputStream dis = new InflaterInputStream(new ByteArrayInputStream(str));
	
	try {
		return readIntoString(dis);
	} catch (IOException e) {
		e.printStackTrace();
		log.severe("Error on decompress",  e, null);
		throw new RuntimeException(e);
	}
}
 
Example #15
Source File: Bytes.java    From dubbox with Apache License 2.0 6 votes vote down vote up
/**
 * unzip.
 * 
 * @param bytes compressed byte array.
 * @return byte uncompressed array.
 * @throws IOException
 */
public static byte[] unzip(byte[] bytes) throws IOException
{
	UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(bytes);
	UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
	InputStream is = new InflaterInputStream(bis);
	try
	{
		IOUtils.write(is, bos);
		return bos.toByteArray();
	}
	finally
	{
		is.close();
		bis.close();
		bos.close();
	}
}
 
Example #16
Source File: ZLibUtils.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
/**
 * 解压缩
 * 
 * @param is 输入流
 * @return byte[] 解压缩后的数据
 */
public static byte[] decompress(InputStream is)
{
	InflaterInputStream iis = new InflaterInputStream(is);
	ByteArrayOutputStream o = new ByteArrayOutputStream(BUFFER_LENGTH);
	try
	{
		byte[] buf = new byte[BUFFER_LENGTH];
		int len = -1;
		while ((len = iis.read(buf)) != -1)
		{
			o.write(buf, 0, len);
		}
	}
	catch (IOException e)
	{
		e.printStackTrace();
	}

	return o.toByteArray();
}
 
Example #17
Source File: S2gsParser.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
private static byte[] extractS2gsData( final File s2gsFile ) throws IOException {
	final byte[] inBuffer = new byte[ 16 ];
	
	try ( final FileInputStream fis = new FileInputStream( s2gsFile ) ) {
		fis.read( inBuffer );
		
		final ByteBuffer header = ByteBuffer.wrap( inBuffer ).order( ByteOrder.LITTLE_ENDIAN );
		header.getInt();                   // Magic word: "ZmpC" ("CmpZ" backward meaning: compressed with Zlib)
		header.getInt();                   // Zeros (reserved? version?)
		final int size = header.getInt() ; // uncompressed data size
		header.getInt();                   // Zeros (reserved? version?)
		
		try ( final InflaterInputStream compressedInputStream = new InflaterInputStream( fis ) ) {
			final byte[] decompressed = new byte[ size ];
			int pos = 0;
			while ( pos < size )
				pos += compressedInputStream.read( decompressed, pos, size - pos );
			
			return decompressed;
		}
	}
}
 
Example #18
Source File: DeflateRFC1951CompressionAlgorithm.java    From Jose4j with Apache License 2.0 6 votes vote down vote up
public byte[] decompress(byte[] compressedData) throws JoseException
{
    Inflater inflater = new Inflater(true);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try (InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(compressedData), inflater))
    {
        int bytesRead;
        byte[] buff = new byte[256];
        while ((bytesRead = iis.read(buff)) != -1)
        {
            byteArrayOutputStream.write(buff, 0, bytesRead);
        }

        return byteArrayOutputStream.toByteArray();
    }
    catch (IOException e)
    {
        throw new JoseException("Problem decompressing data.", e);
    }
}
 
Example #19
Source File: ZlibSingleThreadLowMem.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
synchronized public byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
    byte[] bufferOut;
    int length;

    try {
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        outputStream.flush();
        bufferOut = outputStream.toByteArray();
        outputStream.close();
        inputStream.close();
    }

    return bufferOut;
}
 
Example #20
Source File: JavadocParanamer.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
private InputStream urlToInputStream(URL url) throws IOException {
	URLConnection conn = url.openConnection();
	// pretend to be IE6
	conn.setRequestProperty("User-Agent", IE);
	// allow both GZip and Deflate (ZLib) encodings
	conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
	conn.connect();
	String encoding = conn.getContentEncoding();
	if ((encoding != null) && encoding.equalsIgnoreCase("gzip"))
		return new GZIPInputStream(conn.getInputStream());
	else if ((encoding != null) && encoding.equalsIgnoreCase("deflate"))
		return new InflaterInputStream(conn.getInputStream(), new Inflater(
			true));
	else
		return conn.getInputStream();
}
 
Example #21
Source File: PNGImageReader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] inflate(byte[] b) throws IOException {
    InputStream bais = new ByteArrayInputStream(b);
    InputStream iis = new InflaterInputStream(bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int c;
    try {
        while ((c = iis.read()) != -1) {
            baos.write(c);
        }
    } finally {
        iis.close();
    }
    return baos.toByteArray();
}
 
Example #22
Source File: WZL.java    From mir2.core with Apache License 2.0 5 votes vote down vote up
/** 从zlib解压 
    * @throws IOException */
private static byte[] unzip(byte[] ziped) throws IOException {
	InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(ziped));
	ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
	int i = 1024;
	byte[] buf = new byte[i];

	while ((i = iis.read(buf, 0, i)) > 0) {
		o.write(buf, 0, i);
	}
	return o.toByteArray();
}
 
Example #23
Source File: j.java    From letv with Apache License 2.0 5 votes vote down vote up
public static byte[] b(byte[] bArr) {
    int i = 0;
    if (bArr == null) {
        return null;
    }
    InputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
    InflaterInputStream inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
    Object obj = new byte[0];
    Object obj2 = new byte[1024];
    while (true) {
        try {
            Object obj3;
            int read = inflaterInputStream.read(obj2);
            if (read > 0) {
                i += read;
                obj3 = new byte[i];
                System.arraycopy(obj, 0, obj3, 0, obj.length);
                System.arraycopy(obj2, 0, obj3, obj.length, read);
            } else {
                obj3 = obj;
            }
            if (read <= 0) {
                try {
                    byteArrayInputStream.close();
                    inflaterInputStream.close();
                    return obj3;
                } catch (IOException e) {
                    return null;
                }
            }
            obj = obj3;
        } catch (Exception e2) {
            return null;
        }
    }
}
 
Example #24
Source File: ObjectUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static <T> T readCompressedObject(@Nonnull final byte[] obj)
        throws IOException, ClassNotFoundException {
    FastByteArrayInputStream bis = new FastByteArrayInputStream(obj);
    final InflaterInputStream iis = new InflaterInputStream(bis);
    try {
        return readObject(iis);
    } finally {
        IOUtils.closeQuietly(iis);
    }
}
 
Example #25
Source File: FileSystemTargetRepository.java    From ache with Apache License 2.0 5 votes vote down vote up
private <T> T readFile(Path filePath) throws IOException, FileNotFoundException {
    if (!Files.exists(filePath)) {
        return null;
    }
    try (InputStream fileStream = new FileInputStream(filePath.toFile())) {
        if(compressData) {
            try(InputStream gzipStream = new InflaterInputStream(fileStream)) {
                return unserializeData(gzipStream);
            }
        } else {
            return unserializeData(fileStream);
        }
    }
}
 
Example #26
Source File: Downloader.java    From Downloader with Apache License 2.0 5 votes vote down vote up
private InputStream getInputStream(HttpURLConnection httpURLConnection) throws IOException {
	if ("gzip".equalsIgnoreCase(httpURLConnection.getContentEncoding())) {
		return new GZIPInputStream(httpURLConnection.getInputStream());
	} else if ("deflate".equalsIgnoreCase(httpURLConnection.getContentEncoding())) {
		return new InflaterInputStream(httpURLConnection.getInputStream(), new Inflater(true));
	} else {
		return httpURLConnection.getInputStream();
	}
}
 
Example #27
Source File: AstDeserializer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static FunctionNode deserialize(final byte[] serializedAst) {
    try {
        return (FunctionNode)new ObjectInputStream(new InflaterInputStream(new ByteArrayInputStream(
                serializedAst))).readObject();
    } catch (final ClassNotFoundException | IOException e) {
        // This is internal, can't happen
        throw new AssertionError("Unexpected exception deserializing function", e);
    }
}
 
Example #28
Source File: PNGImageReader.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private static byte[] inflate(byte[] b) throws IOException {
    InputStream bais = new ByteArrayInputStream(b);
    InputStream iis = new InflaterInputStream(bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int c;
    try {
        while ((c = iis.read()) != -1) {
            baos.write(c);
        }
    } finally {
        iis.close();
    }
    return baos.toByteArray();
}
 
Example #29
Source File: Bytes.java    From JobX with Apache License 2.0 5 votes vote down vote up
/**
 * unzip.
 *
 * @param bytes compressed byte array.
 * @return byte uncompressed array.
 * @throws IOException
 */
public static byte[] unzip(byte[] bytes) throws IOException {
    UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(bytes);
    UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
    InputStream is = new InflaterInputStream(bis);
    try {
        IOUtils.write(is, bos);
        return bos.toByteArray();
    } finally {
        is.close();
        bis.close();
        bos.close();
    }
}
 
Example #30
Source File: PNGImageReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static byte[] inflate(byte[] b) throws IOException {
    InputStream bais = new ByteArrayInputStream(b);
    InputStream iis = new InflaterInputStream(bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int c;
    try {
        while ((c = iis.read()) != -1) {
            baos.write(c);
        }
    } finally {
        iis.close();
    }
    return baos.toByteArray();
}