Esempio n. 1
0
 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);
   }
 }
Esempio n. 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;
  }
Esempio n. 3
0
  private void strioInit(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;
    RubyString string;
    IRubyObject mode;
    boolean trunc = false;

    switch (args.length) {
      case 2:
        mode = args[1];
        if (mode instanceof RubyFixnum) {
          int flags = RubyFixnum.fix2int(mode);
          ptr.flags = ModeFlags.getOpenFileFlagsFor(flags);
          trunc = (flags & ModeFlags.TRUNC) != 0;
        } else {
          String m = args[1].convertToString().toString();
          ptr.flags = OpenFile.ioModestrFmode(runtime, m);
          trunc = m.charAt(0) == 'w';
        }
        string = args[0].convertToString();
        if ((ptr.flags & OpenFile.WRITABLE) != 0 && string.isFrozen()) {
          throw runtime.newErrnoEACCESError("Permission denied");
        }
        if (trunc) {
          string.resize(0);
        }
        break;
      case 1:
        string = args[0].convertToString();
        ptr.flags = string.isFrozen() ? OpenFile.READABLE : OpenFile.READWRITE;
        break;
      case 0:
        string = RubyString.newEmptyString(runtime, runtime.getDefaultExternalEncoding());
        ptr.flags = OpenFile.READWRITE;
        break;
      default:
        throw runtime.newArgumentError(args.length, 2);
    }

    ptr.string = string;
    ptr.pos = 0;
    ptr.lineno = 0;
    // funky way of shifting readwrite flags into object flags
    flags |= (ptr.flags & OpenFile.READWRITE) * (STRIO_READABLE / OpenFile.READABLE);
  }
  @JRubyMethod(optional = 2, visibility = PRIVATE)
  @Override
  public IRubyObject initialize(IRubyObject[] args, Block unusedBlock) {
    Object modeArgument = null;
    Ruby runtime = getRuntime();

    switch (args.length) {
      case 0:
        data.internal =
            runtime.is1_9()
                ? RubyString.newEmptyString(runtime, runtime.getDefaultExternalEncoding())
                : RubyString.newEmptyString(getRuntime());
        modeArgument = "r+";
        break;
      case 1:
        data.internal = args[0].convertToString();
        modeArgument = data.internal.isFrozen() ? "r" : "r+";
        break;
      case 2:
        data.internal = args[0].convertToString();
        if (args[1] instanceof RubyFixnum) {
          modeArgument = RubyFixnum.fix2long(args[1]);
        } else {
          modeArgument = args[1].convertToString().toString();
        }
        break;
    }

    initializeModes(modeArgument);

    if (data.modes.isWritable() && data.internal.isFrozen()) {
      throw getRuntime().newErrnoEACCESError("Permission denied");
    }

    if (data.modes.isTruncate()) {
      data.internal.modifyCheck();
      data.internal.empty();
    }

    return this;
  }