1 7 8 package java.nio.charset; 9 10 import java.lang.ref.WeakReference ; 11 import java.nio.*; 12 import java.util.Map ; 13 import java.util.HashMap ; 14 15 16 67 68 public class CoderResult { 69 70 private static final int CR_UNDERFLOW = 0; 71 private static final int CR_OVERFLOW = 1; 72 private static final int CR_ERROR_MIN = 2; 73 private static final int CR_MALFORMED = 2; 74 private static final int CR_UNMAPPABLE = 3; 75 76 private static final String [] names 77 = { "UNDERFLOW", "OVERFLOW", "MALFORMED", "UNMAPPABLE" }; 78 79 private final int type; 80 private final int length; 81 82 private CoderResult(int type, int length) { 83 this.type = type; 84 this.length = length; 85 } 86 87 92 public String toString() { 93 String nm = names[type]; 94 return isError() ? nm + "[" + length + "]" : nm; 95 } 96 97 102 public boolean isUnderflow() { 103 return (type == CR_UNDERFLOW); 104 } 105 106 111 public boolean isOverflow() { 112 return (type == CR_OVERFLOW); 113 } 114 115 121 public boolean isError() { 122 return (type >= CR_ERROR_MIN); 123 } 124 125 132 public boolean isMalformed() { 133 return (type == CR_MALFORMED); 134 } 135 136 143 public boolean isUnmappable() { 144 return (type == CR_UNMAPPABLE); 145 } 146 147 157 public int length() { 158 if (!isError()) 159 throw new UnsupportedOperationException (); 160 return length; 161 } 162 163 168 public static final CoderResult UNDERFLOW 169 = new CoderResult (CR_UNDERFLOW, 0); 170 171 175 public static final CoderResult OVERFLOW 176 = new CoderResult (CR_OVERFLOW, 0); 177 178 private static abstract class Cache { 179 180 private Map cache = null; 181 182 protected abstract CoderResult create(int len); 183 184 private synchronized CoderResult get(int len) { 185 if (len <= 0) 186 throw new IllegalArgumentException ("Non-positive length"); 187 Integer k = new Integer (len); 188 WeakReference w; 189 CoderResult e = null; 190 if (cache == null) { 191 cache = new HashMap (); 192 } else if ((w = (WeakReference )cache.get(k)) != null) { 193 e = (CoderResult )w.get(); 194 } 195 if (e == null) { 196 e = create(len); 197 cache.put(k, new WeakReference (e)); 198 } 199 return e; 200 } 201 202 } 203 204 private static Cache malformedCache 205 = new Cache() { 206 public CoderResult create(int len) { 207 return new CoderResult (CR_MALFORMED, len); 208 }}; 209 210 216 public static CoderResult malformedForLength(int length) { 217 return malformedCache.get(length); 218 } 219 220 private static Cache unmappableCache 221 = new Cache() { 222 public CoderResult create(int len) { 223 return new CoderResult (CR_UNMAPPABLE, len); 224 }}; 225 226 232 public static CoderResult unmappableForLength(int length) { 233 return unmappableCache.get(length); 234 } 235 236 254 public void throwException() 255 throws CharacterCodingException 256 { 257 switch (type) { 258 case CR_UNDERFLOW: throw new BufferUnderflowException(); 259 case CR_OVERFLOW: throw new BufferOverflowException(); 260 case CR_MALFORMED: throw new MalformedInputException (length); 261 case CR_UNMAPPABLE: throw new UnmappableCharacterException (length); 262 default: 263 assert false; 264 } 265 } 266 267 } 268 | Popular Tags |