Java Code Examples for java.net.URLConnection.getContentEncoding()

The following are Jave code examples for showing how to use getContentEncoding() of the java.net.URLConnection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: alfresco-remote-api   File: HTTPProxy.java   Source Code and License Vote up 6 votes
/**
 * Initialise response
 * 
 * @param urlConnection  url connection
 */
protected void initialiseResponse(URLConnection urlConnection)
{
    String type = urlConnection.getContentType();
    if (type != null)
    {
        int encodingIdx = type.lastIndexOf("charset=");
        if (encodingIdx == -1)
        {
            String encoding = urlConnection.getContentEncoding();
            if (encoding != null && encoding.length() > 0)
            {
                type += ";charset=" + encoding;
            }
        }
        
        response.setContentType(type);
    }
}
 
Example 2
Project: nat-network-transfer   File: TransferModule.java   Source Code and License Vote up 5 votes
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException {
    String encoding = conn.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream()));
    }
    return new SimpleTrackingInputStream(conn.getInputStream());
}
 
Example 3
Project: neoscada   File: UrlConnectionInput.java   Source Code and License Vote up 5 votes
@Override
protected Data makeData () throws Exception
{
    final URLConnection connection = this.url.openConnection ();
    connection.connect ();

    final int len = connection.getContentLength ();

    final ByteArrayOutputStream bos = new ByteArrayOutputStream ( len > 0 ? len : 0 );

    final byte[] buffer = new byte[4096];
    try ( InputStream stream = connection.getInputStream () )
    {
        int rc;
        while ( ( rc = stream.read ( buffer ) ) > 0 )
        {
            bos.write ( buffer, 0, rc );
        }
        bos.close ();
    }

    final String encoding = connection.getContentEncoding ();
    final String type = connection.getContentType ();

    logger.debug ( "Content-Encoding: {}", encoding );
    logger.debug ( "Content-Type: {}", type );

    Charset charset = null;

    if ( this.charset != null )
    {
        charset = this.charset;
    }
    else if ( this.probeCharset )
    {
        charset = makeCharsetFromType ( type );
    }

    return new UrlConnectionData ( convert ( buffer, charset ), null );
}
 
Example 4
Project: siiMobilityAppKit   File: FileTransfer.java   Source Code and License Vote up 5 votes
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException {
    String encoding = conn.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
      return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream()));
    }
    return new SimpleTrackingInputStream(conn.getInputStream());
}
 
Example 5
Project: siiMobilityAppKit   File: FileTransfer.java   Source Code and License Vote up 5 votes
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException {
    String encoding = conn.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
      return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream()));
    }
    return new SimpleTrackingInputStream(conn.getInputStream());
}
 
Example 6
Project: siiMobilityAppKit   File: FileTransfer.java   Source Code and License Vote up 5 votes
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException {
    String encoding = conn.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
      return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream()));
    }
    return new SimpleTrackingInputStream(conn.getInputStream());
}
 
Example 7
Project: Helpers   File: TextReader.java   Source Code and License Vote up 4 votes
public static BufferedReader getWebsiteReader(String link) throws IOException {
    URLConnection conn = new URL(link).openConnection();
    conn.setRequestProperty("User-Agent", "Mozilla");
    String encoding = conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding();
    return new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
}
 
Example 8
Project: letv   File: WebViewAppLinkResolver.java   Source Code and License Vote up 4 votes
private static String readFromConnection(URLConnection connection) throws IOException {
    InputStream stream;
    if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        try {
            stream = connection.getInputStream();
        } catch (Exception e) {
            stream = httpConnection.getErrorStream();
        }
    } else {
        stream = connection.getInputStream();
    }
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        while (true) {
            int read = stream.read(buffer);
            if (read == -1) {
                break;
            }
            output.write(buffer, 0, read);
        }
        String charset = connection.getContentEncoding();
        if (charset == null) {
            for (String part : connection.getContentType().split(";")) {
                String part2 = part2.trim();
                if (part2.startsWith("charset=")) {
                    charset = part2.substring("charset=".length());
                    break;
                }
            }
            if (charset == null) {
                charset = "UTF-8";
            }
        }
        String str = new String(output.toByteArray(), charset);
        return str;
    } finally {
        stream.close();
    }
}