Example #1
0
  public EncodingService(Ruby runtime) {
    this.runtime = runtime;
    encodings = EncodingDB.getEncodings();
    aliases = EncodingDB.getAliases();
    ascii8bit = encodings.get("ASCII-8BIT".getBytes()).getEncoding();

    Charset javaDefaultCharset = Charset.defaultCharset();
    ByteList javaDefaultBL = new ByteList(javaDefaultCharset.name().getBytes());
    Entry javaDefaultEntry = findEncodingOrAliasEntry(javaDefaultBL);
    javaDefault = javaDefaultEntry == null ? ascii8bit : javaDefaultEntry.getEncoding();

    encodingList = new IRubyObject[encodings.size()];

    if (runtime.is1_9()) {
      RubyEncoding.createEncodingClass(runtime);
      RubyConverter.createConverterClass(runtime);
      defineEncodings();
      defineAliases();

      // External should always have a value, but Encoding.external_encoding{,=} will lazily setup
      String encoding = runtime.getInstanceConfig().getExternalEncoding();
      if (encoding != null && !encoding.equals("")) {
        Encoding loadedEncoding = loadEncoding(ByteList.create(encoding));
        if (loadedEncoding == null)
          throw new MainExitException(1, "unknown encoding name - " + encoding);
        runtime.setDefaultExternalEncoding(loadedEncoding);
      } else {
        Encoding consoleEncoding = getConsoleEncoding();
        Encoding availableEncoding =
            consoleEncoding == null ? getLocaleEncoding() : consoleEncoding;
        runtime.setDefaultExternalEncoding(availableEncoding);
      }

      encoding = runtime.getInstanceConfig().getInternalEncoding();
      if (encoding != null && !encoding.equals("")) {
        Encoding loadedEncoding = loadEncoding(ByteList.create(encoding));
        if (loadedEncoding == null)
          throw new MainExitException(1, "unknown encoding name - " + encoding);
        runtime.setDefaultInternalEncoding(loadedEncoding);
      }
    }
  }
Example #2
0
  public IRubyObject getDefaultExternal() {
    IRubyObject defaultExternal =
        convertEncodingToRubyEncoding(runtime.getDefaultExternalEncoding());

    if (defaultExternal.isNil()) {
      // TODO: MRI seems to default blindly to US-ASCII and we were using Charset default from
      // Java...which is right?
      ByteList encodingName = ByteList.create("US-ASCII");
      Encoding encoding = runtime.getEncodingService().loadEncoding(encodingName);

      runtime.setDefaultExternalEncoding(encoding);
      defaultExternal = convertEncodingToRubyEncoding(encoding);
    }

    return defaultExternal;
  }