Example #1
0
  public static RubyEncoding encoding(ThreadContext context, MutableCallSite site, String name) {
    RubyEncoding rubyEncoding = IRRuntimeHelpers.retrieveEncoding(context, name);

    MethodHandle constant = (MethodHandle) rubyEncoding.constant();
    if (constant == null)
      constant = (MethodHandle) OptoFactory.newConstantWrapper(RubyEncoding.class, rubyEncoding);

    site.setTarget(constant);

    return rubyEncoding;
  }
  @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());
    }
  }
Example #3
0
 private void defineEncodings() {
   HashEntryIterator hei = encodings.entryIterator();
   while (hei.hasNext()) {
     CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry> e =
         ((CaseInsensitiveBytesHash.CaseInsensitiveBytesHashEntry<Entry>) hei.next());
     Entry ee = e.value;
     RubyEncoding encoding = RubyEncoding.newEncoding(runtime, e.bytes, e.p, e.end, ee.isDummy());
     encodingList[ee.getIndex()] = encoding;
     defineEncodingConstants(runtime, encoding, e.bytes, e.p, e.end);
   }
 }
  @JRubyMethod(visibility = PRIVATE)
  public IRubyObject initialize(ThreadContext context, IRubyObject src, IRubyObject dest) {

    if (src instanceof RubyEncoding) {
      srcEncoding = (RubyEncoding) src;
    } else {
      srcEncoding = (RubyEncoding) context.runtime.getEncodingService().rubyEncodingFromObject(src);
    }

    if (dest instanceof RubyEncoding) {
      destEncoding = (RubyEncoding) dest;
    } else {
      destEncoding =
          (RubyEncoding) context.runtime.getEncodingService().rubyEncodingFromObject(dest);
    }

    try {
      srcDecoder =
          context
              .runtime
              .getEncodingService()
              .charsetForEncoding(srcEncoding.getEncoding())
              .newDecoder();
      destEncoder =
          context
              .runtime
              .getEncodingService()
              .charsetForEncoding(destEncoding.getEncoding())
              .newEncoder();
    } catch (RaiseException e) {
      if (e.getException().getMetaClass().getBaseName().equals("CompatibilityError")) {
        throw context.runtime.newConverterNotFoundError(
            "code converter not found (" + srcEncoding + " to " + destEncoding + ")");
      } else {
        throw e;
      }
    }

    return context.runtime.getNil();
  }
Example #5
0
  public EncodingService(Ruby runtime) {
    this.runtime = runtime;
    encodings = EncodingDB.getEncodings();
    aliases = EncodingDB.getAliases();
    ascii8bit = encodings.get("ASCII-8BIT".getBytes()).getEncoding();

    Charset javaDefaultCharset = Charset.defaultCharset();
    ByteList javaDefaultBL = new ByteList(javaDefaultCharset.name().getBytes());
    Entry javaDefaultEntry = findEncodingOrAliasEntry(javaDefaultBL);
    javaDefault = javaDefaultEntry == null ? ascii8bit : javaDefaultEntry.getEncoding();

    encodingList = new IRubyObject[encodings.size()];

    if (runtime.is1_9()) {
      RubyEncoding.createEncodingClass(runtime);
      RubyConverter.createConverterClass(runtime);
      defineEncodings();
      defineAliases();

      // External should always have a value, but Encoding.external_encoding{,=} will lazily setup
      String encoding = runtime.getInstanceConfig().getExternalEncoding();
      if (encoding != null && !encoding.equals("")) {
        Encoding loadedEncoding = loadEncoding(ByteList.create(encoding));
        if (loadedEncoding == null)
          throw new MainExitException(1, "unknown encoding name - " + encoding);
        runtime.setDefaultExternalEncoding(loadedEncoding);
      } else {
        Encoding consoleEncoding = getConsoleEncoding();
        Encoding availableEncoding =
            consoleEncoding == null ? getLocaleEncoding() : consoleEncoding;
        runtime.setDefaultExternalEncoding(availableEncoding);
      }

      encoding = runtime.getInstanceConfig().getInternalEncoding();
      if (encoding != null && !encoding.equals("")) {
        Encoding loadedEncoding = loadEncoding(ByteList.create(encoding));
        if (loadedEncoding == null)
          throw new MainExitException(1, "unknown encoding name - " + encoding);
        runtime.setDefaultInternalEncoding(loadedEncoding);
      }
    }
  }
Example #6
0
File: Dir.java Project: vvs/jruby
 private static String newStringFromUTF8(byte[] buf) {
   return RubyEncoding.decodeUTF8(buf);
 }
Example #7
0
File: Dir.java Project: vvs/jruby
 private static String newStringFromUTF8(byte[] buf, int offset, int len) {
   return RubyEncoding.decodeUTF8(buf, offset, len);
 }
Example #8
0
File: Dir.java Project: vvs/jruby
 private static byte[] getBytesInUTF8(String s) {
   return RubyEncoding.encodeUTF8(s);
 }
 public static String getHashForString(String str) {
   return getHashForBytes(RubyEncoding.encodeUTF8(str));
 }