Beispiel #1
0
  public void defaultVariablesUnmarshal(IRubyObject object) throws IOException {
    int count = unmarshalInt();

    RubyClass cls = object.getMetaClass().getRealClass();

    for (int i = 0; i < count; i++) {

      IRubyObject key = unmarshalObject(false);

      if (i == 0) { // first ivar provides encoding

        if (object instanceof EncodingCapable) {

          EncodingCapable strObj = (EncodingCapable) object;

          if (key.asJavaString().equals(MarshalStream.SYMBOL_ENCODING_SPECIAL)) {

            // special case for USASCII and UTF8
            if (unmarshalObject().isTrue()) {
              strObj.setEncoding(UTF8Encoding.INSTANCE);
            } else {
              strObj.setEncoding(USASCIIEncoding.INSTANCE);
            }
            continue;

          } else if (key.asJavaString().equals("encoding")) {

            IRubyObject encodingNameObj = unmarshalObject(false);
            String encodingNameStr = encodingNameObj.asJavaString();
            ByteList encodingName = new ByteList(ByteList.plain(encodingNameStr));

            Entry entry = runtime.getEncodingService().findEncodingOrAliasEntry(encodingName);
            if (entry == null) {
              throw runtime.newArgumentError(
                  "invalid encoding in marshaling stream: " + encodingName);
            }
            Encoding encoding = entry.getEncoding();
            strObj.setEncoding(encoding);
            continue;
          } // else fall through as normal ivar
        }
      }

      String name = key.asJavaString();
      IRubyObject value = unmarshalObject();

      cls.getVariableAccessorForWrite(name).set(object, value);
    }
  }
  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;
  }
 public Encoding toEncoding(Ruby runtime) {
   EncodingService service = runtime.getEncodingService();
   switch (this) {
     case LOCALE:
       return service.getLocaleEncoding();
     case EXTERNAL:
       return runtime.getDefaultExternalEncoding();
     case INTERNAL:
       return runtime.getDefaultInternalEncoding();
     case FILESYSTEM:
       // This needs to do something different on Windows. See encoding.c,
       // in the enc_set_filesystem_encoding function.
       return runtime.getDefaultExternalEncoding();
     default:
       throw new RuntimeException("invalid SpecialEncoding: " + this);
   }
 }