src/java.base/share/classes/java/nio/charset/CoderResult.java
Print this page
*** 23,34 ****
* questions.
*/
package java.nio.charset;
! import java.lang.ref.WeakReference;
! import java.nio.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
/**
* A description of the result state of a coder.
--- 23,34 ----
* questions.
*/
package java.nio.charset;
! import java.nio.BufferOverflowException;
! import java.nio.BufferUnderflowException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
/**
* A description of the result state of a coder.
*** 188,225 ****
* room in the output buffer.
*/
public static final CoderResult OVERFLOW
= new CoderResult(CR_OVERFLOW, 0);
! private abstract static class Cache {
! private Map<Integer,WeakReference<CoderResult>> cache = null;
!
! protected abstract CoderResult create(int len);
!
! private CoderResult get(int len) {
! Integer k = len;
! WeakReference<CoderResult> w;
! CoderResult e = null;
! if (cache == null) {
! cache = new ConcurrentHashMap<>();
! } else if ((w = cache.get(k)) != null) {
! e = w.get();
! }
! if (e == null) {
! e = create(len);
! cache.put(k, new WeakReference<>(e));
! }
! return e;
}
- }
-
- private static final Cache malformedCache
- = new Cache() {
- public CoderResult create(int len) {
- return new CoderResult(CR_MALFORMED, len);
- }};
private static final CoderResult[] malformed4 = new CoderResult[] {
new CoderResult(CR_MALFORMED, 1),
new CoderResult(CR_MALFORMED, 2),
new CoderResult(CR_MALFORMED, 3),
--- 188,204 ----
* room in the output buffer.
*/
public static final CoderResult OVERFLOW
= new CoderResult(CR_OVERFLOW, 0);
! private static final class Cache {
! static final Cache INSTANCE = new Cache();
! private Cache() {}
! final Map<Integer, CoderResult> unmappable = new ConcurrentHashMap<>();
! final Map<Integer, CoderResult> malformed = new ConcurrentHashMap<>();
}
private static final CoderResult[] malformed4 = new CoderResult[] {
new CoderResult(CR_MALFORMED, 1),
new CoderResult(CR_MALFORMED, 2),
new CoderResult(CR_MALFORMED, 3),
*** 238,256 ****
public static CoderResult malformedForLength(int length) {
if (length <= 0)
throw new IllegalArgumentException("Non-positive length");
if (length <= 4)
return malformed4[length - 1];
! return malformedCache.get(length);
}
- private static final Cache unmappableCache
- = new Cache() {
- public CoderResult create(int len) {
- return new CoderResult(CR_UNMAPPABLE, len);
- }};
-
private static final CoderResult[] unmappable4 = new CoderResult[] {
new CoderResult(CR_UNMAPPABLE, 1),
new CoderResult(CR_UNMAPPABLE, 2),
new CoderResult(CR_UNMAPPABLE, 3),
new CoderResult(CR_UNMAPPABLE, 4),
--- 217,230 ----
public static CoderResult malformedForLength(int length) {
if (length <= 0)
throw new IllegalArgumentException("Non-positive length");
if (length <= 4)
return malformed4[length - 1];
! return Cache.INSTANCE.malformed.computeIfAbsent(length,
! n -> new CoderResult(CR_MALFORMED, n));
}
private static final CoderResult[] unmappable4 = new CoderResult[] {
new CoderResult(CR_UNMAPPABLE, 1),
new CoderResult(CR_UNMAPPABLE, 2),
new CoderResult(CR_UNMAPPABLE, 3),
new CoderResult(CR_UNMAPPABLE, 4),
*** 268,278 ****
public static CoderResult unmappableForLength(int length) {
if (length <= 0)
throw new IllegalArgumentException("Non-positive length");
if (length <= 4)
return unmappable4[length - 1];
! return unmappableCache.get(length);
}
/**
* Throws an exception appropriate to the result described by this object.
*
--- 242,253 ----
public static CoderResult unmappableForLength(int length) {
if (length <= 0)
throw new IllegalArgumentException("Non-positive length");
if (length <= 4)
return unmappable4[length - 1];
! return Cache.INSTANCE.unmappable.computeIfAbsent(length,
! n -> new CoderResult(CR_UNMAPPABLE, n));
}
/**
* Throws an exception appropriate to the result described by this object.
*