Search in sources :

Example 1 with ByteList

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

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();
}
Also used : ByteList(org.jruby.util.ByteList) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ByteList

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

Example 4 with ByteList

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();
}
Also used : ByteList(org.jruby.util.ByteList) ByteBuffer(java.nio.ByteBuffer)

Example 5 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)

Aggregations

ByteList (org.jruby.util.ByteList)43 Ruby (org.jruby.Ruby)21 JRubyMethod (org.jruby.anno.JRubyMethod)19 RubyString (org.jruby.RubyString)18 IRubyObject (org.jruby.runtime.builtin.IRubyObject)10 GeneralSecurityException (java.security.GeneralSecurityException)8 IOException (java.io.IOException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 ByteBuffer (java.nio.ByteBuffer)5 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BigInteger (java.math.BigInteger)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)3 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)3 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)3 ASN1TaggedObject (org.bouncycastle.asn1.ASN1TaggedObject)3 InputSource (org.xml.sax.InputSource)3 StringReader (java.io.StringReader)2