Ejemplo n.º 1
0
  @JRubyMethod
  public IRubyObject convert(ThreadContext context, IRubyObject srcBuffer) {
    if (!(srcBuffer instanceof RubyString)) {
      throw context.runtime.newTypeError(srcBuffer, context.runtime.getString());
    }

    RubyString srcString = (RubyString) srcBuffer;

    ByteList srcBL = srcString.getByteList();

    if (srcBL.getRealSize() == 0) return context.runtime.newSymbol("source_buffer_empty");

    ByteBuffer srcBB = ByteBuffer.wrap(srcBL.getUnsafeBytes(), srcBL.begin(), srcBL.getRealSize());
    try {
      CharBuffer srcCB =
          CharBuffer.allocate((int) (srcDecoder.maxCharsPerByte() * srcBL.getRealSize()) + 1);
      CoderResult decodeResult = srcDecoder.decode(srcBB, srcCB, true);
      srcCB.flip();

      ByteBuffer destBB =
          ByteBuffer.allocate((int) (destEncoder.maxBytesPerChar() * srcCB.limit()) + 1);
      CoderResult encodeResult = destEncoder.encode(srcCB, destBB, true);
      destBB.flip();

      byte[] destBytes = new byte[destBB.limit()];
      destBB.get(destBytes);

      srcDecoder.reset();
      destEncoder.reset();

      return context.runtime.newString(new ByteList(destBytes, destEncoding.getEncoding(), false));
    } catch (Exception e) {
      throw context.runtime.newRuntimeError(e.getLocalizedMessage());
    }
  }
Ejemplo n.º 2
0
  @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);
  }
 private synchronized IRubyObject status(Ruby runtime) {
   if (threadImpl.isAlive()) {
     return RubyString.newStringShared(runtime, status.get().bytes);
   } else if (exitingException != null) {
     return runtime.getNil();
   } else {
     return runtime.getFalse();
   }
 }
Ejemplo n.º 4
0
 @JRubyMethod
 public IRubyObject inspect(ThreadContext context) {
   return RubyString.newString(
       context.runtime,
       "#<Encoding::Converter: "
           + srcDecoder.charset().name()
           + " to "
           + destEncoder.charset().name());
 }
Ejemplo n.º 5
0
    @JRubyMethod
    public static IRubyObject readagain_bytes(ThreadContext context, IRubyObject self) {
      RubyCoderResult result = (RubyCoderResult) self.dataGetStruct();

      if (result != null && result.isError() && result.readagainBytes != null) {
        // FIXME: do this elsewhere and cache it
        ByteList errorBytes = new ByteList(result.readagainBytes, ASCIIEncoding.INSTANCE, true);
        return RubyString.newString(context.runtime, errorBytes);
      }

      return context.nil;
    }
Ejemplo n.º 6
0
  @Override
  @JRubyMethod(
      name = {"to_s", "inspect"},
      optional = 1)
  public IRubyObject to_s(ThreadContext context, IRubyObject[] args) {
    String s =
        size != Long.MAX_VALUE
            ? String.format(
                "#<%s address=0x%x size=%s>", getMetaClass().getName(), getAddress(), size)
            : String.format("#<%s address=0x%x>", getMetaClass().getName(), getAddress());

    return RubyString.newString(context.runtime, s);
  }
Ejemplo n.º 7
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;
  }
Ejemplo n.º 8
0
    @JRubyMethod
    public static IRubyObject destination_encoding_name(ThreadContext context, IRubyObject self) {
      RubyCoderResult result = (RubyCoderResult) self.dataGetStruct();

      return RubyString.newString(context.runtime, result.outEncoding.getName());
    }
Ejemplo n.º 9
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);
  }
Ejemplo n.º 10
0
 @JRubyMethod
 public IRubyObject inspect(ThreadContext context) {
   return RubyString.newString(
       context.runtime,
       "#<Encoding::Converter: " + transcoder.inEncoding + " to " + transcoder.outEncoding);
 }
Ejemplo n.º 11
0
 @JRubyMethod(compat = RUBY1_9)
 public IRubyObject replacement(ThreadContext context) {
   return RubyString.newString(context.runtime, srcDecoder.replacement());
 }