/** * Returns the encoding used to transmit the response body over the network. * This is null or "identity" if the content was not encoded, or "gzip" if * the body was gzip compressed. Most callers will be more interested in the * {@link #getContentType() content type}, which may also include the * content's character encoding. */ @Override public String getContentEncoding() { return super.getContentEncoding(); // overridden for Javadoc only }
URL url = new URL("http://www.example.com/"); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); System.out.println(body);
URLConnection connection = url.openConnection(); InputStream input = connection.getInputStream(); if ("gzip".equals(connection.getContentEncoding())) { input = new GZIPInputStream(input); } // ...
URLConnection connection = url.openConnection(); InputStream stream = connection.getInputStream(); if ("gzip".equals(connection.getContentEncoding())) { stream = new GZIPInputStream(stream)); } InputSource is = new InputSource(stream);
String contentEncoding = con.getContentEncoding() != null ? con.getContentEncoding() : "UTF-8"; return IOUtils.toString(is, contentEncoding); //Apache Commons IO } catch (Exception e) {
URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail HttpURLConnection.setFollowRedirects(true); // allow both GZip and Deflate (ZLib) encodings conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); String encoding = conn.getContentEncoding(); InputStream inStr = null; // create the appropriate stream wrapper based on // the encoding type if (encoding != null && encoding.equalsIgnoreCase("gzip")) { inStr = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); } else { inStr = conn.getInputStream(); }
/** * TODO(jwagenleitner): remove or fix in future release * * According to the spec getContentEncoding() returns the Content-Encoding * HTTP Header which typically carries values such as 'gzip' or 'deflate' * and is not the character set encoding. For compatibility in 2.4.x, * this behavior is retained but should be removed or fixed (parse * charset from Content-Type header) in future releases. * * see GROOVY-8056 and https://github.com/apache/groovy/pull/500 */ private static String getContentEncoding(URL url) throws IOException { URLConnection urlConnection = url.openConnection(); String encoding = urlConnection.getContentEncoding(); try { IOGroovyMethods.closeQuietly(urlConnection.getInputStream()); } catch (IOException ignore) { // For compatibility, ignore exceptions from getInputStream() call } return encoding; }
String encoding = conn.getContentEncoding();
InputStream input = inputStream; try { if ("gzip".equals(connection.getContentEncoding())) {
private int pump(OutputStream output, URLConnection connection) throws IOException { final int dataLength; final CounterInputStream counterInputStream = new CounterInputStream( connection.getInputStream()); InputStream input = counterInputStream; try { if ("gzip".equals(connection.getContentEncoding())) { input = new GZIPInputStream(input); } InputOutput.pump(input, output); } finally { try { input.close(); } finally { close(connection); dataLength = counterInputStream.getDataLength(); } } return dataLength; }
/** * Get the class of the scriptName in question, so that you can instantiate * Groovy objects with caching and reloading. * * @param scriptName resource name pointing to the script * @return the loaded scriptName as a compiled class * @throws ResourceException if there is a problem accessing the script * @throws ScriptException if there is a problem parsing the script */ public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException { URLConnection conn = rc.getResourceConnection(scriptName); String path = conn.getURL().toExternalForm(); ScriptCacheEntry entry = scriptCache.get(path); Class clazz = null; if (entry != null) clazz = entry.scriptClass; try { if (isSourceNewer(entry)) { try { String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : config.getSourceEncoding(); String content = IOGroovyMethods.getText(conn.getInputStream(), encoding); clazz = groovyLoader.parseClass(content, path); } catch (IOException e) { throw new ResourceException(e); } } } finally { forceClose(conn); } return clazz; }
&& GZIP_CONTENT_ENCODING.equals(connection.getContentEncoding())) { is = new GZIPInputStream(is);
@Override public String getEncoding() { return con.getContentEncoding(); }
public SimpleHTTPResponse(final URLConnection connection) throws IOException { this.connection = connection; InputStream inputStream = connection.getInputStream(); final String contentEncoding = connection.getContentEncoding(); if (contentEncoding != null && connection.getContentEncoding().indexOf("gzip") != -1) { inputStream = new GZIPInputStream(inputStream); } responseStream = inputStream; }
private static InputStream getInputStream(URLConnection urlConnection) throws IOException { if ("gzip".equals(urlConnection.getContentEncoding())) { return new GZIPInputStream(urlConnection.getInputStream()); } return urlConnection.getInputStream(); }
URL url = new URL("http://www.example.com/"); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); // ** WRONG: should use "con.getContentType()" instead but it returns something like "text/html; charset=UTF-8" so this value must be parsed to extract the actual encoding encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); System.out.println(body);
final String encoding = conn.getContentEncoding(); if (encoding != null) { if ("gzip".equalsIgnoreCase(encoding)) {
String encoding = connection.getContentEncoding(); if (encoding != null) { metadata.set(Metadata.CONTENT_ENCODING, encoding);
/** * Returns the encoding used to transmit the response body over the network. * This is null or "identity" if the content was not encoded, or "gzip" if * the body was gzip compressed. Most callers will be more interested in the * {@link #getContentType() content type}, which may also include the * content's character encoding. */ @Override public String getContentEncoding() { return super.getContentEncoding(); // overridden for Javadoc only }
private JSONObject getJSONObject(URLConnection conn) throws IOException, JSONException { try(InputStream in = conn.getInputStream()) { String encoding = conn.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; String body = IOUtils.toString(in, encoding); JSONObject obj = new JSONObject(body); JSONObject clusterInfo = obj.getJSONObject("clusterInfo"); return clusterInfo; } }