Package org.jruby.runtime.encoding

Examples of org.jruby.runtime.encoding.EncodingService


    @SuppressWarnings("unchecked")
    @JRubyMethod(name = "names")
    public IRubyObject names(ThreadContext context) {
        Ruby runtime = context.runtime;
        EncodingService service = runtime.getEncodingService();
        Entry entry = service.findEncodingOrAliasEntry(name);

        RubyArray result = runtime.newArray();
        HashEntryIterator i;
        i = service.getEncodings().entryIterator();
        while (i.hasNext()) {
            CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
                ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)i.next());
            if (e.value == entry) {
                result.append(RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
            }
        }
        i = service.getAliases().entryIterator();       
        while (i.hasNext()) {
            CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
                ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>)i.next());
            if (e.value == entry) {
                result.append(RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context));
View Full Code Here


    }

    @JRubyMethod(name = "default_external=", meta = true, compat = RUBY1_9)
    public static IRubyObject setDefaultExternal(IRubyObject recv, IRubyObject encoding) {
        Ruby runtime = recv.getRuntime();
        EncodingService service = runtime.getEncodingService();
        if (encoding.isNil()) {
            throw recv.getRuntime().newArgumentError("default_external can not be nil");
        }
        runtime.setDefaultExternalEncoding(service.getEncodingFromObject(encoding));
        return encoding;
    }
View Full Code Here

    }

    @JRubyMethod(name = "default_internal=", required = 1, meta = true, compat = RUBY1_9)
    public static IRubyObject setDefaultInternal(IRubyObject recv, IRubyObject encoding) {
        Ruby runtime = recv.getRuntime();
        EncodingService service = runtime.getEncodingService();
        if (encoding.isNil()) {
            recv.getRuntime().newArgumentError("default_internal can not be nil");
        }
        recv.getRuntime().setDefaultInternalEncoding(service.getEncodingFromObject(encoding));
        return encoding;
    }
View Full Code Here

        }
    }

    @JRubyMethod(compat = RUBY1_9)
    public IRubyObject external_encoding(ThreadContext context) {
        EncodingService encodingService = context.runtime.getEncodingService();
       
        if (writeEncoding != null) return encodingService.getEncoding(writeEncoding);
       
        if (openFile.isWritable()) {
            return readEncoding == null ? context.runtime.getNil() : encodingService.getEncoding(readEncoding);
        }
       
        return encodingService.getEncoding(getReadEncoding(context.runtime));
    }
View Full Code Here

        RubyComparable.createComparable(this);
        RubyEnumerable.createEnumerableModule(this);
        RubyString.createStringClass(this);

        encodingService = new EncodingService(this);

        RubySymbol.createSymbolClass(this);
       
        recursiveKey = newSymbol("__recursive_key__");
View Full Code Here

   
    // FIXME: MRI skips this logic on windows?  Does not make sense to me why so I left it in.
    // mri: file_path_convert
    private static RubyString filePathConvert(ThreadContext context, RubyString path) {
        Ruby runtime = context.getRuntime();
        EncodingService encodingService = runtime.getEncodingService();
        Encoding pathEncoding = path.getEncoding();

        // If we are not ascii and do not match fs encoding then transcode to fs.
        if (runtime.getDefaultInternalEncoding() != null &&
                pathEncoding != encodingService.getUSAsciiEncoding() &&
                pathEncoding != encodingService.getAscii8bitEncoding() &&
                pathEncoding != encodingService.getFileSystemEncoding(runtime) &&
                !path.isAsciiOnly()) {
            ByteList bytes = CharsetTranscoder.transcode(context, path.getByteList(), pathEncoding, encodingService.getFileSystemEncoding(runtime), null);
            path = RubyString.newString(runtime, bytes);
        }               

        return path;
    }
View Full Code Here

        }
    }

    // c: parse_mode_enc
    public static EncodingOption getEncodingOptionFromString(Ruby runtime, String option) {
        EncodingService service = runtime.getEncodingService();
        Encoding ascii8bit = service.getAscii8bitEncoding();
        Encoding extEncoding = null;
        Encoding intEncoding = null;
        boolean isBom = false;

        String[] encs = option.split(":", 2);

        if (encs[0].toLowerCase().startsWith("bom|utf-")) {
            isBom = true;
            encs[0] = encs[0].substring(4);
        }

        extEncoding = service.getEncodingFromString(encs[0]);

        if (encs.length > 1) {
            if (encs[1].equals("-")) {
                // null;
            } else {
                intEncoding = service.getEncodingFromString(encs[1]);
            }
        }

        // Duplicating some rb_io_ext_int_to_encs logic here, which is also in RubyIO#setupReadWriteEncodings.
View Full Code Here

    }
   
    @JRubyMethod(meta = true)
    public static IRubyObject asciicompat_encoding(ThreadContext context, IRubyObject self, IRubyObject strOrEnc) {
        Ruby runtime = context.runtime;
        EncodingService encodingService = runtime.getEncodingService();
       
        Encoding encoding = encodingService.getEncodingFromObjectNoError(strOrEnc);
       
        if (encoding == null) {
            return context.nil;
        }
       
        if (encoding.isAsciiCompatible()) {
            return context.nil;
        }
       
        Encoding asciiCompat = NONASCII_TO_ASCII.get(encoding);
       
        if (asciiCompat == null) {
            throw runtime.newConverterNotFoundError("no ASCII compatible encoding found for " + strOrEnc);
        }
       
        return encodingService.convertEncodingToRubyEncoding(asciiCompat);
    }
View Full Code Here

        RubyComparable.createComparable(this);
        RubyEnumerable.createEnumerableModule(this);
        RubyString.createStringClass(this);

        encodingService = new EncodingService(this);

        RubySymbol.createSymbolClass(this);
       
        recursiveKey = newSymbol("__recursive_key__");
View Full Code Here

        }
    }

    @JRubyMethod
    public IRubyObject external_encoding(ThreadContext context) {
        EncodingService encodingService = context.runtime.getEncodingService();
       
        if (openFile.encs.enc2 != null) return encodingService.getEncoding(openFile.encs.enc2);
       
        if (openFile.isWritable()) {
            return openFile.encs.enc == null ? context.runtime.getNil() : encodingService.getEncoding(openFile.encs.enc);
        }
       
        return encodingService.getEncoding(getReadEncoding());
    }
View Full Code Here

TOP

Related Classes of org.jruby.runtime.encoding.EncodingService

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.