KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > charset > PlainCharset


1 package org.jruby.charset;
2
3 import java.nio.ByteBuffer JavaDoc;
4 import java.nio.CharBuffer JavaDoc;
5 import java.nio.charset.Charset JavaDoc;
6 import java.nio.charset.CharsetDecoder JavaDoc;
7 import java.nio.charset.CharsetEncoder JavaDoc;
8 import java.nio.charset.CoderResult JavaDoc;
9
10 public class PlainCharset extends Charset JavaDoc {
11     public PlainCharset() {
12         super("PLAIN", new String JavaDoc[0]);
13     }
14     public boolean contains(final Charset JavaDoc chs) {
15         return chs instanceof PlainCharset;
16     }
17     
18     public CharsetDecoder JavaDoc newDecoder() {
19         return new PlainCharsetDecoder(this);
20     }
21
22     public CharsetEncoder JavaDoc newEncoder() {
23         return new PlainCharsetEncoder(this);
24     }
25
26     private static class PlainCharsetDecoder extends CharsetDecoder JavaDoc {
27         PlainCharsetDecoder(final PlainCharset charset) {
28             super(charset,1F,1F);
29         }
30         public CoderResult JavaDoc decodeLoop(final ByteBuffer JavaDoc in, final CharBuffer JavaDoc 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 JavaDoc {
42         PlainCharsetEncoder(final PlainCharset charset) {
43             super(charset,1F,1F);
44         }
45         public CoderResult JavaDoc encodeLoop(final CharBuffer JavaDoc in, final ByteBuffer JavaDoc 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