1 /* ByteCharset.java -- Abstract class for generic 1-byte encodings.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.nio.charset;
40 import java.nio.ByteBuffer;
41 import java.nio.CharBuffer;
42 import java.nio.charset.Charset;
43 import java.nio.charset.CharsetDecoder;
44 import java.nio.charset.CharsetEncoder;
45 import java.nio.charset.CoderResult;
48 * A generic encoding framework for single-byte encodings,
49 * utilizing a look-up table.
51 * This replaces the gnu.java.io.EncoderEightBitLookup class,
52 * created by Aron Renn.
54 * @author Sven de Marothy
56 abstract class ByteCharset extends Charset
58 protected char[] lookupTable;
60 * Char to signify the character in the table is undefined
62 protected static final char NONE = (char)0xFFFD;
64 ByteCharset (String canonicalName, String[] aliases)
66 super (canonicalName, aliases);
70 * Most western charsets include ASCII, but this should
71 * be overloaded for others.
73 public boolean contains (Charset cs)
75 return cs instanceof US_ASCII || (cs.getClass() == getClass());
78 char[] getLookupTable()
83 public CharsetDecoder newDecoder ()
85 return new Decoder (this);
88 public CharsetEncoder newEncoder ()
90 return new Encoder (this);
93 private static final class Decoder extends CharsetDecoder
95 private char[] lookup;
97 // Package-private to avoid a trampoline constructor.
98 Decoder (ByteCharset cs)
100 super (cs, 1.0f, 1.0f);
101 lookup = cs.getLookupTable();
104 protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out)
106 // TODO: Optimize this in the case in.hasArray() / out.hasArray()
107 while (in.hasRemaining ())
112 if (!out.hasRemaining ())
114 in.position (in.position () - 1);
115 return CoderResult.OVERFLOW;
118 if((c = lookup[(int) (b & 0xFF)]) == NONE);
119 // return CoderResult.unmappableForLength (1);
123 return CoderResult.UNDERFLOW;
127 private static final class Encoder extends CharsetEncoder
129 private byte[] lookup;
131 // Package-private to avoid a trampoline constructor.
132 Encoder (ByteCharset cs)
134 super (cs, 1.0f, 1.0f);
136 char[] lookup_table = cs.getLookupTable();
138 // Create the inverse look-up table.
139 // determine required size of encoding_table:
141 for (int i = 0; i < lookup_table.length; i++)
143 int c = (int)lookup_table[i];
144 max = (c > max && c < NONE) ? c : max;
147 lookup = new byte[max+1];
149 for (int i = 0; i < lookup_table.length; i++)
151 int c = (int)lookup_table[i];
152 if (c != 0 && c < NONE)
159 protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
161 // TODO: Optimize this in the case in.hasArray() / out.hasArray()
162 while (in.hasRemaining ())
164 int c = (int)in.get ();
166 if (!out.hasRemaining ())
168 in.position (in.position () - 1);
169 return CoderResult.OVERFLOW;
172 // lookup byte encoding
173 byte b = (c < lookup.length) ? lookup[c] : (byte)0;
175 if ((int)b != 0 || (int)c == 0)
179 in.position (in.position () - 1);
180 return CoderResult.unmappableForLength (1);
184 return CoderResult.UNDERFLOW;