Java Code Examples for java.nio.charset.UnsupportedCharsetException#getMessage()

The following examples show how to use java.nio.charset.UnsupportedCharsetException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityUtils.java    From java-android-websocket-client with Apache License 2.0 6 votes vote down vote up
/**
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity,
 * or if the entity provided charset is invalid or not available.
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in
 * this instance of the Java virtual machine and no defaultCharset is provided.
 */
public static String toString(
        final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
    Args.notNull(entity, "Entity");
    ContentType contentType = null;
    try {
        contentType = ContentType.get(entity);
    } catch (final UnsupportedCharsetException ex) {
        if (defaultCharset == null) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
    }
    if (contentType != null) {
        if (contentType.getCharset() == null) {
            contentType = contentType.withCharset(defaultCharset);
        }
    } else {
        contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
    }
    return toString(entity, contentType);
}
 
Example 2
Source File: HttpResponseConsumer.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
protected void consumeResponseContent(Map<String, String> result) throws IOException {
    if (StringUtils.isEmpty(destinationFile)) {
        String document;
        try {
            document = IOUtils.toString(httpResponse.getEntity().getContent(), responseCharacterSet);
        } catch (UnsupportedCharsetException e) {
            throw new IllegalArgumentException("Could not parse responseCharacterSet. " + e.getMessage(), e);
        }
        result.put(HttpClientService.RETURN_RESULT, document);
    } else {
        consumeToDestinationFile();
    }
}
 
Example 3
Source File: EntityUtils.java    From RoboZombie with Apache License 2.0 4 votes vote down vote up
/**
 * Get the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be deserialized
 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 */
public static String toString(
        final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    InputStream instream = entity.getContent();
    if (instream == null) {
        return null;
    }
    try {
        if (entity.getContentLength() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
        }
        int i = (int)entity.getContentLength();
        if (i < 0) {
            i = 4096;
        }
        Charset charset = null;
        try {
            ContentType contentType = ContentType.getOrDefault(entity);
            charset = contentType.getCharset();
        } catch (UnsupportedCharsetException ex) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
        if (charset == null) {
            charset = defaultCharset;
        }
        if (charset == null) {
            charset = HTTP.DEF_CONTENT_CHARSET;
        }
        Reader reader = new InputStreamReader(instream, charset);
        CharArrayBuffer buffer = new CharArrayBuffer(i);
        char[] tmp = new char[1024];
        int l;
        while((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}