use of java.util.zip.InflaterInputStream in project teaTime by ancfdy.
the class AppZLibMgr method decompress.
/**
* 解压缩
*
* @param is
* 输入流
* @return byte[] 解压缩后的数据
*/
public static byte[] decompress(InputStream is) {
InflaterInputStream iis = new InflaterInputStream(is);
ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
try {
int i = 1024;
byte[] buf = new byte[i];
while ((i = iis.read(buf, 0, i)) > 0) {
o.write(buf, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
}
return o.toByteArray();
}
use of java.util.zip.InflaterInputStream in project commons by twitter.
the class Base64ZlibCodec method decompress.
private static byte[] decompress(byte[] compressed) throws InvalidDataException {
byte[] bytes;
try {
final InputStream bin = new ByteArrayInputStream(compressed);
final InputStream zin;
if (startsWith(compressed, GZIP_HEADER_PREFIX)) {
zin = new GZIPInputStream(bin);
} else if (startsWith(compressed, ZLIB_HEADER_PREFIX)) {
zin = new InflaterInputStream(bin);
} else {
throw new Base64ZlibCodec.InvalidDataException("Value doesn't start with either GZIP or zlib header");
}
try {
bytes = ByteStreams.toByteArray(zin);
} finally {
zin.close();
}
} catch (IOException e) {
throw new Base64ZlibCodec.InvalidDataException("zlib/GZIP decoding error", e);
}
return bytes;
}
use of java.util.zip.InflaterInputStream in project hazelcast by hazelcast.
the class MetricsCompressor method readDictionary.
private static String[] readDictionary(byte[] dictionaryBlob) throws IOException {
try (DataInputStream dis = new DataInputStream(new InflaterInputStream(new ByteArrayInputStream(dictionaryBlob)))) {
int dictionarySize = dis.readInt();
String[] dictionary = new String[dictionarySize];
String lastWord = "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dictionarySize; i++) {
int dictionaryId = dis.readInt();
int commonLen = dis.readUnsignedByte();
int diffLen = dis.readUnsignedByte();
sb.append(lastWord, 0, commonLen);
for (int j = 0; j < diffLen; j++) {
sb.append(dis.readChar());
}
String readWord = sb.toString();
lastWord = readWord;
dictionary[dictionaryId] = readWord;
sb.setLength(0);
}
return dictionary;
}
}
use of java.util.zip.InflaterInputStream in project jena by apache.
the class HttpLib method getInputStream.
/**
* Get the InputStream from an HttpResponse, handling possible compression settings.
* The application must consume or close the {@code InputStream} (see {@link #finish(InputStream)}).
* Closing the InputStream may close the HTTP connection.
* Assumes the status code has been handled e.g. {@link #handleHttpStatusCode} has been called.
*/
public static InputStream getInputStream(HttpResponse<InputStream> httpResponse) {
String encoding = httpResponse.headers().firstValue(HttpNames.hContentEncoding).orElse("");
InputStream responseInput = httpResponse.body();
// "Content-Encoding: chunked, <compression>"
try {
switch(encoding) {
case "":
case // Proper name for no compression.
"identity":
return responseInput;
case "gzip":
return new GZIPInputStream(responseInput, 2 * 1024);
case "inflate":
return new InflaterInputStream(responseInput);
// RFC7932
case "br":
default:
throw new UnsupportedOperationException("Not supported: Content-Encoding: " + encoding);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
use of java.util.zip.InflaterInputStream in project j2objc by google.
the class DeflaterOutputStreamTest method test_write$BII.
/**
* java.util.zip.DeflaterOutputStream#write(byte[], int, int)
*/
/* J2ObjC removed: not supported by Junit 4.11 (https://github.com/google/j2objc/issues/1318).
@DisableResourceLeakageDetection(
why = "DeflaterOutputStream.close() does not work properly if finish() throws an"
+ " exception; DeflaterOutputStream.finish() throws an exception if the"
+ " underlying OutputStream has been closed and the Deflater still has data to"
+ " write.",
bug = "31797037") */
public void test_write$BII() throws Exception {
byte[] byteArray = { 1, 3, 4, 7, 8, 3, 6 };
// Test to see if the correct bytes are saved.
File f1 = File.createTempFile("writeBII", ".tst");
FileOutputStream fos1 = new FileOutputStream(f1);
DeflaterOutputStream dos1 = new DeflaterOutputStream(fos1);
dos1.write(byteArray, 2, 3);
dos1.close();
FileInputStream fis = new FileInputStream(f1);
InflaterInputStream iis = new InflaterInputStream(fis);
assertEquals("Incorrect Byte Returned.", 4, iis.read());
assertEquals("Incorrect Byte Returned.", 7, iis.read());
assertEquals("Incorrect Byte Returned.", 8, iis.read());
assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
assertEquals("Incorrect Byte Returned (EOF).", -1, iis.read());
iis.close();
f1.delete();
// Test for trying to write more bytes than available from the array
File f2 = File.createTempFile("writeBII2", ".tst");
FileOutputStream fos2 = new FileOutputStream(f2);
DeflaterOutputStream dos2 = new DeflaterOutputStream(fos2);
try {
dos2.write(byteArray, 2, 10);
fail("IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException e) {
}
// Test for trying to write a negative number of bytes.
try {
dos2.write(byteArray, 2, Integer.MIN_VALUE);
fail("IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException e) {
}
// Test for trying to start writing from a byte < 0 from the array.
try {
dos2.write(byteArray, Integer.MIN_VALUE, 2);
fail("IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException e) {
}
// size.
try {
dos2.write(byteArray, 10, 2);
fail("IndexOutOfBoundsException not thrown");
} catch (IndexOutOfBoundsException e) {
}
dos2.close();
// Not sure if this test is that important.
// Checks to see if you can write using the DeflaterOutputStream
// after
// the FileOutputStream has been closed.
FileOutputStream fos3 = new FileOutputStream(f2);
DeflaterOutputStream dos3 = new DeflaterOutputStream(fos3);
fos3.close();
try {
dos3.write(byteArray, 2, 3);
fail("IOException not thrown");
} catch (IOException e) {
}
// Close to try and free up the resources.
try {
dos3.close();
fail("IOException not thrown");
} catch (IOException e) {
}
f2.delete();
}
Aggregations