Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby;
 
 
 
 import static org.jruby.CompatVersion.*;
 import static org.jruby.runtime.Visibility.*;
 
 @JRubyClass(name="Converter")
 public class RubyConverter extends RubyObject {
     private RubyEncoding srcEncoding;
     private RubyEncoding destEncoding;
     private CharsetDecoder srcDecoder;
     private CharsetEncoder destEncoder;
 
     public static RubyClass createConverterClass(Ruby runtime) {
         RubyClass converterc = runtime.defineClassUnder("Converter"runtime.getClass("Data"), runtime.getEncoding());
         runtime.setConverter(converterc);
         converterc.index = .;
         converterc.setReifiedClass(RubyConverter.class);
         converterc.kindOf = new RubyModule.KindOf() {
             @Override
             public boolean isKindOf(IRubyObject objRubyModule type) {
                 return obj instanceof RubyConverter;
             }
         };
 
         converterc.defineAnnotatedMethods(RubyConverter.class);
         return converterc;
     }
 
     private static ObjectAllocator CONVERTER_ALLOCATOR = new ObjectAllocator() {
         public IRubyObject allocate(Ruby runtimeRubyClass klass) {
             return new RubyConverter(runtimeklass);
         }
     };
 
     private static final Encoding UTF16 = .;
 
     public RubyConverter(Ruby runtimeRubyClass klass) {
         super(runtimeklass);
     }
 
     public RubyConverter(Ruby runtime) {
         super(runtimeruntime.getConverter());
     }
 
     @JRubyMethod(visibility = )
     public IRubyObject initialize(ThreadContext contextIRubyObject convpath) {
         return context.runtime.getNil();
     }
 
     @JRubyMethod(visibility = )
     public IRubyObject initialize(ThreadContext contextIRubyObject srcIRubyObject dest) {
          = (RubyEncoding)context.runtime.getEncodingService().rubyEncodingFromObject(src);
          = (RubyEncoding)context.runtime.getEncodingService().rubyEncodingFromObject(dest);
 
 
         return context.runtime.getNil();
    }
    @JRubyMethod(visibility = )
    public IRubyObject initialize(ThreadContext contextIRubyObject srcIRubyObject destIRubyObject opt) {
        // TODO: opt
        initialize(contextsrcdest);
        return context.runtime.getNil();
    }
    public IRubyObject inspect(ThreadContext context) {
        return RubyString.newString(context.runtime"#<Encoding::Converter: " + .charset().name() + " to " + .charset().name());
    }
    public IRubyObject convpath(ThreadContext context) {
        // we always pass through UTF-16
        IRubyObject utf16Encoding = context.runtime.getEncodingService().getEncodingList()[.getIndex()];
        return RubyArray.newArray(
                context.runtime,
                RubyArray.newArray(context.runtimeutf16Encoding),
                RubyArray.newArray(context.runtimeutf16Encoding)
        );
    }
    public IRubyObject source_encoding() {
        return ;
    }
    public IRubyObject destination_encoding() {
        return ;
    }
    public IRubyObject primitive_convert(ThreadContext contextIRubyObject srcIRubyObject dest) {
        RubyString result = (RubyString)convert(contextsrc);
        dest.convertToString().replace19(result);
        return context.runtime.newSymbol("finished");
    }
    public IRubyObject convert(ThreadContext contextIRubyObject srcBuffer) {
        if (!(srcBuffer instanceof RubyString)) {
            throw context.runtime.newTypeError(srcBuffercontext.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) (.maxCharsPerByte() * srcBL.getRealSize()) + 1);
            CoderResult decodeResult = .decode(srcBBsrcCBtrue);
            srcCB.flip();
            ByteBuffer destBB = ByteBuffer.allocate((int) (.maxBytesPerChar() * srcCB.limit()) + 1);
            CoderResult encodeResult = .encode(srcCBdestBBtrue);
            destBB.flip();
            byte[] destBytes = new byte[destBB.limit()];
            destBB.get(destBytes);
            .reset();
            .reset();
            
            return context.runtime.newString(new ByteList(destBytes.getEncoding(), false));
        } catch (Exception e) {
            throw context.runtime.newRuntimeError(e.getLocalizedMessage());
        }
    }
    @JRubyMethod(compat = )
    public IRubyObject replacement(ThreadContext context) {
        return RubyString.newString(context.runtime.replacement());
    }
    @JRubyMethod(name = "replacement=", compat = )
    public IRubyObject replacement_set(ThreadContext contextIRubyObject replacement) {
        .replaceWith(replacement.convertToString().asJavaString());
        return replacement;
    }}
New to GrepCode? Check out our FAQ X