Search in sources :

Example 6 with ByteList

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);
        }
    }
}
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 7 with ByteList

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;
}
Also used : ByteList(org.jruby.util.ByteList) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 8 with ByteList

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;
}
Also used : ByteList(org.jruby.util.ByteList) SaveContextVisitor(nokogiri.internals.SaveContextVisitor) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ByteBuffer(java.nio.ByteBuffer) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

ByteList (org.jruby.util.ByteList)8 RubyString (org.jruby.RubyString)4 IRubyObject (org.jruby.runtime.builtin.IRubyObject)4 ByteBuffer (java.nio.ByteBuffer)3 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)3 Ruby (org.jruby.Ruby)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringReader (java.io.StringReader)2 Charset (java.nio.charset.Charset)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 RubyIO (org.jruby.RubyIO)2 InputSource (org.xml.sax.InputSource)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)1 SaveContextVisitor (nokogiri.internals.SaveContextVisitor)1 RubyClass (org.jruby.RubyClass)1 JRubyMethod (org.jruby.anno.JRubyMethod)1 ThreadContext (org.jruby.runtime.ThreadContext)1