Esempio n. 1
0
  @SuppressWarnings("unchecked")
  @JRubyMethod(name = "aliases", meta = true)
  public static IRubyObject aliases(ThreadContext context, IRubyObject recv) {
    Ruby runtime = context.runtime;
    EncodingService service = runtime.getEncodingService();

    IRubyObject list[] = service.getEncodingList();
    HashEntryIterator i = service.getAliases().entryIterator();
    RubyHash result = RubyHash.newHash(runtime);

    while (i.hasNext()) {
      CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
          ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) i.next());
      IRubyObject alias =
          RubyString.newUsAsciiStringShared(runtime, e.bytes, e.p, e.end - e.p).freeze(context);
      IRubyObject name =
          RubyString.newUsAsciiStringShared(runtime, ((RubyEncoding) list[e.value.getIndex()]).name)
              .freeze(context);
      result.fastASet(alias, name);
    }

    result.fastASet(
        runtime.newString(EXTERNAL),
        runtime.newString(new ByteList(runtime.getDefaultExternalEncoding().getName())));
    result.fastASet(
        runtime.newString(LOCALE),
        runtime.newString(new ByteList(service.getLocaleEncoding().getName())));

    return result;
  }
Esempio n. 2
0
  @JRubyMethod(visibility = PRIVATE)
  public IRubyObject initialize(
      ThreadContext context, IRubyObject src, IRubyObject dest, IRubyObject _opt) {
    Ruby runtime = context.runtime;
    EncodingService encodingService = runtime.getEncodingService();

    // both may be null
    Encoding srcEncoding = encodingService.getEncodingFromObjectNoError(src);
    Encoding destEncoding = encodingService.getEncodingFromObjectNoError(dest);

    int flags = 0;
    IRubyObject replace = context.nil;

    if (srcEncoding == destEncoding && srcEncoding != null) {
      throw runtime.newConverterNotFoundError(
          "code converter not found (" + srcEncoding + " to " + destEncoding + ")");
    }

    // Ensure we'll be able to get charsets fo these encodings
    try {
      if (srcEncoding != destEncoding) {
        if (srcEncoding != null) encodingService.charsetForEncoding(srcEncoding);
        if (destEncoding != null) encodingService.charsetForEncoding(destEncoding);
      }
    } catch (RaiseException e) {
      if (e.getException().getMetaClass().getBaseName().equals("CompatibilityError")) {
        throw runtime.newConverterNotFoundError(
            "code converter not found (" + srcEncoding + " to " + destEncoding + ")");
      } else {
        throw e;
      }
    }

    if (!_opt.isNil()) {
      if (_opt instanceof RubyHash) {
        RubyHash opt = (RubyHash) _opt;
        flags |= EncodingUtils.econvPrepareOpts(context, opt, new IRubyObject[] {opt});

        IRubyObject value = opt.fastARef(runtime.newSymbol("replace"));
        if (value != null) {
          replace = value;
        }
      } else {
        flags = (int) _opt.convertToInteger().getLongValue();
        replace = context.nil;
      }
    }

    transcoder = new CharsetTranscoder(context, destEncoding, srcEncoding, flags, replace);

    return context.runtime.getNil();
  }
Esempio n. 3
0
 private static IRubyObject getEnvTimeZone(Ruby runtime) {
   RubyString tzVar = runtime.newString(TZ_STRING);
   RubyHash h = ((RubyHash) runtime.getObject().getConstant("ENV"));
   IRubyObject tz = h.op_aref(runtime.getCurrentContext(), tzVar);
   return tz;
 }