Search in sources :

Example 11 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project RoboZombie by sahan.

the class EntityUtils method toString.

/**
     * 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();
    }
}
Also used : ContentType(org.apache.http42.entity.ContentType) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 12 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project nokogiri by sparklemotion.

the class ParserContext method setInputSource.

/**
     * Set the InputSource from <code>url</code> or <code>data</code>,
     * which may be an IO object, a String, or a StringIO.
     */
public void setInputSource(ThreadContext context, IRubyObject data, IRubyObject url) {
    source = new InputSource();
    Ruby ruby = context.getRuntime();
    ParserContext.setUrl(context, source, url);
    // to the EncodingReaderInputStream
    if (setEncoding(context, data))
        return;
    RubyString stringData = null;
    if (invoke(context, data, "respond_to?", ruby.newSymbol("to_io")).isTrue()) {
        RubyIO io = (RubyIO) TypeConverter.convertToType(data, ruby.getIO(), "to_io");
        // use unclosedable input stream to fix #495
        source.setByteStream(new UncloseableInputStream(io.getInStream()));
    } else if (invoke(context, data, "respond_to?", ruby.newSymbol("read")).isTrue()) {
        stringData = invoke(context, data, "read").convertToString();
    } else if (invoke(context, data, "respond_to?", ruby.newSymbol("string")).isTrue()) {
        stringData = invoke(context, data, "string").convertToString();
    } else if (data instanceof RubyString) {
        stringData = (RubyString) data;
    } else {
        throw ruby.newArgumentError("must be kind_of String or respond to :to_io, :read, or :string");
    }
    if (stringData != null) {
        String encName = null;
        if (stringData.encoding(context) != null) {
            encName = stringData.encoding(context).toString();
        }
        Charset charset = null;
        if (encName != null) {
            try {
                charset = Charset.forName(encName);
            } catch (UnsupportedCharsetException e) {
            // do nothing;
            }
        }
        ByteList bytes = stringData.getByteList();
        if (charset != null) {
            StringReader reader = new StringReader(new String(bytes.unsafeBytes(), bytes.begin(), bytes.length(), charset));
            source.setCharacterStream(reader);
            source.setEncoding(charset.name());
        } else {
            stringDataSize = bytes.length() - bytes.begin();
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
            source.setByteStream(stream);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) ByteList(org.jruby.util.ByteList) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) RubyString(org.jruby.RubyString) StringReader(java.io.StringReader) Charset(java.nio.charset.Charset) RubyString(org.jruby.RubyString) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyIO(org.jruby.RubyIO) Ruby(org.jruby.Ruby)

Example 13 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project gocd by gocd.

the class ParserContext method setInputSource.

/**
     * Set the InputSource from <code>url</code> or <code>data</code>,
     * which may be an IO object, a String, or a StringIO.
     */
public void setInputSource(ThreadContext context, IRubyObject data, IRubyObject url) {
    source = new InputSource();
    Ruby ruby = context.getRuntime();
    ParserContext.setUrl(context, source, url);
    // to the EncodingReaderInputStream
    if (setEncoding(context, data))
        return;
    RubyString stringData = null;
    if (invoke(context, data, "respond_to?", ruby.newSymbol("to_io").to_sym()).isTrue()) {
        /* IO or other object that responds to :to_io */
        RubyIO io = (RubyIO) TypeConverter.convertToType(data, ruby.getIO(), "to_io");
        // use unclosedable input stream to fix #495
        source.setByteStream(new UncloseableInputStream(io.getInStream()));
    } else {
        if (invoke(context, data, "respond_to?", ruby.newSymbol("string").to_sym()).isTrue()) {
            /* StringIO or other object that responds to :string */
            stringData = invoke(context, data, "string").convertToString();
        } else if (data instanceof RubyString) {
            stringData = (RubyString) data;
        } else {
            throw ruby.newArgumentError("must be kind_of String or respond to :to_io or :string");
        }
    }
    if (stringData != null) {
        String encName = null;
        if (stringData.encoding(context) != null) {
            encName = stringData.encoding(context).toString();
        }
        Charset charset = null;
        if (encName != null) {
            try {
                charset = Charset.forName(encName);
            } catch (UnsupportedCharsetException e) {
            // do nothing;
            }
        }
        ByteList bytes = stringData.getByteList();
        if (charset != null) {
            StringReader reader = new StringReader(new String(bytes.unsafeBytes(), bytes.begin(), bytes.length(), charset));
            source.setCharacterStream(reader);
            source.setEncoding(charset.name());
        } else {
            stringDataSize = bytes.length() - bytes.begin();
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
            source.setByteStream(stream);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) ByteList(org.jruby.util.ByteList) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) RubyString(org.jruby.RubyString) StringReader(java.io.StringReader) Charset(java.nio.charset.Charset) RubyString(org.jruby.RubyString) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyIO(org.jruby.RubyIO) Ruby(org.jruby.Ruby)

Example 14 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.

the class IOSCharsetEncoder method encodeImpl.

private byte[] encodeImpl(CharBuffer in) {
    Charset cs = charset();
    if (!(cs instanceof IOSCharset)) {
        throw new UnsupportedCharsetException(cs.name());
    }
    char[] chars;
    int i;
    if (inBuffer != null) {
        i = inBuffer.length;
        chars = new char[i + in.remaining()];
        System.arraycopy(inBuffer, 0, chars, 0, inBuffer.length);
        inBuffer = null;
    } else {
        if (nsEncoding == /* NSUnicodeStringEncoding */
        10L) {
            // Prepend required BOM for Java's big-endian encoding default.
            chars = new char[in.remaining() + 1];
            chars[0] = (char) 0xFEFF;
            i = 1;
        } else {
            i = 0;
            chars = new char[in.remaining()];
        }
    }
    in.get(chars, i, chars.length - i);
    byte[] bytes = encode(chars, nsEncoding);
    if (bytes.length == 0) {
        inBuffer = chars;
    } else {
        inBuffer = null;
    }
    return bytes;
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset)

Example 15 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project j2objc by google.

the class ChannelsTest method testnewWriterCharsetError.

public void testnewWriterCharsetError() throws Exception {
    this.fouts = new FileOutputStream(tmpFile);
    WritableByteChannel wbChannel = Channels.newChannel(this.fouts);
    try {
        Channels.newWriter(wbChannel, Charset.forName(BAD_CODE_SET).newEncoder(), -1);
        fail();
    } catch (UnsupportedCharsetException e) {
    // correct
    }
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel)

Aggregations

UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)46 Charset (java.nio.charset.Charset)18 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)13 IOException (java.io.IOException)10 File (java.io.File)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 FileOutputStream (java.io.FileOutputStream)4 PrintStream (java.io.PrintStream)4 HashMap (java.util.HashMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 FormBuilder (com.jgoodies.forms.builder.FormBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 WritableByteChannel (java.nio.channels.WritableByteChannel)2