diff options
| author | Michael Koch <konqueror@gmx.de> | 2002-11-08 12:15:32 +0000 |
|---|---|---|
| committer | Michael Koch <konqueror@gmx.de> | 2002-11-08 12:15:32 +0000 |
| commit | 03e7fa338ecf4ad04592db8ac84e175646416213 (patch) | |
| tree | 672c51dbc3e6331a6ee29c1dedb7031495f3436b /java/nio/charset/CoderResult.java | |
| parent | 607c7096d2fe8bef1455b837ccb8d4ed7ae2e083 (diff) | |
| download | classpath-03e7fa338ecf4ad04592db8ac84e175646416213.tar.gz | |
2002-11-08 Jesse Rosenstock <jmr@fulcrummicro.com>
* java/nio/charset/CharacterCodingException.java:
This class must be public.
* java/nio/charset/Charset.java:
Implemented whole class.
* java/nio/charset/CharsetDecoder.java:
Implemented whole class.
* java/nio/charset/CharsetEncoder.java:
Implemented whole class.
* java/nio/charset/CoderMalfunctionError.java:
This class must be public.
* java/nio/charset/CoderResult.java:
Implemented whole class.
* java/nio/charset/CodingErrorAction.java:
This class must be public.
* java/nio/charset/IllegalCharsetNameException.java:
This class must be public, better implementation.
* java/nio/charset/MalformedInputException.java:
This class must be public, better implementation.
* java/nio/charset/UnmappableCharacterException.java:
This class must be public, better implementation.
* java/nio/charset/UnsupportedCharsetException.java:
This class must be public, better implementation.
* gnu/java/nio/charset/ISO_8859_1.java,
gnu/java/nio/charset/Provider.java,
gnu/java/nio/charset/US_ASCII.java,
gnu/java/nio/charset/UTF_16.java,
gnu/java/nio/charset/UTF_16BE.java,
gnu/java/nio/charset/UTF_16Decoder.java,
gnu/java/nio/charset/UTF_16Encoder.java,
gnu/java/nio/charset/UTF_16LE.java,
gnu/java/nio/charset/UTF_8.java,
gnu/java/nio/charset/Makefile.am,
gnu/java/nio/charset/.cvsignore:
New files.
* gnu/java/nio/Makefile.am: Add new subdir charset.
* configure.in: Added gnu/java/nio/charset/Makefile to AC_OUTPUT.
Diffstat (limited to 'java/nio/charset/CoderResult.java')
| -rw-r--r-- | java/nio/charset/CoderResult.java | 191 |
1 files changed, 144 insertions, 47 deletions
diff --git a/java/nio/charset/CoderResult.java b/java/nio/charset/CoderResult.java index 6c2a971fd..b78d7724c 100644 --- a/java/nio/charset/CoderResult.java +++ b/java/nio/charset/CoderResult.java @@ -37,59 +37,156 @@ exception statement from your version. */ package java.nio.charset; -class CoderResult +import java.nio.BufferOverflowException; +import java.nio.BufferUnderflowException; +import java.util.HashMap; + +/** + * @author Jesse Rosenstock + * @since 1.4 + */ +public class CoderResult { - boolean err; - - boolean isError() - { - return err; - } - - boolean isMalformed() - { - return false; - } - - boolean isOverflow() - { - return false; - } - - boolean isUnderflow() - { - return false; - } - - boolean isUnmappable() - { - return false; - } - - int length() - { - return 0; - } - - static CoderResult malformedForLength(int length) - { - return null; - } + public static final CoderResult OVERFLOW + = new CoderResult (TYPE_OVERFLOW, 0); + public static final CoderResult UNDERFLOW + = new CoderResult (TYPE_UNDERFLOW, 0); + + private static final int TYPE_MALFORMED = 0; + private static final int TYPE_OVERFLOW = 1; + private static final int TYPE_UNDERFLOW = 2; + private static final int TYPE_UNMAPPABLE = 3; + + private static final String[] names + = { "MALFORMED", "OVERFLOW", "UNDERFLOW", "UNMAPPABLE" }; + + private static final Cache malformedCache + = new Cache () + { + protected CoderResult make (int length) + { + return new CoderResult (TYPE_MALFORMED, length); + } + }; + + private static final Cache unmappableCache + = new Cache () + { + protected CoderResult make (int length) + { + return new CoderResult (TYPE_UNMAPPABLE, length); + } + }; + + private final int type; + private final int length; + + private CoderResult (int type, int length) + { + this.type = type; + this.length = length; + } + + public boolean isError () + { + return length > 0; + } + + public boolean isMalformed () + { + return type == TYPE_MALFORMED; + } + + public boolean isOverflow () + { + return type == TYPE_OVERFLOW; + } + + public boolean isUnderflow () + { + return type == TYPE_UNDERFLOW; + } + + public boolean isUnmappable () + { + return type == TYPE_UNMAPPABLE; + } + + public int length () + { + if (length <= 0) + throw new UnsupportedOperationException (); + else + return length; + } + + public static CoderResult malformedForLength (int length) + { + return malformedCache.get (length); + } - void throwException() - throws CharacterCodingException + public void throwException () + throws CharacterCodingException + { + switch (type) + { + case TYPE_MALFORMED: + throw new MalformedInputException (length); + case TYPE_OVERFLOW: + throw new BufferOverflowException (); + case TYPE_UNDERFLOW: + throw new BufferUnderflowException (); + case TYPE_UNMAPPABLE: + throw new UnmappableCharacterException (length); + } + } + + public String toString () + { + String name = names[type]; + return (length > 0) ? name + '[' + length + ']' : name; + } + + public static CoderResult unmappableForLength (int length) + { + return unmappableCache.get (length); + } + + private abstract static class Cache + { + private final HashMap cache; + + private Cache () { - throw new CharacterCodingException(); + // If we didn't synchronize on this, then cache would be initialized + // without holding a lock. Undefined behavior would occur if the + // first thread to call get(int) was not the same as the one that + // called the constructor. + synchronized (this) + { + cache = new HashMap (); + } } - public String toString() + private synchronized CoderResult get (int length) { - return "coder error"; + if (length <= 0) + throw new IllegalArgumentException ("Non-positive length"); + + Integer len = new Integer (length); + CoderResult cr = null; + Object o; + if ((o = cache.get (len)) != null) + cr = (CoderResult) ((WeakReference) o).get (); + if (cr == null) + { + cr = make (length); + cache.put (len, cr); + } + + return cr; } - static CoderResult unmappableForLength(int length) - { - return null; - } - + protected abstract CoderResult make (int length); + } } |
