Package org.jruby.util

Examples of org.jruby.util.ByteList


     * <p>Invoke a block for each line.</p>
     */
    @JRubyMethod(name = {"each_line", "each"}, optional = 1, frame = true)
    public RubyIO each_line(ThreadContext context, IRubyObject[] args, Block block) {
        Ruby runtime = context.getRuntime();
        ByteList separator = getSeparatorForGets(runtime, args);
       
        for (IRubyObject line = getline(runtime, separator); !line.isNil();
          line = getline(runtime, separator)) {
            block.yield(context, line);
        }
View Full Code Here


    @JRubyMethod(name = "readlines", optional = 1)
    public RubyArray readlines(ThreadContext context, IRubyObject[] args) {
        Ruby runtime = context.getRuntime();
        IRubyObject[] separatorArgs = args.length > 0 ? new IRubyObject[] { args[0] } : IRubyObject.NULL_ARRAY;
        ByteList separator = getSeparatorForGets(runtime, separatorArgs);
        RubyArray result = runtime.newArray();
        IRubyObject line;
       
        while (! (line = getline(runtime, separator)).isNil()) {
            result.append(line);
View Full Code Here

        clinitMethod.anewarray(p(ByteList.class));
        clinitMethod.putstatic(scriptCompiler.getClassname(), "byteLists", ci(ByteList[].class));

        for (Map.Entry<String, Integer> entry : byteListIndices.entrySet()) {
            int index = entry.getValue();
            ByteList byteList = byteListValues.get(entry.getKey());

            clinitMethod.getstatic(scriptCompiler.getClassname(), "byteLists", ci(ByteList[].class));
            clinitMethod.ldc(index);
            clinitMethod.ldc(byteList.toString());
            clinitMethod.invokestatic(p(ByteList.class), "create", sig(ByteList.class, CharSequence.class));
            clinitMethod.arraystore();
        }
    }
View Full Code Here

    public static IRubyObject guess(ThreadContext context, IRubyObject recv, IRubyObject s) {
        Ruby runtime = context.getRuntime();
        if (!s.respondsTo("to_str")) {
            throw runtime.newTypeError("can't convert " + s.getMetaClass() + " into String");
        }
        ByteList bytes = s.convertToString().getByteList();
        ByteBuffer buf = ByteBuffer.wrap(bytes.unsafeBytes(), bytes.begin(), bytes.length());
        CharsetDecoder decoder = Charset.forName("x-JISAutoDetect").newDecoder();
        try {
            decoder.decode(buf);
        } catch (CharacterCodingException e) {
            return runtime.newFixnum(UNKNOWN.getValue());
View Full Code Here

            encoder = Charset.forName(encodeCharset).newEncoder();
        } catch (UnsupportedCharsetException e) {
            throw runtime.newArgumentError("invalid encoding");
        }
       
        ByteList bytes = str.convertToString().getByteList();
        ByteBuffer buf = ByteBuffer.wrap(bytes.unsafeBytes(), bytes.begin(), bytes.length());
        try {
            CharBuffer cbuf = decoder.decode(buf);
            buf = encoder.encode(cbuf);
        } catch (CharacterCodingException e) {
            throw runtime.newArgumentError("invalid encoding");
        }
        byte[] arr = buf.array();
       
        return runtime.newString(new ByteList(arr, 0, buf.limit()));
       
    }
View Full Code Here

            buffer[i++] = (byte)b;
        }
        if (i < length) {
            throw getRuntime().newArgumentError("marshal data too short");
        }
        return new ByteList(buffer,false);
    }
View Full Code Here

      return result;
    }

    private IRubyObject userUnmarshal() throws IOException {
        String className = unmarshalObject().asJavaString();
        ByteList marshaled = unmarshalString();
        RubyModule classInstance = findClass(className);
        if (!classInstance.respondsTo("_load")) {
            throw runtime.newTypeError("class " + classInstance.getName() + " needs to have method `_load'");
        }
        RubyString data = RubyString.newString(getRuntime(), marshaled);
View Full Code Here

        Ruby runtime = context.getRuntime();
        int count = args.length;
        IRubyObject filename = args[0].convertToString();
        runtime.checkSafeString(filename);
      
        ByteList separator = getSeparatorFromArgs(runtime, args, 1);

        RubyIO io = (RubyIO)RubyFile.open(context, runtime.getFile(), new IRubyObject[] { filename }, Block.NULL_BLOCK);
       
        if (!io.isNil()) {
            try {
View Full Code Here

    @JRubyMethod(name = "crc32", optional = 2, module = true, visibility = Visibility.PRIVATE)
    public static IRubyObject crc32(IRubyObject recv, IRubyObject[] args) throws Exception {
        args = Arity.scanArgs(recv.getRuntime(),args,0,2);
        long crc = 0;
        ByteList bytes = null;
       
        if (!args[0].isNil()) bytes = args[0].convertToString().getByteList();
        if (!args[1].isNil()) crc = RubyNumeric.num2long(args[1]);

        CRC32Ext ext = new CRC32Ext((int)crc);
        if (bytes != null) {
            ext.update(bytes.unsafeBytes(), bytes.begin(), bytes.length());
        }
       
        return recv.getRuntime().newFixnum(ext.getValue());
    }
View Full Code Here

    @JRubyMethod(name = "adler32", optional = 2, module = true, visibility = Visibility.PRIVATE)
    public static IRubyObject adler32(IRubyObject recv, IRubyObject[] args) throws Exception {
        args = Arity.scanArgs(recv.getRuntime(),args,0,2);
        int adler = 1;
        ByteList bytes = null;
        if (!args[0].isNil()) bytes = args[0].convertToString().getByteList();
        if (!args[1].isNil()) adler = RubyNumeric.fix2int(args[1]);

        Adler32Ext ext = new Adler32Ext(adler);
        if (bytes != null) {
            ext.update(bytes.unsafeBytes(), bytes.begin(), bytes.length()); // it's safe since adler.update doesn't modify the array
        }
        return recv.getRuntime().newFixnum(ext.getValue());
    }
View Full Code Here

TOP

Related Classes of org.jruby.util.ByteList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.