use of java.util.zip.Inflater in project WordPress-Android by wordpress-mobile.
the class Note method buildFromBase64EncodedData.
public static synchronized Note buildFromBase64EncodedData(String noteId, String base64FullNoteData) {
Note note = null;
if (base64FullNoteData == null)
return null;
byte[] b64DecodedPayload = Base64.decode(base64FullNoteData, Base64.DEFAULT);
// Decompress the payload
Inflater decompresser = new Inflater();
decompresser.setInput(b64DecodedPayload, 0, b64DecodedPayload.length);
//max length an Android PN payload can have
byte[] result = new byte[4096];
int resultLength = 0;
try {
resultLength = decompresser.inflate(result);
decompresser.end();
} catch (DataFormatException e) {
AppLog.e(AppLog.T.NOTIFS, "Can't decompress the PN Payload. It could be > 4K", e);
}
String out = null;
try {
out = new String(result, 0, resultLength, "UTF8");
} catch (UnsupportedEncodingException e) {
AppLog.e(AppLog.T.NOTIFS, "Notification data contains non UTF8 characters.", e);
}
if (out != null) {
try {
JSONObject jsonObject = new JSONObject(out);
if (jsonObject.has("notes")) {
JSONArray jsonArray = jsonObject.getJSONArray("notes");
if (jsonArray != null && jsonArray.length() == 1) {
jsonObject = jsonArray.getJSONObject(0);
}
}
note = new Note(noteId, jsonObject);
} catch (JSONException e) {
AppLog.e(AppLog.T.NOTIFS, "Can't parse the Note JSON received in the PN", e);
}
}
return note;
}
use of java.util.zip.Inflater in project oxCore by GluuFederation.
the class CompressionHelper method inflate.
public static byte[] inflate(byte[] data, boolean nowrap) throws IOException, DataFormatException {
Inflater inflater = new Inflater(nowrap);
inflater.setInput(data);
ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
try {
byte[] buffer = new byte[1024];
while (!inflater.finished()) {
int count = inflater.inflate(buffer);
os.write(buffer, 0, count);
}
} finally {
IOUtils.closeQuietly(os);
}
return os.toByteArray();
}
use of java.util.zip.Inflater in project intellij-plugins by JetBrains.
the class SwfTranscoder method readSource.
// in will be closed
protected void readSource(InputStream in, long inputLength) throws IOException {
final int uncompressedBodyLength;
final boolean compressed;
byte[] data;
try {
int n = in.read(partialHeader);
assert n == PARTIAL_HEADER_LENGTH;
uncompressedBodyLength = (partialHeader[4] & 0xFF | (partialHeader[5] & 0xFF) << 8 | (partialHeader[6] & 0xFF) << 16 | partialHeader[7] << 24) - PARTIAL_HEADER_LENGTH;
compressed = partialHeader[0] == 0x43;
data = FileUtil.loadBytes(in, compressed ? (int) inputLength - PARTIAL_HEADER_LENGTH : uncompressedBodyLength);
} finally {
in.close();
}
if (compressed) {
final Inflater inflater = INFLATER.get();
try {
inflater.setInput(data);
byte[] uncompressedData = new byte[uncompressedBodyLength];
try {
inflater.inflate(uncompressedData);
} catch (DataFormatException e) {
throw new ZipException(e.getMessage() != null ? e.getMessage() : "Invalid ZLIB data format");
}
data = uncompressedData;
} finally {
inflater.reset();
}
}
buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
readFrameSizeFrameRateAndFrameCount(data[0]);
}
use of java.util.zip.Inflater in project jdk8u_jdk by JetBrains.
the class PNGImageReader method readImage.
private void readImage(ImageReadParam param) throws IIOException {
readMetadata();
int width = metadata.IHDR_width;
int height = metadata.IHDR_height;
// Init default values
sourceXSubsampling = 1;
sourceYSubsampling = 1;
sourceMinProgressivePass = 0;
sourceMaxProgressivePass = 6;
sourceBands = null;
destinationBands = null;
destinationOffset = new Point(0, 0);
// If an ImageReadParam is available, get values from it
if (param != null) {
sourceXSubsampling = param.getSourceXSubsampling();
sourceYSubsampling = param.getSourceYSubsampling();
sourceMinProgressivePass = Math.max(param.getSourceMinProgressivePass(), 0);
sourceMaxProgressivePass = Math.min(param.getSourceMaxProgressivePass(), 6);
sourceBands = param.getSourceBands();
destinationBands = param.getDestinationBands();
destinationOffset = param.getDestinationOffset();
}
Inflater inf = null;
try {
stream.seek(imageStartPosition);
Enumeration<InputStream> e = new PNGImageDataEnumeration(stream);
InputStream is = new SequenceInputStream(e);
/* InflaterInputStream uses an Inflater instance which consumes
* native (non-GC visible) resources. This is normally implicitly
* freed when the stream is closed. However since the
* InflaterInputStream wraps a client-supplied input stream,
* we cannot close it.
* But the app may depend on GC finalization to close the stream.
* Therefore to ensure timely freeing of native resources we
* explicitly create the Inflater instance and free its resources
* when we are done with the InflaterInputStream by calling
* inf.end();
*/
inf = new Inflater();
is = new InflaterInputStream(is, inf);
is = new BufferedInputStream(is);
this.pixelStream = new DataInputStream(is);
/*
* NB: the PNG spec declares that valid range for width
* and height is [1, 2^31-1], so here we may fail to allocate
* a buffer for destination image due to memory limitation.
*
* However, the recovery strategy for this case should be
* defined on the level of application, so we will not
* try to estimate the required amount of the memory and/or
* handle OOM in any way.
*/
theImage = getDestination(param, getImageTypes(0), width, height);
Rectangle destRegion = new Rectangle(0, 0, 0, 0);
sourceRegion = new Rectangle(0, 0, 0, 0);
computeRegions(param, width, height, theImage, sourceRegion, destRegion);
destinationOffset.setLocation(destRegion.getLocation());
// At this point the header has been read and we know
// how many bands are in the image, so perform checking
// of the read param.
int colorType = metadata.IHDR_colorType;
checkReadParamBandSettings(param, inputBandsForColorType[colorType], theImage.getSampleModel().getNumBands());
processImageStarted(0);
decodeImage();
if (abortRequested()) {
processReadAborted();
} else {
processImageComplete();
}
} catch (IOException e) {
throw new IIOException("Error reading PNG image data", e);
} finally {
if (inf != null) {
inf.end();
}
}
}
use of java.util.zip.Inflater in project ceylon-compiler by ceylon.
the class ZipFileIndex method inflate.
private int inflate(byte[] src, byte[] dest) {
Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());
// construct the inflater object or reuse an existing one
if (inflater == null)
inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));
inflater.reset();
inflater.setInput(src);
try {
return inflater.inflate(dest);
} catch (DataFormatException ex) {
return -1;
}
}
Aggregations