/[classpath]/classpath/java/nio/charset/Charset.java
ViewVC logotype

Diff of /classpath/java/nio/charset/Charset.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5 by mark, Tue Apr 30 21:37:27 2002 UTC revision 1.6 by mkoch, Fri Nov 8 12:15:32 2002 UTC
# Line 37  exception statement from your version. * Line 37  exception statement from your version. *
37    
38  package java.nio.charset;  package java.nio.charset;
39    
40    import java.nio.ByteBuffer;
41  import java.nio.*;  import java.nio.CharBuffer;
42    import java.util.HashSet;
43  public class Charset  import java.util.Set;
44    import java.util.TreeMap;
45    import gnu.java.nio.charset.Provider;
46    
47    /**
48     * @author Jesse Rosenstock
49     * @since 1.4
50     */
51    public abstract class Charset implements Comparable
52  {  {
53      public static Charset forName(String name)    private final String canonicalName;
54      {    private final String[] aliases;
55          return new Charset();    
56      }    protected Charset (String canonicalName, String[] aliases)
57      {
58      public CharsetDecoder newDecoder()      checkName (canonicalName);
59      {        if (aliases != null)
60          return new CharsetDecoder(this,2,2)        {
61              {          int n = aliases.length;
62                  protected CoderResult decodeLoop(ByteBuffer  in,          for (int i = 0; i < n; ++i)
63                                                   CharBuffer  out)              checkName (aliases[i]);
64                  {        }
65                      while (in.hasRemaining())  
66                          {      this.canonicalName = canonicalName;
67                              char a = (char) in.get();      this.aliases = aliases;
68                              out.put(a);    }
69                          }  
70                      return null;    /**
71                  }     * @throws IllegalCharsetNameException  if the name is illegal
72              };     */
73      }    private static void checkName (String name)
74      {
75      public CharsetEncoder newEncoder()      int n = name.length ();
76      {            
77          return new CharsetEncoder(this,2,2)      if (n == 0)
78              {        throw new IllegalCharsetNameException (name);
79                  protected CoderResult encodeLoop(CharBuffer  in,  
80                                                   ByteBuffer  out)      char ch = name.charAt (0);
81                  {      if (!(('A' <= ch && ch <= 'Z')
82                      //System.out.println("in encode loop:"+in.hasRemaining());            || ('a' <= ch && ch <= 'z')
83              || ('0' <= ch && ch <= '9')))
84                      while (in.hasRemaining())        throw new IllegalCharsetNameException (name);
85                          {  
86                              char a = in.get();      for (int i = 1; i < n; ++i)
87                              out.put((byte)a);        {
88            ch = name.charAt (i);
89                              //int len = out.position();          if (!(('A' <= ch && ch <= 'Z')
90                              //System.out.println("pos="+len + ","+a);                || ('a' <= ch && ch <= 'z')
91                          }                || ('0' <= ch && ch <= '9')
92                      return null;                || ch == '-' || ch == '.' || ch == ':' || ch == '_'))
93                  }            throw new IllegalCharsetNameException (name);
94              };        }
95      }    }
96    
97      public static boolean isSupported (String charsetName)
98      {
99        return charsetForName (charsetName) != null;
100      }
101    
102      public static Charset forName (String charsetName)
103      {
104        Charset cs = charsetForName (charsetName);
105        if (cs == null)
106          throw new UnsupportedCharsetException (charsetName);
107        return cs;
108      }
109    
110      /**
111       * Retrieves a charset for the given charset name.
112       *
113       * @return A charset object for the charset with the specified name, or
114       *   <code>null</code> if no such charset exists.
115       *
116       * @throws IllegalCharsetNameException  if the name is illegal
117       */
118      private static Charset charsetForName (String charsetName)
119      {
120        checkName (charsetName);
121        return provider ().charsetForName (charsetName);
122      }
123    
124      public static SortedMap availableCharsets ()
125      {
126        TreeMap charsets = new TreeMap (String.CASE_INSENSITIVE_ORDER);
127    
128        for (Iterator i = provider ().charsets (); i.hasNext (); )
129          {
130            Charset cs = (Charset) i.next ();
131            charsets.put (cs.name (), cs);
132          }
133    
134        return Collections.unmodifiableSortedMap (charsets);
135      }
136    
137      // XXX: we need to support multiple providers, reading them from
138      // java.nio.charset.spi.CharsetProvider in the resource directory
139      // META-INF/services
140      private static final CharsetProvider provider ()
141      {
142        return Provider.provider ();
143      }
144    
145      public final String name ()
146      {
147        return canonicalName;
148      }
149    
150      public final Set aliases ()
151      {
152        if (aliases == null)
153          return Collections.EMPTY_SET;
154    
155        // should we cache the aliasSet instead?
156        int n = aliases.length;
157        HashSet aliasSet = new HashSet (n);
158        for (int i = 0; i < n; ++i)
159            aliasSet.add (aliases[i]);
160        return Collections.unmodifiableSet (aliasSet);
161      }
162    
163      public String displayName ()
164      {
165        return canonicalName;
166      }
167    
168      public String displayName (Locale locale)
169      {
170        return canonicalName;
171      }
172    
173      public final boolean isRegistered (String name)
174      {
175        return !name.startsWith ("x-") && !name.startsWith ("X-");
176      }
177    
178      public abstract boolean contains (Charset cs);
179    
180      public abstract CharsetDecoder newDecoder ();
181    
182      public abstract CharsetEncoder newEncoder ();
183    
184      public boolean canEncode ()
185      {
186        return true;
187      }
188    
189      public final ByteBuffer encode (CharBuffer cb)
190      {
191        try
192          {
193            // TODO: cache encoders between sucessive invocations
194            return newEncoder ().onMalformedInput (CodingErrorAction.REPLACE)
195                                .onUnmappableCharacter (CodingErrorAction.REPLACE)
196                                .encode (cb);
197          }
198        catch (CharacterCodingException e)
199          {
200            throw new AssertionError (e);
201          }
202      }
203      
204      public final ByteBuffer encode (String str)
205      {
206        return encode (CharBuffer.wrap (str));
207      }
208    
209      public CharBuffer decode (ByteBuffer bb)
210      {
211        try
212         {
213            // TODO: cache encoders between sucessive invocations
214            return newDecoder ().onMalformedInput (CodingErrorAction.REPLACE)
215                                .onUnmappableCharacter (CodingErrorAction.REPLACE)
216                                .decode (bb);
217          }
218        catch (CharacterCodingException e)
219          {
220            throw new AssertionError (e);
221          }
222      }
223    
224      public final int compareTo (Object ob)
225      {
226        return canonicalName.compareToIgnoreCase (((Charset) ob).canonicalName);
227      }
228    
229      public final int hashCode ()
230      {
231        return canonicalName.hashCode ();
232      }
233    
234      public final boolean equals (Object ob)
235      {
236        if (ob instanceof Charset)
237          return canonicalName.equalsIgnoreCase (((Charset) ob).canonicalName);
238        else
239          return false;
240      }
241    
242      public final String toString ()
243      {
244        return canonicalName;
245      }
246  }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.6

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26