use of java.util.zip.InflaterInputStream in project elasticsearch by elastic.
the class DeflateCompressor method streamInput.
@Override
public StreamInput streamInput(StreamInput in) throws IOException {
final byte[] headerBytes = new byte[HEADER.length];
int len = 0;
while (len < headerBytes.length) {
final int read = in.read(headerBytes, len, headerBytes.length - len);
if (read == -1) {
break;
}
len += read;
}
if (len != HEADER.length || Arrays.equals(headerBytes, HEADER) == false) {
throw new IllegalArgumentException("Input stream is not compressed with DEFLATE!");
}
final boolean nowrap = true;
final Inflater inflater = new Inflater(nowrap);
InputStream decompressedIn = new InflaterInputStream(in, inflater, BUFFER_SIZE);
decompressedIn = new BufferedInputStream(decompressedIn, BUFFER_SIZE);
return new InputStreamStreamInput(decompressedIn) {
final AtomicBoolean closed = new AtomicBoolean(false);
public void close() throws IOException {
try {
super.close();
} finally {
if (closed.compareAndSet(false, true)) {
// important to release native memory
inflater.end();
}
}
}
};
}
use of java.util.zip.InflaterInputStream in project jersey by jersey.
the class DeflateEncoder method decode.
@Override
public InputStream decode(String contentEncoding, InputStream encodedStream) throws IOException {
// correct impl. should wrap deflate in zlib, but some don't do it - have to identify, which one we got
InputStream markSupportingStream = encodedStream.markSupported() ? encodedStream : new BufferedInputStream(encodedStream);
markSupportingStream.mark(1);
// read the first byte
int firstByte = markSupportingStream.read();
markSupportingStream.reset();
// that should never be the case if no zlib wrapper
if ((firstByte & 15) == 8) {
// ok, zlib wrapped stream
return new InflaterInputStream(markSupportingStream);
} else {
// no zlib wrapper
return new InflaterInputStream(markSupportingStream, new Inflater(true));
}
}
use of java.util.zip.InflaterInputStream in project nutz by nutzam.
the class Sender method createResponse.
protected Response createResponse(Map<String, String> reHeaders) throws IOException {
Response rep = null;
if (reHeaders != null) {
rep = new Response(conn, reHeaders);
if (rep.isOK()) {
InputStream is1 = conn.getInputStream();
InputStream is2 = null;
String encoding = conn.getContentEncoding();
// 如果采用了压缩,则需要处理否则都是乱码
if (encoding != null && encoding.contains("gzip")) {
is2 = new GZIPInputStream(is1);
} else if (encoding != null && encoding.contains("deflate")) {
is2 = new InflaterInputStream(is1, new Inflater(true));
} else {
is2 = is1;
}
BufferedInputStream is = new BufferedInputStream(is2);
rep.setStream(is);
} else {
try {
rep.setStream(conn.getInputStream());
} catch (IOException e) {
try {
rep.setStream(conn.getErrorStream());
} catch (Exception e1) {
rep.setStream(new VoidInputStream());
}
}
}
}
if (this.interceptor != null)
this.interceptor.afterResponse(request, conn, rep);
return rep;
}
use of java.util.zip.InflaterInputStream in project OpenAM by OpenRock.
the class IOUtils method deserialise.
/**
* Deserialises an object from a byte array to an object of a specified type.
*
* @param bytes The bytes that represent the Object to be deserialized. The classes to be loaded must be from the
* set specified in the whitelist maintained in the <code>WhitelistObjectInputStream</code>
* @param compressed If true, expect that the bytes are compressed.
* @param classLoader Used in place of the default ClassLoader, default will be used if null.
* @param <T> The returned object type.
* @return The Object T representing the deserialized bytes
* @throws IOException If there was a problem with the ObjectInputStream process.
* @throws ClassNotFoundException If there was problem loading a class that makes up the bytes to be deserialized.
*/
public static <T> T deserialise(byte[] bytes, boolean compressed, ClassLoader classLoader) throws IOException, ClassNotFoundException {
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
final ObjectInputStream ois = compressed ? new WhitelistObjectInputStream(new InflaterInputStream(bais), classLoader) : new WhitelistObjectInputStream(bais, classLoader);
final T result;
try {
result = (T) ois.readObject();
} finally {
closeIfNotNull(ois);
}
return result;
}
use of java.util.zip.InflaterInputStream in project voltdb by VoltDB.
the class Encoder method decodeBase64AndDecompressToBytes.
public static byte[] decodeBase64AndDecompressToBytes(String string) {
byte[] bytes = Base64.decodeFast(string);
if (string.length() == 0) {
return new byte[0];
}
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InflaterInputStream dis = new InflaterInputStream(bais);
byte[] buffer = new byte[1024 * 8];
int length = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
while ((length = dis.read(buffer)) >= 0) {
baos.write(buffer, 0, length);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return baos.toByteArray();
}
Aggregations