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

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

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

revision 1.2 by mark, Tue Apr 30 21:37:27 2002 UTC revision 1.3 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  class CoderResult  import java.nio.BufferOverflowException;
41    import java.nio.BufferUnderflowException;
42    import java.util.HashMap;
43    
44    /**
45     * @author Jesse Rosenstock
46     * @since 1.4
47     */
48    public class CoderResult
49  {  {
50      boolean err;    public static final CoderResult OVERFLOW
51        = new CoderResult (TYPE_OVERFLOW, 0);
52      boolean isError()    public static final CoderResult UNDERFLOW
53      {      = new CoderResult (TYPE_UNDERFLOW, 0);
54          return err;    
55      }    private static final int TYPE_MALFORMED  = 0;
56      private static final int TYPE_OVERFLOW   = 1;
57      boolean isMalformed()    private static final int TYPE_UNDERFLOW  = 2;
58      {    private static final int TYPE_UNMAPPABLE = 3;
59          return false;  
60      }    private static final String[] names
61        = { "MALFORMED", "OVERFLOW", "UNDERFLOW", "UNMAPPABLE" };
62      boolean isOverflow()  
63      {    private static final Cache malformedCache
64          return false;      = new Cache ()
65      }        {
66            protected CoderResult make (int length)
67      boolean isUnderflow()          {
68      {            return new CoderResult (TYPE_MALFORMED, length);
69          return false;          }
70      }        };
71    
72      boolean isUnmappable()    private static final Cache unmappableCache
73      {      = new Cache ()
74          return false;        {
75      }          protected CoderResult make (int length)
76            {
77      int length()            return new CoderResult (TYPE_UNMAPPABLE, length);
78      {          }
79          return 0;        };
80      }  
81      private final int type;
82      static CoderResult malformedForLength(int length)    private final int length;
83      {  
84          return null;    private CoderResult (int type, int length)
85      }    {
86        this.type = type;
87        this.length = length;
88      }
89    
90      public boolean isError ()
91      {
92        return length > 0;
93      }
94    
95      public boolean isMalformed ()
96      {
97        return type == TYPE_MALFORMED;
98      }
99    
100      public boolean isOverflow ()
101      {
102        return type == TYPE_OVERFLOW;
103      }
104    
105      public boolean isUnderflow ()
106      {
107        return type == TYPE_UNDERFLOW;
108      }
109    
110      public boolean isUnmappable ()
111      {
112        return type == TYPE_UNMAPPABLE;
113      }
114    
115      public int length ()
116      {
117        if (length <= 0)
118          throw new UnsupportedOperationException ();
119        else
120          return length;
121      }
122    
123      public static CoderResult malformedForLength (int length)
124      {
125        return malformedCache.get (length);
126      }
127            
128      void throwException()    public void throwException ()
129          throws CharacterCodingException      throws CharacterCodingException
130      {    {
131          throw new CharacterCodingException();      switch (type)
132      }        {
133            case TYPE_MALFORMED:
134              throw new MalformedInputException (length);
135            case TYPE_OVERFLOW:
136              throw new BufferOverflowException ();
137            case TYPE_UNDERFLOW:
138              throw new BufferUnderflowException ();
139            case TYPE_UNMAPPABLE:
140              throw new UnmappableCharacterException (length);
141          }
142      }
143    
144      public String toString ()
145      {
146        String name = names[type];
147        return (length > 0) ? name + '[' + length + ']' : name;
148      }
149    
150      public static CoderResult unmappableForLength (int length)
151      {
152        return unmappableCache.get (length);
153      }    
154    
155      private abstract static class Cache
156      {
157        private final HashMap cache;
158    
159        private Cache ()
160        {
161          // If we didn't synchronize on this, then cache would be initialized
162          // without holding a lock.  Undefined behavior would occur if the
163          // first thread to call get(int) was not the same as the one that
164          // called the constructor.
165          synchronized (this)
166            {
167              cache = new HashMap ();
168            }
169        }
170    
171        private synchronized CoderResult get (int length)
172        {
173          if (length <= 0)
174            throw new IllegalArgumentException ("Non-positive length");
175    
176          Integer len = new Integer (length);
177          CoderResult cr = null;
178          Object o;
179          if ((o = cache.get (len)) != null)
180            cr = (CoderResult) ((WeakReference) o).get ();
181          if (cr == null)
182            {
183              cr = make (length);
184              cache.put (len, cr);
185            }
186    
187      public String toString()        return cr;
     {  
         return "coder error";  
188      }      }
189    
190      static CoderResult unmappableForLength(int length)      protected abstract CoderResult make (int length);
191      {    }
         return null;  
     }      
   
192  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

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