Example #1
0
  @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;
  }
Example #2
0
  private IRubyObject symbolFromResult(
      RubyCoderResult result, Ruby runtime, int flags, ThreadContext context) {
    if (result != null) {
      return runtime.newSymbol(result.stringResult);
    }

    if ((flags & PARTIAL_INPUT) == 0) {
      return context.runtime.newSymbol("finished");
    } else {
      return context.runtime.newSymbol("source_buffer_empty");
    }
  }
Example #3
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();
  }
Example #4
0
  @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);
  }