< prev index next >

src/java.base/share/classes/java/nio/charset/CoderResult.java

Print this page




 187     /**
 188      * Result object indicating overflow, meaning that there is insufficient
 189      * room in the output buffer.
 190      */
 191     public static final CoderResult OVERFLOW
 192         = new CoderResult(CR_OVERFLOW, 0);
 193 
 194     private static abstract class Cache {
 195 
 196         private Map<Integer,WeakReference<CoderResult>> cache = null;
 197 
 198         protected abstract CoderResult create(int len);
 199 
 200         private synchronized CoderResult get(int len) {
 201             if (len <= 0)
 202                 throw new IllegalArgumentException("Non-positive length");
 203             Integer k = len;
 204             WeakReference<CoderResult> w;
 205             CoderResult e = null;
 206             if (cache == null) {
 207                 cache = new HashMap<Integer,WeakReference<CoderResult>>();
 208             } else if ((w = cache.get(k)) != null) {
 209                 e = w.get();
 210             }
 211             if (e == null) {
 212                 e = create(len);
 213                 cache.put(k, new WeakReference<CoderResult>(e));
 214             }
 215             return e;
 216         }
 217 
 218     }
 219 
 220     private static Cache malformedCache
 221         = new Cache() {
 222                 public CoderResult create(int len) {
 223                     return new CoderResult(CR_MALFORMED, len);
 224                 }};
 225 
 226     /**
 227      * Static factory method that returns the unique object describing a
 228      * malformed-input error of the given length.
 229      *
 230      * @param   length
 231      *          The given length
 232      *
 233      * @return  The requested coder-result object




 187     /**
 188      * Result object indicating overflow, meaning that there is insufficient
 189      * room in the output buffer.
 190      */
 191     public static final CoderResult OVERFLOW
 192         = new CoderResult(CR_OVERFLOW, 0);
 193 
 194     private static abstract class Cache {
 195 
 196         private Map<Integer,WeakReference<CoderResult>> cache = null;
 197 
 198         protected abstract CoderResult create(int len);
 199 
 200         private synchronized CoderResult get(int len) {
 201             if (len <= 0)
 202                 throw new IllegalArgumentException("Non-positive length");
 203             Integer k = len;
 204             WeakReference<CoderResult> w;
 205             CoderResult e = null;
 206             if (cache == null) {
 207                 cache = new HashMap<>();
 208             } else if ((w = cache.get(k)) != null) {
 209                 e = w.get();
 210             }
 211             if (e == null) {
 212                 e = create(len);
 213                 cache.put(k, new WeakReference<>(e));
 214             }
 215             return e;
 216         }
 217 
 218     }
 219 
 220     private static Cache malformedCache
 221         = new Cache() {
 222                 public CoderResult create(int len) {
 223                     return new CoderResult(CR_MALFORMED, len);
 224                 }};
 225 
 226     /**
 227      * Static factory method that returns the unique object describing a
 228      * malformed-input error of the given length.
 229      *
 230      * @param   length
 231      *          The given length
 232      *
 233      * @return  The requested coder-result object


< prev index next >