コード例 #1
0
ファイル: RubyConverter.java プロジェクト: RB-DPR/RB-DPR
  @JRubyMethod(compat = RUBY1_9)
  public IRubyObject last_error(ThreadContext context) {
    RaiseException lastError = transcoder.getLastError();

    if (lastError != null) return lastError.getException();

    return context.nil;
  }
コード例 #2
0
ファイル: RubyConverter.java プロジェクト: RB-DPR/RB-DPR
  @JRubyMethod(compat = RUBY1_9)
  public IRubyObject primitive_errinfo(ThreadContext context) {
    Ruby runtime = context.runtime;

    RubyCoderResult lastResult = transcoder.getLastResult();

    // if we have not done anything, produce an empty errinfo
    if (lastResult == null) {
      return runtime.newArray(
          new IRubyObject[] {
            runtime.newSymbol("source_buffer_empty"),
            context.nil,
            context.nil,
            context.nil,
            context.nil
          });
    }

    RubyArray errinfo = RubyArray.newArray(context.runtime);

    if (!lastResult.isError()) {
      return runtime.newArray(
          new IRubyObject[] {
            runtime.newSymbol(lastResult.stringResult),
            context.nil,
            context.nil,
            context.nil,
            context.nil
          });
    } else {
      errinfo.append(runtime.newSymbol(lastResult.stringResult));

      // FIXME: gross
      errinfo.append(RubyString.newString(runtime, lastResult.inEncoding.getName()));
      errinfo.append(RubyString.newString(runtime, lastResult.outEncoding.getName()));

      if (lastResult.isError() && lastResult.errorBytes != null) {
        // FIXME: do this elsewhere and cache it
        ByteList errorBytes = new ByteList(lastResult.errorBytes, lastResult.inEncoding, true);
        errinfo.append(RubyString.newString(runtime, errorBytes));
      } else {
        errinfo.append(RubyString.newEmptyString(runtime));
      }

      if (lastResult.readagainBytes != null) {
        // FIXME: do this elsewhere and cache it
        ByteList readagainBytes =
            new ByteList(lastResult.readagainBytes, lastResult.inEncoding, true);
        errinfo.append(RubyString.newString(runtime, readagainBytes));
      } else {
        errinfo.append(RubyString.newEmptyString(runtime));
      }
    }

    return errinfo;
  }
コード例 #3
0
ファイル: RubyConverter.java プロジェクト: RB-DPR/RB-DPR
  @JRubyMethod(compat = RUBY1_9)
  public IRubyObject replacement(ThreadContext context) {
    String replacement = transcoder.getReplaceWith();

    if (replacement == null) {
      return context.nil;
    }

    return context.runtime.newString(replacement);
  }
コード例 #4
0
ファイル: RubyConverter.java プロジェクト: RB-DPR/RB-DPR
  @JRubyMethod
  public IRubyObject convert(ThreadContext context, IRubyObject srcBuffer) {
    RubyString srcString = srcBuffer.convertToString();
    boolean is7BitAscii = srcString.isCodeRangeAsciiOnly();
    ByteList srcBL = srcString.getByteList();

    ByteList bytes = transcoder.convert(context, srcBL, is7BitAscii);

    return context.runtime.newString(bytes);
  }
コード例 #5
0
ファイル: RubyConverter.java プロジェクト: RB-DPR/RB-DPR
  @JRubyMethod(name = "replacement=", compat = RUBY1_9)
  public IRubyObject replacement_set(ThreadContext context, IRubyObject replacement) {
    transcoder.getCodingErrorActions().setReplaceWith(replacement.convertToString().asJavaString());

    return replacement;
  }
コード例 #6
0
ファイル: RubyConverter.java プロジェクト: RB-DPR/RB-DPR
 @JRubyMethod
 public IRubyObject finish(ThreadContext context) {
   // alternate encoding here should be smarter
   return context.runtime.newString(transcoder.finish(ASCIIEncoding.INSTANCE));
 }
コード例 #7
0
ファイル: RubyConverter.java プロジェクト: RB-DPR/RB-DPR
  @JRubyMethod(required = 2, optional = 4)
  public IRubyObject primitive_convert(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;

    RubyString input;
    RubyString output;
    int outputByteoffset = -1;
    int outputBytesize = 0;
    int flags = 0;

    int hashArg = -1;

    if (args.length > 2 && !args[2].isNil()) {
      if (args.length == 3 && args[2] instanceof RubyHash) {
        hashArg = 2;
      } else {
        outputByteoffset = (int) args[2].convertToInteger().getLongValue();
        if (outputByteoffset < 0) throw runtime.newArgumentError("negative offset");
      }
    }

    if (args.length > 3 && !args[3].isNil()) {
      if (args.length == 4 && args[3] instanceof RubyHash) {
        hashArg = 3;
      } else {
        outputBytesize = (int) args[3].convertToInteger().getLongValue();
        if (outputBytesize < 0) throw runtime.newArgumentError("negative bytesize");
      }
    }

    if (args.length > 4 && !args[4].isNil()) {
      if (args.length > 5 && !args[5].isNil()) {
        throw runtime.newArgumentError(args.length, 5);
      }

      if (args[4] instanceof RubyHash) {
        hashArg = 4;
      } else {
        flags = (int) args[4].convertToInteger().getLongValue();
      }
    }

    IRubyObject opt = context.nil;
    if (hashArg != -1 && !(opt = TypeConverter.checkHashType(runtime, args[hashArg])).isNil()) {
      IRubyObject v = ((RubyHash) opt).op_aref(context, runtime.newSymbol("partial_input"));
      if (v.isTrue()) {
        flags |= EncodingUtils.ECONV_PARTIAL_INPUT;
      }
      v = ((RubyHash) opt).op_aref(context, runtime.newSymbol("after_output"));
      if (v.isTrue()) {
        flags |= EncodingUtils.ECONV_AFTER_OUTPUT;
      }
    } else {
      flags = 0;
    }

    ByteList inBytes;
    ByteList outBytes;

    if (args[0].isNil()) {
      inBytes = new ByteList();
    } else {
      input = args[0].convertToString();
      input.modify19();
      inBytes = input.getByteList();
    }

    output = args[1].convertToString();
    output.modify19();
    outBytes = output.getByteList();

    if (outputByteoffset == -1) {
      outputByteoffset = outBytes.getRealSize();
    } else if (outputByteoffset > outBytes.getRealSize()) {
      throw runtime.newArgumentError("offset too big");
    }

    int outputByteEnd = outputByteoffset + outputBytesize;

    if (outputByteEnd > outBytes.getRealSize()) {
      outBytes.ensure(outputByteEnd);
    }

    RubyCoderResult result =
        transcoder.primitiveConvert(
            context,
            inBytes,
            output.getByteList(),
            outputByteoffset,
            outputBytesize,
            inBytes.getEncoding(),
            inBytes.getEncoding().isAsciiCompatible(),
            flags);

    outBytes.setEncoding(
        transcoder.outEncoding != null ? transcoder.outEncoding : inBytes.getEncoding());

    return symbolFromResult(result, runtime, flags, context);
  }