List of usage examples for java.util.zip InflaterInputStream InflaterInputStream
public InflaterInputStream(InputStream in)
From source file:gov.nasa.ensemble.common.CommonUtils.java
public static byte[] decompress(byte[] input) { try {//from w w w . j a va 2 s .c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); InflaterInputStream ios = new InflaterInputStream(new ByteArrayInputStream(input)); IOUtils.copy(ios, baos); return baos.toByteArray(); } catch (IOException e) { throw new Error(e); } }
From source file:org.apache.nutch.protocol.Content.java
public final void readFields(DataInput in) throws IOException { metadata.clear();// ww w . j a v a 2s . c om int sizeOrVersion = in.readInt(); if (sizeOrVersion < 0) { // version version = sizeOrVersion; switch (version) { case VERSION: url = Text.readString(in); base = Text.readString(in); content = new byte[in.readInt()]; in.readFully(content); contentType = Text.readString(in); metadata.readFields(in); break; default: throw new VersionMismatchException((byte) VERSION, (byte) version); } } else { // size byte[] compressed = new byte[sizeOrVersion]; in.readFully(compressed, 0, compressed.length); ByteArrayInputStream deflated = new ByteArrayInputStream(compressed); DataInput inflater = new DataInputStream(new InflaterInputStream(deflated)); readFieldsCompressed(inflater); } }
From source file:org.mcxiaoke.commons.http.impl.DeflateDecompressingEntity.java
/** * Returns the non-null InputStream that should be returned to by all * requests to {@link #getContent()}.//from w w w .ja va 2 s . c om * * @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. */ byte[] peeked = new byte[6]; PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.length); int headerLength = pushback.read(peeked); if (headerLength == -1) { throw new IOException("Unable to read the response"); } /* We try to read the first uncompressed byte. */ byte[] dummy = new byte[1]; 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 (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)); } }
From source file:com.fanfou.app.opensource.http.support.DeflateDecompressingEntity.java
/** * Returns the non-null InputStream that should be returned to by all * requests to {@link #getContent()}./*from w w w.j ava 2 s.c om*/ * * @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)); } }
From source file:com.apporiented.hermesftp.cmd.AbstractFtpCmdStor.java
/** * Creates an input stream that supports unstructured file data. * //from w w w .jav a 2 s. c o m * @param is The nested input stream. * @param mode The transmission mode. * @param charset The encoding or null if binary. * @param restartMarkers Optional map that stores restart markers. * @return The stream object. * @throws UnsupportedEncodingException Thrown if encoding is unknown. */ private InputStream createInputStream(InputStream is, int mode, Map<Long, Long> restartMarkers, String charset) throws UnsupportedEncodingException { InputStream result = null; if (mode == MODE_BLOCK) { byte[] eorBytes = getEorBytes(null); result = new BlockModeInputStream(is, eorBytes, restartMarkers); } else if (mode == MODE_STREAM) { result = is; } else if (mode == MODE_ZIP) { result = new InflaterInputStream(is); } else { log.error("Unsupported file mode: " + mode); } if (charset != null) { result = new TextInputStream(is, charset); } return result; }
From source file:org.bimserver.servlets.UploadServlet.java
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getHeader("Origin") != null && !getBimServer().getServerSettingsCache().isHostAllowed(request.getHeader("Origin"))) { response.setStatus(403);//from w w w . java2 s . c o m return; } response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); response.setHeader("Access-Control-Allow-Headers", "Content-Type"); String token = (String) request.getSession().getAttribute("token"); ObjectNode result = OBJECT_MAPPER.createObjectNode(); response.setContentType("text/json"); try { boolean isMultipart = ServletFileUpload.isMultipartContent(request); long poid = -1; String comment = null; if (isMultipart) { ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iter = upload.getItemIterator(request); InputStream in = null; String name = ""; long deserializerOid = -1; boolean merge = false; boolean sync = false; String compression = null; String action = null; long topicId = -1; while (iter.hasNext()) { FileItemStream item = iter.next(); if (item.isFormField()) { if ("action".equals(item.getFieldName())) { action = Streams.asString(item.openStream()); } else if ("token".equals(item.getFieldName())) { token = Streams.asString(item.openStream()); } else if ("poid".equals(item.getFieldName())) { poid = Long.parseLong(Streams.asString(item.openStream())); } else if ("comment".equals(item.getFieldName())) { comment = Streams.asString(item.openStream()); } else if ("topicId".equals(item.getFieldName())) { topicId = Long.parseLong(Streams.asString(item.openStream())); } else if ("sync".equals(item.getFieldName())) { sync = Streams.asString(item.openStream()).equals("true"); } else if ("merge".equals(item.getFieldName())) { merge = Streams.asString(item.openStream()).equals("true"); } else if ("compression".equals(item.getFieldName())) { compression = Streams.asString(item.openStream()); } else if ("deserializerOid".equals(item.getFieldName())) { deserializerOid = Long.parseLong(Streams.asString(item.openStream())); } } else { name = item.getName(); in = item.openStream(); if ("file".equals(action)) { ServiceInterface serviceInterface = getBimServer().getServiceFactory() .get(token, AccessMethod.INTERNAL).get(ServiceInterface.class); SFile file = new SFile(); byte[] data = IOUtils.toByteArray(in); file.setData(data); file.setSize(data.length); file.setFilename(name); file.setMime(item.getContentType()); result.put("fileId", serviceInterface.uploadFile(file)); } else if (poid != -1) { InputStream realStream = null; if ("gzip".equals(compression)) { realStream = new GZIPInputStream(in); } else if ("deflate".equals(compression)) { realStream = new InflaterInputStream(in); } else { realStream = in; } InputStreamDataSource inputStreamDataSource = new InputStreamDataSource(realStream); inputStreamDataSource.setName(name); DataHandler ifcFile = new DataHandler(inputStreamDataSource); if (token != null) { if (topicId == -1) { ServiceInterface service = getBimServer().getServiceFactory() .get(token, AccessMethod.INTERNAL).get(ServiceInterface.class); long newTopicId = service.checkin(poid, comment, deserializerOid, -1L, name, ifcFile, merge, sync); result.put("topicId", newTopicId); } else { ServiceInterface service = getBimServer().getServiceFactory() .get(token, AccessMethod.INTERNAL).get(ServiceInterface.class); long newTopicId = service.checkinInitiated(topicId, poid, comment, deserializerOid, -1L, name, ifcFile, merge, true); result.put("topicId", newTopicId); } } } else { result.put("exception", "No poid"); } } } } } catch (Exception e) { LOGGER.error("", e); sendException(response, e); return; } response.getWriter().write(result.toString()); }
From source file:PNGDecoder.java
public byte[] getImageData() { try {/*from w w w . j av a 2s . c o m*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); // Write all the IDAT data into the array. for (int i = 0; i < mNumberOfChunks; i++) { PNGChunk chunk = mChunks[i]; if (chunk.getTypeString().equals("IDAT")) { out.write(chunk.getData()); } } out.flush(); // Now deflate the data. InflaterInputStream in = new InflaterInputStream(new ByteArrayInputStream(out.toByteArray())); ByteArrayOutputStream inflatedOut = new ByteArrayOutputStream(); int readLength; byte[] block = new byte[8192]; while ((readLength = in.read(block)) != -1) inflatedOut.write(block, 0, readLength); inflatedOut.flush(); byte[] imageData = inflatedOut.toByteArray(); // Compute the real length. int width = (int) getWidth(); int height = (int) getHeight(); int bitsPerPixel = getBitsPerPixel(); int length = width * height * bitsPerPixel / 8; byte[] prunedData = new byte[length]; // We can only deal with non-interlaced images. if (getInterlace() == 0) { int index = 0; for (int i = 0; i < length; i++) { if ((i * 8 / bitsPerPixel) % width == 0) { index++; // Skip the filter byte. } prunedData[i] = imageData[index++]; } } else System.out.println("Couldn't undo interlacing."); return prunedData; } catch (IOException ioe) { } return null; }
From source file:org.openhab.binding.nest.internal.messages.AbstractRequest.java
/** * Executes the given <code>url</code> with the given <code>httpMethod</code>. In the case of httpMethods that do * not support automatic redirection, manually handle the HTTP temporary redirect (307) and retry with the new URL. * /*from w w w.ja va 2 s. c o m*/ * @param httpMethod * the HTTP method to use * @param url * the url to execute (in milliseconds) * @param contentString * the content to be sent to the given <code>url</code> or <code>null</code> if no content should be * sent. * @param contentType * the content type of the given <code>contentString</code> * @return the response body or <code>NULL</code> when the request went wrong */ protected final String executeUrl(final String httpMethod, final String url, final String contentString, final String contentType) { HttpClient client = new HttpClient(); HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url); method.getParams().setSoTimeout(HTTP_REQUEST_TIMEOUT); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); for (String httpHeaderKey : HTTP_HEADERS.stringPropertyNames()) { method.addRequestHeader(new Header(httpHeaderKey, HTTP_HEADERS.getProperty(httpHeaderKey))); } // add content if a valid method is given ... if (method instanceof EntityEnclosingMethod && contentString != null) { EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method; InputStream content = new ByteArrayInputStream(contentString.getBytes()); eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType)); } if (logger.isDebugEnabled()) { try { logger.trace("About to execute '" + method.getURI().toString() + "'"); } catch (URIException e) { logger.trace(e.getMessage()); } } try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) { // perfectly fine but we cannot expect any answer... return null; } // Manually handle 307 redirects with a little tail recursion if (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) { Header[] headers = method.getResponseHeaders("Location"); String newUrl = headers[headers.length - 1].getValue(); return executeUrl(httpMethod, newUrl, contentString, contentType); } if (statusCode != HttpStatus.SC_OK) { logger.warn("Method failed: " + method.getStatusLine()); } InputStream tmpResponseStream = method.getResponseBodyAsStream(); Header encodingHeader = method.getResponseHeader("Content-Encoding"); if (encodingHeader != null) { for (HeaderElement ehElem : encodingHeader.getElements()) { if (ehElem.toString().matches(".*gzip.*")) { tmpResponseStream = new GZIPInputStream(tmpResponseStream); logger.trace("GZipped InputStream from {}", url); } else if (ehElem.toString().matches(".*deflate.*")) { tmpResponseStream = new InflaterInputStream(tmpResponseStream); logger.trace("Deflated InputStream from {}", url); } } } String responseBody = IOUtils.toString(tmpResponseStream); if (!responseBody.isEmpty()) { logger.trace(responseBody); } return responseBody; } catch (HttpException he) { logger.error("Fatal protocol violation: {}", he.toString()); } catch (IOException ioe) { logger.error("Fatal transport error: {}", ioe.toString()); } finally { method.releaseConnection(); } return null; }
From source file:org.wso2.carbon.identity.authenticator.saml2.sso.ui.Util.java
/** * Decoding and deflating the encoded AuthReq * * @param encodedStr encoded AuthReq//from w w w. j a v a 2 s . co m * @return decoded AuthReq */ public static String decode(String encodedStr) throws SAML2SSOUIAuthenticatorException { try { org.apache.commons.codec.binary.Base64 base64Decoder = new org.apache.commons.codec.binary.Base64(); byte[] xmlBytes = encodedStr.getBytes("UTF-8"); byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes); try { Inflater inflater = new Inflater(true); inflater.setInput(base64DecodedByteArray); byte[] xmlMessageBytes = new byte[5000]; int resultLength = inflater.inflate(xmlMessageBytes); if (inflater.getRemaining() > 0) { throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data"); } inflater.end(); return new String(xmlMessageBytes, 0, resultLength, "UTF-8"); } catch (DataFormatException e) { ByteArrayInputStream bais = new ByteArrayInputStream(base64DecodedByteArray); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InflaterInputStream iis = new InflaterInputStream(bais); byte[] buf = new byte[1024]; int count = iis.read(buf); while (count != -1) { baos.write(buf, 0, count); count = iis.read(buf); } iis.close(); String decodedStr = new String(baos.toByteArray()); return decodedStr; } } catch (IOException e) { throw new SAML2SSOUIAuthenticatorException("Error when decoding the SAML Request.", e); } }
From source file:net.oddsoftware.android.html.HttpCache.java
private void download(CacheItem cacheItem) { try {/*from w w w. ja v a 2 s .c o m*/ // check to see if file exist, if so check etag and last-modified if (cacheItem.mFilename.length() > 0) { File f = new File(cacheItem.mFilename); try { InputStream is = new FileInputStream(f); is.close(); } catch (IOException exc) { // no file, nuke the cache stats cacheItem.mETag = ""; cacheItem.mLastModified = 0; } } else { cacheItem.mFilename = mCacheDirectory + File.separator + UUID.randomUUID().toString() + ".html.gz"; } HttpContext httpContext = new BasicHttpContext(); HttpClient client = createHttpClient(); HttpUriRequest request = createHttpRequest(cacheItem.mUrl, cacheItem.mETag, cacheItem.mLastModified); if (request == null || request.getURI() == null || request.getURI().getHost() == null || request.getURI().getHost().length() == 0) { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "unable to create http request for url " + cacheItem.mUrl); return; // sadness } HttpResponse response = client.execute(request, httpContext); StatusLine status = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (status.getStatusCode() == 304) { if (Globals.LOGGING) Log.d(Globals.LOG_TAG, "received 304 not modified"); cacheItem.mHitTime = new Date().getTime(); cacheItem.update(mContentResolver); return; } if (status.getStatusCode() == 200) { InputStream inputStream = null; if (entity != null) { inputStream = entity.getContent(); } else { return; } long contentLength = entity.getContentLength(); if (contentLength > MAX_CONTENT_LENGTH) { if (Globals.LOGGING) Log.w(Globals.LOG_TAG, "HttpCache.download item " + cacheItem.mUrl + " content length is too big " + contentLength); return; } Header encodingHeader = entity.getContentEncoding(); boolean encoded = false; if (encodingHeader != null) { if (encodingHeader.getValue().equalsIgnoreCase("gzip")) { inputStream = new GZIPInputStream(inputStream); encoded = true; } else if (encodingHeader.getValue().equalsIgnoreCase("deflate")) { inputStream = new InflaterInputStream(inputStream); encoded = true; } } File tmpFile = File.createTempFile("httpcache", ".html.gz.tmp", mCacheDirectory); OutputStream os = new GZIPOutputStream(new FileOutputStream(tmpFile)); byte[] buffer = new byte[4096]; int count = 0; long fileSize = 0; while ((count = inputStream.read(buffer)) != -1) { os.write(buffer, 0, count); fileSize += count; } inputStream.close(); os.close(); if (!encoded && contentLength > 0 && fileSize != contentLength) { Log.e(Globals.LOG_TAG, "HttpCache.download: content-length: " + contentLength + " but file size: " + fileSize + " aborting"); tmpFile.delete(); return; } tmpFile.renameTo(new File(cacheItem.mFilename)); // if the parse was ok, update these attributes // ETag: "6050003-78e5-4981d775e87c0" Header etagHeader = response.getFirstHeader("ETag"); if (etagHeader != null) { if (etagHeader.getValue().length() < MAX_ETAG_LENGTH) { cacheItem.mETag = etagHeader.getValue(); } else { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "etag length was too big: " + etagHeader.getValue().length()); } } // Last-Modified: Fri, 24 Dec 2010 00:57:11 GMT Header lastModifiedHeader = response.getFirstHeader("Last-Modified"); if (lastModifiedHeader != null) { try { cacheItem.mLastModified = FeedManager.parseRFC822Date(lastModifiedHeader.getValue()) .getTime(); } catch (ParseException exc) { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "unable to parse date", exc); } } // Expires: Thu, 01 Dec 1994 16:00:00 GMT Header expiresHeader = response.getFirstHeader("Expires"); if (expiresHeader != null) { try { cacheItem.mExpiresAt = FeedManager.parseRFC822Date(expiresHeader.getValue()).getTime(); } catch (ParseException exc) { if (Globals.LOGGING) Log.e(Globals.LOG_TAG, "unable to parse expires", exc); } } long now = new Date().getTime() + DEFAULT_EXPIRES; if (cacheItem.mExpiresAt < now) { cacheItem.mExpiresAt = now; } HttpUriRequest currentReq = (HttpUriRequest) httpContext .getAttribute(ExecutionContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST); String currentUrl = currentHost.toURI() + currentReq.getURI(); if (Globals.LOGGING) Log.w(Globals.LOG_TAG, "loaded redirect from " + request.getURI().toString() + " to " + currentUrl); cacheItem.mLastUrl = currentUrl; cacheItem.mHitTime = new Date().getTime(); cacheItem.update(mContentResolver); } } catch (IOException exc) { if (Globals.LOGGING) { Log.e(Globals.LOG_TAG, "error downloading file to cache", exc); } } }