use of org.jruby.RubyIO 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.RubyIO 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.RubyIO in project jruby-openssl by jruby.
the class SSLSocket method initialize.
@JRubyMethod(name = "initialize", rest = true, frame = true, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final Ruby runtime = context.runtime;
if (Arity.checkArgumentCount(runtime, args, 1, 2) == 1) {
sslContext = new SSLContext(runtime).initializeImpl();
} else {
sslContext = (SSLContext) args[1];
}
if (!(args[0] instanceof RubyIO)) {
throw runtime.newTypeError("IO expected but got " + args[0].getMetaClass().getName());
}
// compat (we do not read @io)
setInstanceVariable("@io", this.io = (RubyIO) args[0]);
// Ruby 2.3 : @io.nonblock = true if @io.respond_to?(:nonblock=)
if (io.respondsTo("nonblock=")) {
io.callMethod(context, "nonblock=", runtime.getTrue());
}
// only compat (we do not use @context)
setInstanceVariable("@context", this.sslContext);
// This is a bit of a hack: SSLSocket should share code with
// RubyBasicSocket, which always sets sync to true.
// Instead we set it here for now.
// io.sync = true
this.set_sync(context, runtime.getTrue());
this.callMethod(context, "sync_close=", runtime.getFalse());
sslContext.setup(context);
// super()
return Utils.invokeSuper(context, this, args, Block.NULL_BLOCK);
}
Aggregations