1 package org.jruby.charset; 2 3 import java.nio.ByteBuffer ; 4 import java.nio.CharBuffer ; 5 import java.nio.charset.Charset ; 6 import java.nio.charset.CharsetDecoder ; 7 import java.nio.charset.CharsetEncoder ; 8 import java.nio.charset.CoderResult ; 9 10 public class PlainCharset extends Charset { 11 public PlainCharset() { 12 super("PLAIN", new String [0]); 13 } 14 public boolean contains(final Charset chs) { 15 return chs instanceof PlainCharset; 16 } 17 18 public CharsetDecoder newDecoder() { 19 return new PlainCharsetDecoder(this); 20 } 21 22 public CharsetEncoder newEncoder() { 23 return new PlainCharsetEncoder(this); 24 } 25 26 private static class PlainCharsetDecoder extends CharsetDecoder { 27 PlainCharsetDecoder(final PlainCharset charset) { 28 super(charset,1F,1F); 29 } 30 public CoderResult decodeLoop(final ByteBuffer in, final CharBuffer out) { 31 while(in.remaining() > 0 && out.remaining() > 0) { 32 out.put((char)(in.get() & 0xFF)); 33 } 34 if(in.remaining() > 0) { 35 return CoderResult.OVERFLOW; 36 } 37 return CoderResult.UNDERFLOW; 38 } 39 } 40 41 private static class PlainCharsetEncoder extends CharsetEncoder { 42 PlainCharsetEncoder(final PlainCharset charset) { 43 super(charset,1F,1F); 44 } 45 public CoderResult encodeLoop(final CharBuffer in, final ByteBuffer out) { 46 while(in.remaining() > 0 && out.remaining() > 0) { 47 out.put((byte)in.get()); 48 } 49 if(in.remaining() > 0) { 50 return CoderResult.OVERFLOW; 51 } 52 return CoderResult.UNDERFLOW; 53 } 54 } 55 } 56 | Popular Tags |