use of org.jruby.util.ByteList 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 org.jruby.util.ByteList in project nokogiri by sparklemotion.
the class NokogiriHelpers method rubyStringToString.
public static String rubyStringToString(RubyString str) {
ByteList byteList = str.getByteList();
byte[] data = byteList.unsafeBytes();
int offset = byteList.begin();
int len = byteList.length();
ByteBuffer buf = ByteBuffer.wrap(data, offset, len);
return UTF8.decode(buf).toString();
}
use of org.jruby.util.ByteList in project nokogiri by sparklemotion.
the class NokogiriEncodingReaderWrapper method read.
@Override
public int read(byte[] b, int off, int len) {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int copyLength = Math.min(firstChunkLength - firstChunkOff, len);
if (copyLength > 0) {
System.arraycopy(firstChunk, firstChunkOff, b, off, copyLength);
len -= copyLength;
firstChunkOff += copyLength;
}
if (len <= 0)
return copyLength;
IRubyObject returnValue = encodingReader.callMethod(context, "read", ruby.newFixnum(len));
if (returnValue.isNil())
return -1;
ByteList bytes = returnValue.asString().getByteList();
int length = bytes.length();
System.arraycopy(bytes.unsafeBytes(), bytes.getBegin(), b, off + copyLength, length);
return length + copyLength;
}
use of org.jruby.util.ByteList in project gocd by gocd.
the class NokogiriHelpers method rubyStringToString.
public static String rubyStringToString(RubyString str) {
ByteList byteList = str.getByteList();
byte[] data = byteList.unsafeBytes();
int offset = byteList.begin();
int len = byteList.length();
ByteBuffer buf = ByteBuffer.wrap(data, offset, len);
return getCharsetUTF8().decode(buf).toString();
}
use of org.jruby.util.ByteList 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);
}
}
}
Aggregations