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);
}
}
}
use of org.jruby.util.ByteList in project gocd by gocd.
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 nokogiri by sparklemotion.
the class XmlNode method native_write_to.
/**
* @param args {IRubyObject io,
* IRubyObject encoding,
* IRubyObject indentString,
* IRubyObject options}
*/
@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject native_write_to(ThreadContext context, IRubyObject[] args) {
IRubyObject io = args[0];
IRubyObject encoding = args[1];
IRubyObject indentString = args[2];
IRubyObject options = args[3];
String encString = encoding.isNil() ? null : rubyStringToString(encoding);
SaveContextVisitor visitor = new SaveContextVisitor(RubyFixnum.fix2int(options), rubyStringToString(indentString), encString, isHtmlDoc(context), isFragment(), 0);
accept(context, visitor);
final IRubyObject rubyString;
if (NokogiriHelpers.isUTF8(encString)) {
rubyString = convertString(context.getRuntime(), visitor.getInternalBuffer());
} else {
ByteBuffer bytes = convertEncoding(Charset.forName(encString), visitor.getInternalBuffer());
ByteList str = new ByteList(bytes.array(), bytes.arrayOffset(), bytes.remaining());
rubyString = RubyString.newString(context.getRuntime(), str);
}
RuntimeHelpers.invoke(context, io, "write", rubyString);
return io;
}
Aggregations