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();
}
}
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);
}
}
}
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);
}
}
}
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;
}
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
}
}
Aggregations