rt/emul/compact/src/main/java/java/nio/charset/CharsetEncoder.java
changeset 1343 802e5d2da9f6
parent 1337 c794024954b5
     1.1 --- a/rt/emul/compact/src/main/java/java/nio/charset/CharsetEncoder.java	Thu Oct 03 17:36:44 2013 +0200
     1.2 +++ b/rt/emul/compact/src/main/java/java/nio/charset/CharsetEncoder.java	Fri Oct 04 15:02:17 2013 +0200
     1.3 @@ -27,13 +27,13 @@
     1.4  
     1.5  package java.nio.charset;
     1.6  
     1.7 -import java.nio.Buffer;
     1.8 -import java.nio.ByteBuffer;
     1.9 -import java.nio.CharBuffer;
    1.10 -import java.nio.BufferOverflowException;
    1.11 -import java.nio.BufferUnderflowException;
    1.12 +//import java.nio.Buffer;
    1.13 +//import java.nio.ByteBuffer;
    1.14 +//import java.nio.CharBuffer;
    1.15 +//import java.nio.BufferOverflowException;
    1.16 +//import java.nio.BufferUnderflowException;
    1.17  import java.lang.ref.WeakReference;
    1.18 -import java.nio.charset.CoderMalfunctionError;                  // javadoc
    1.19 +//import java.nio.charset.CoderMalfunctionError;                  // javadoc
    1.20  
    1.21  
    1.22  /**
    1.23 @@ -140,10 +140,10 @@
    1.24      private final float maxBytesPerChar;
    1.25  
    1.26      private byte[] replacement;
    1.27 -    private CodingErrorAction malformedInputAction
    1.28 -        = CodingErrorAction.REPORT;
    1.29 -    private CodingErrorAction unmappableCharacterAction
    1.30 -        = CodingErrorAction.REPORT;
    1.31 +//    private CodingErrorAction malformedInputAction
    1.32 +//        = CodingErrorAction.REPORT;
    1.33 +//    private CodingErrorAction unmappableCharacterAction
    1.34 +//        = CodingErrorAction.REPORT;
    1.35  
    1.36      // Internal states
    1.37      //
    1.38 @@ -191,12 +191,10 @@
    1.39          if (maxBytesPerChar <= 0.0f)
    1.40              throw new IllegalArgumentException("Non-positive "
    1.41                                                 + "maxBytesPerChar");
    1.42 -        if (!Charset.atBugLevel("1.4")) {
    1.43 -            if (averageBytesPerChar > maxBytesPerChar)
    1.44 -                throw new IllegalArgumentException("averageBytesPerChar"
    1.45 -                                                   + " exceeds "
    1.46 -                                                   + "maxBytesPerChar");
    1.47 -        }
    1.48 +        if (averageBytesPerChar > maxBytesPerChar)
    1.49 +            throw new IllegalArgumentException("averageBytesPerChar"
    1.50 +                                               + " exceeds "
    1.51 +                                               + "maxBytesPerChar");
    1.52          this.replacement = replacement;
    1.53          this.averageBytesPerChar = averageBytesPerChar;
    1.54          this.maxBytesPerChar = maxBytesPerChar;
    1.55 @@ -281,8 +279,8 @@
    1.56          if (len > maxBytesPerChar)
    1.57              throw new IllegalArgumentException("Replacement too long");
    1.58  
    1.59 -        if (!isLegalReplacement(newReplacement))
    1.60 -            throw new IllegalArgumentException("Illegal replacement");
    1.61 +//        if (!isLegalReplacement(newReplacement))
    1.62 +//            throw new IllegalArgumentException("Illegal replacement");
    1.63  
    1.64          this.replacement = newReplacement;
    1.65          implReplaceWith(newReplacement);
    1.66 @@ -321,23 +319,23 @@
    1.67       * @return  <tt>true</tt> if, and only if, the given byte array
    1.68       *          is a legal replacement value for this encoder
    1.69       */
    1.70 -    public boolean isLegalReplacement(byte[] repl) {
    1.71 -        WeakReference<CharsetDecoder> wr = cachedDecoder;
    1.72 -        CharsetDecoder dec = null;
    1.73 -        if ((wr == null) || ((dec = wr.get()) == null)) {
    1.74 -            dec = charset().newDecoder();
    1.75 -            dec.onMalformedInput(CodingErrorAction.REPORT);
    1.76 -            dec.onUnmappableCharacter(CodingErrorAction.REPORT);
    1.77 -            cachedDecoder = new WeakReference<CharsetDecoder>(dec);
    1.78 -        } else {
    1.79 -            dec.reset();
    1.80 -        }
    1.81 -        ByteBuffer bb = ByteBuffer.wrap(repl);
    1.82 -        CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()
    1.83 -                                                  * dec.maxCharsPerByte()));
    1.84 -        CoderResult cr = dec.decode(bb, cb, true);
    1.85 -        return !cr.isError();
    1.86 -    }
    1.87 +//    public boolean isLegalReplacement(byte[] repl) {
    1.88 +//        WeakReference<CharsetDecoder> wr = cachedDecoder;
    1.89 +//        CharsetDecoder dec = null;
    1.90 +//        if ((wr == null) || ((dec = wr.get()) == null)) {
    1.91 +//            dec = charset().newDecoder();
    1.92 +//            dec.onMalformedInput(CodingErrorAction.REPORT);
    1.93 +//            dec.onUnmappableCharacter(CodingErrorAction.REPORT);
    1.94 +//            cachedDecoder = new WeakReference<CharsetDecoder>(dec);
    1.95 +//        } else {
    1.96 +//            dec.reset();
    1.97 +//        }
    1.98 +//        ByteBuffer bb = ByteBuffer.wrap(repl);
    1.99 +//        CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()
   1.100 +//                                                  * dec.maxCharsPerByte()));
   1.101 +//        CoderResult cr = dec.decode(bb, cb, true);
   1.102 +//        return !cr.isError();
   1.103 +//    }
   1.104  
   1.105  
   1.106  
   1.107 @@ -346,9 +344,9 @@
   1.108       *
   1.109       * @return The current malformed-input action, which is never <tt>null</tt>
   1.110       */
   1.111 -    public CodingErrorAction malformedInputAction() {
   1.112 -        return malformedInputAction;
   1.113 -    }
   1.114 +//    public CodingErrorAction malformedInputAction() {
   1.115 +//        return malformedInputAction;
   1.116 +//    }
   1.117  
   1.118      /**
   1.119       * Changes this encoder's action for malformed-input errors.  </p>
   1.120 @@ -363,13 +361,13 @@
   1.121       * @throws IllegalArgumentException
   1.122       *         If the precondition on the parameter does not hold
   1.123       */
   1.124 -    public final CharsetEncoder onMalformedInput(CodingErrorAction newAction) {
   1.125 -        if (newAction == null)
   1.126 -            throw new IllegalArgumentException("Null action");
   1.127 -        malformedInputAction = newAction;
   1.128 -        implOnMalformedInput(newAction);
   1.129 -        return this;
   1.130 -    }
   1.131 +//    public final CharsetEncoder onMalformedInput(CodingErrorAction newAction) {
   1.132 +//        if (newAction == null)
   1.133 +//            throw new IllegalArgumentException("Null action");
   1.134 +//        malformedInputAction = newAction;
   1.135 +//        implOnMalformedInput(newAction);
   1.136 +//        return this;
   1.137 +//    }
   1.138  
   1.139      /**
   1.140       * Reports a change to this encoder's malformed-input action.
   1.141 @@ -378,7 +376,7 @@
   1.142       * should be overridden by encoders that require notification of changes to
   1.143       * the malformed-input action.  </p>
   1.144       */
   1.145 -    protected void implOnMalformedInput(CodingErrorAction newAction) { }
   1.146 +//    protected void implOnMalformedInput(CodingErrorAction newAction) { }
   1.147  
   1.148      /**
   1.149       * Returns this encoder's current action for unmappable-character errors.
   1.150 @@ -387,9 +385,9 @@
   1.151       * @return The current unmappable-character action, which is never
   1.152       *         <tt>null</tt>
   1.153       */
   1.154 -    public CodingErrorAction unmappableCharacterAction() {
   1.155 -        return unmappableCharacterAction;
   1.156 -    }
   1.157 +//    public CodingErrorAction unmappableCharacterAction() {
   1.158 +//        return unmappableCharacterAction;
   1.159 +//    }
   1.160  
   1.161      /**
   1.162       * Changes this encoder's action for unmappable-character errors.
   1.163 @@ -404,15 +402,15 @@
   1.164       * @throws IllegalArgumentException
   1.165       *         If the precondition on the parameter does not hold
   1.166       */
   1.167 -    public final CharsetEncoder onUnmappableCharacter(CodingErrorAction
   1.168 -                                                      newAction)
   1.169 -    {
   1.170 -        if (newAction == null)
   1.171 -            throw new IllegalArgumentException("Null action");
   1.172 -        unmappableCharacterAction = newAction;
   1.173 -        implOnUnmappableCharacter(newAction);
   1.174 -        return this;
   1.175 -    }
   1.176 +//    public final CharsetEncoder onUnmappableCharacter(CodingErrorAction
   1.177 +//                                                      newAction)
   1.178 +//    {
   1.179 +//        if (newAction == null)
   1.180 +//            throw new IllegalArgumentException("Null action");
   1.181 +//        unmappableCharacterAction = newAction;
   1.182 +//        implOnUnmappableCharacter(newAction);
   1.183 +//        return this;
   1.184 +//    }
   1.185  
   1.186      /**
   1.187       * Reports a change to this encoder's unmappable-character action.
   1.188 @@ -421,7 +419,7 @@
   1.189       * should be overridden by encoders that require notification of changes to
   1.190       * the unmappable-character action.  </p>
   1.191       */
   1.192 -    protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
   1.193 +//    protected void implOnUnmappableCharacter(CodingErrorAction newAction) { }
   1.194  
   1.195      /**
   1.196       * Returns the average number of bytes that will be produced for each
   1.197 @@ -545,66 +543,66 @@
   1.198       *          If an invocation of the encodeLoop method threw
   1.199       *          an unexpected exception
   1.200       */
   1.201 -    public final CoderResult encode(CharBuffer in, ByteBuffer out,
   1.202 -                                    boolean endOfInput)
   1.203 -    {
   1.204 -        int newState = endOfInput ? ST_END : ST_CODING;
   1.205 -        if ((state != ST_RESET) && (state != ST_CODING)
   1.206 -            && !(endOfInput && (state == ST_END)))
   1.207 -            throwIllegalStateException(state, newState);
   1.208 -        state = newState;
   1.209 -
   1.210 -        for (;;) {
   1.211 -
   1.212 -            CoderResult cr;
   1.213 -            try {
   1.214 -                cr = encodeLoop(in, out);
   1.215 -            } catch (BufferUnderflowException x) {
   1.216 -                throw new CoderMalfunctionError(x);
   1.217 -            } catch (BufferOverflowException x) {
   1.218 -                throw new CoderMalfunctionError(x);
   1.219 -            }
   1.220 -
   1.221 -            if (cr.isOverflow())
   1.222 -                return cr;
   1.223 -
   1.224 -            if (cr.isUnderflow()) {
   1.225 -                if (endOfInput && in.hasRemaining()) {
   1.226 -                    cr = CoderResult.malformedForLength(in.remaining());
   1.227 -                    // Fall through to malformed-input case
   1.228 -                } else {
   1.229 -                    return cr;
   1.230 -                }
   1.231 -            }
   1.232 -
   1.233 -            CodingErrorAction action = null;
   1.234 -            if (cr.isMalformed())
   1.235 -                action = malformedInputAction;
   1.236 -            else if (cr.isUnmappable())
   1.237 -                action = unmappableCharacterAction;
   1.238 -            else
   1.239 -                assert false : cr.toString();
   1.240 -
   1.241 -            if (action == CodingErrorAction.REPORT)
   1.242 -                return cr;
   1.243 -
   1.244 -            if (action == CodingErrorAction.REPLACE) {
   1.245 -                if (out.remaining() < replacement.length)
   1.246 -                    return CoderResult.OVERFLOW;
   1.247 -                out.put(replacement);
   1.248 -            }
   1.249 -
   1.250 -            if ((action == CodingErrorAction.IGNORE)
   1.251 -                || (action == CodingErrorAction.REPLACE)) {
   1.252 -                // Skip erroneous input either way
   1.253 -                in.position(in.position() + cr.length());
   1.254 -                continue;
   1.255 -            }
   1.256 -
   1.257 -            assert false;
   1.258 -        }
   1.259 -
   1.260 -    }
   1.261 +//    public final CoderResult encode(CharBuffer in, ByteBuffer out,
   1.262 +//                                    boolean endOfInput)
   1.263 +//    {
   1.264 +//        int newState = endOfInput ? ST_END : ST_CODING;
   1.265 +//        if ((state != ST_RESET) && (state != ST_CODING)
   1.266 +//            && !(endOfInput && (state == ST_END)))
   1.267 +//            throwIllegalStateException(state, newState);
   1.268 +//        state = newState;
   1.269 +//
   1.270 +//        for (;;) {
   1.271 +//
   1.272 +//            CoderResult cr;
   1.273 +//            try {
   1.274 +//                cr = encodeLoop(in, out);
   1.275 +//            } catch (BufferUnderflowException x) {
   1.276 +//                throw new CoderMalfunctionError(x);
   1.277 +//            } catch (BufferOverflowException x) {
   1.278 +//                throw new CoderMalfunctionError(x);
   1.279 +//            }
   1.280 +//
   1.281 +//            if (cr.isOverflow())
   1.282 +//                return cr;
   1.283 +//
   1.284 +//            if (cr.isUnderflow()) {
   1.285 +//                if (endOfInput && in.hasRemaining()) {
   1.286 +//                    cr = CoderResult.malformedForLength(in.remaining());
   1.287 +//                    // Fall through to malformed-input case
   1.288 +//                } else {
   1.289 +//                    return cr;
   1.290 +//                }
   1.291 +//            }
   1.292 +//
   1.293 +//            CodingErrorAction action = null;
   1.294 +//            if (cr.isMalformed())
   1.295 +//                action = malformedInputAction;
   1.296 +//            else if (cr.isUnmappable())
   1.297 +//                action = unmappableCharacterAction;
   1.298 +//            else
   1.299 +//                assert false : cr.toString();
   1.300 +//
   1.301 +//            if (action == CodingErrorAction.REPORT)
   1.302 +//                return cr;
   1.303 +//
   1.304 +//            if (action == CodingErrorAction.REPLACE) {
   1.305 +//                if (out.remaining() < replacement.length)
   1.306 +//                    return CoderResult.OVERFLOW;
   1.307 +//                out.put(replacement);
   1.308 +//            }
   1.309 +//
   1.310 +//            if ((action == CodingErrorAction.IGNORE)
   1.311 +//                || (action == CodingErrorAction.REPLACE)) {
   1.312 +//                // Skip erroneous input either way
   1.313 +//                in.position(in.position() + cr.length());
   1.314 +//                continue;
   1.315 +//            }
   1.316 +//
   1.317 +//            assert false;
   1.318 +//        }
   1.319 +//
   1.320 +//    }
   1.321  
   1.322      /**
   1.323       * Flushes this encoder.
   1.324 @@ -645,19 +643,19 @@
   1.325       *          with a value of <tt>true</tt> for the <tt>endOfInput</tt>
   1.326       *          parameter
   1.327       */
   1.328 -    public final CoderResult flush(ByteBuffer out) {
   1.329 -        if (state == ST_END) {
   1.330 -            CoderResult cr = implFlush(out);
   1.331 -            if (cr.isUnderflow())
   1.332 -                state = ST_FLUSHED;
   1.333 -            return cr;
   1.334 -        }
   1.335 -
   1.336 -        if (state != ST_FLUSHED)
   1.337 -            throwIllegalStateException(state, ST_FLUSHED);
   1.338 -
   1.339 -        return CoderResult.UNDERFLOW; // Already flushed
   1.340 -    }
   1.341 +//    public final CoderResult flush(ByteBuffer out) {
   1.342 +//        if (state == ST_END) {
   1.343 +//            CoderResult cr = implFlush(out);
   1.344 +//            if (cr.isUnderflow())
   1.345 +//                state = ST_FLUSHED;
   1.346 +//            return cr;
   1.347 +//        }
   1.348 +//
   1.349 +//        if (state != ST_FLUSHED)
   1.350 +//            throwIllegalStateException(state, ST_FLUSHED);
   1.351 +//
   1.352 +//        return CoderResult.UNDERFLOW; // Already flushed
   1.353 +//    }
   1.354  
   1.355      /**
   1.356       * Flushes this encoder.
   1.357 @@ -673,9 +671,9 @@
   1.358       * @return  A coder-result object, either {@link CoderResult#UNDERFLOW} or
   1.359       *          {@link CoderResult#OVERFLOW}
   1.360       */
   1.361 -    protected CoderResult implFlush(ByteBuffer out) {
   1.362 -        return CoderResult.UNDERFLOW;
   1.363 -    }
   1.364 +//    protected CoderResult implFlush(ByteBuffer out) {
   1.365 +//        return CoderResult.UNDERFLOW;
   1.366 +//    }
   1.367  
   1.368      /**
   1.369       * Resets this encoder, clearing any internal state.
   1.370 @@ -736,8 +734,8 @@
   1.371       *
   1.372       * @return  A coder-result object describing the reason for termination
   1.373       */
   1.374 -    protected abstract CoderResult encodeLoop(CharBuffer in,
   1.375 -                                              ByteBuffer out);
   1.376 +//    protected abstract CoderResult encodeLoop(CharBuffer in,
   1.377 +//                                              ByteBuffer out);
   1.378  
   1.379      /**
   1.380       * Convenience method that encodes the remaining content of a single input
   1.381 @@ -770,36 +768,38 @@
   1.382       *          the current unmappable-character action is {@link
   1.383       *          CodingErrorAction#REPORT}
   1.384       */
   1.385 -    public final ByteBuffer encode(CharBuffer in)
   1.386 -        throws CharacterCodingException
   1.387 -    {
   1.388 -        int n = (int)(in.remaining() * averageBytesPerChar());
   1.389 -        ByteBuffer out = ByteBuffer.allocate(n);
   1.390 +//    public final ByteBuffer encode(CharBuffer in)
   1.391 +//        throws CharacterCodingException
   1.392 +//    {
   1.393 +//        int n = (int)(in.remaining() * averageBytesPerChar());
   1.394 +//        ByteBuffer out = ByteBuffer.allocate(n);
   1.395 +//
   1.396 +//        if ((n == 0) && (in.remaining() == 0))
   1.397 +//            return out;
   1.398 +//        reset();
   1.399 +//        for (;;) {
   1.400 +//            CoderResult cr = in.hasRemaining() ?
   1.401 +//                encode(in, out, true) : CoderResult.UNDERFLOW;
   1.402 +//            if (cr.isUnderflow())
   1.403 +//                cr = flush(out);
   1.404 +//
   1.405 +//            if (cr.isUnderflow())
   1.406 +//                break;
   1.407 +//            if (cr.isOverflow()) {
   1.408 +//                n = 2*n + 1;    // Ensure progress; n might be 0!
   1.409 +//                ByteBuffer o = ByteBuffer.allocate(n);
   1.410 +//                out.flip();
   1.411 +//                o.put(out);
   1.412 +//                out = o;
   1.413 +//                continue;
   1.414 +//            }
   1.415 +//            cr.throwException();
   1.416 +//        }
   1.417 +//        out.flip();
   1.418 +//        return out;
   1.419 +//    }
   1.420  
   1.421 -        if ((n == 0) && (in.remaining() == 0))
   1.422 -            return out;
   1.423 -        reset();
   1.424 -        for (;;) {
   1.425 -            CoderResult cr = in.hasRemaining() ?
   1.426 -                encode(in, out, true) : CoderResult.UNDERFLOW;
   1.427 -            if (cr.isUnderflow())
   1.428 -                cr = flush(out);
   1.429  
   1.430 -            if (cr.isUnderflow())
   1.431 -                break;
   1.432 -            if (cr.isOverflow()) {
   1.433 -                n = 2*n + 1;    // Ensure progress; n might be 0!
   1.434 -                ByteBuffer o = ByteBuffer.allocate(n);
   1.435 -                out.flip();
   1.436 -                o.put(out);
   1.437 -                out = o;
   1.438 -                continue;
   1.439 -            }
   1.440 -            cr.throwException();
   1.441 -        }
   1.442 -        out.flip();
   1.443 -        return out;
   1.444 -    }
   1.445  
   1.446  
   1.447  
   1.448 @@ -877,28 +877,26 @@
   1.449  
   1.450  
   1.451  
   1.452 -
   1.453 -
   1.454 -    private boolean canEncode(CharBuffer cb) {
   1.455 -        if (state == ST_FLUSHED)
   1.456 -            reset();
   1.457 -        else if (state != ST_RESET)
   1.458 -            throwIllegalStateException(state, ST_CODING);
   1.459 -        CodingErrorAction ma = malformedInputAction();
   1.460 -        CodingErrorAction ua = unmappableCharacterAction();
   1.461 -        try {
   1.462 -            onMalformedInput(CodingErrorAction.REPORT);
   1.463 -            onUnmappableCharacter(CodingErrorAction.REPORT);
   1.464 -            encode(cb);
   1.465 -        } catch (CharacterCodingException x) {
   1.466 -            return false;
   1.467 -        } finally {
   1.468 -            onMalformedInput(ma);
   1.469 -            onUnmappableCharacter(ua);
   1.470 -            reset();
   1.471 -        }
   1.472 -        return true;
   1.473 -    }
   1.474 +//    private boolean canEncode(CharBuffer cb) {
   1.475 +//        if (state == ST_FLUSHED)
   1.476 +//            reset();
   1.477 +//        else if (state != ST_RESET)
   1.478 +//            throwIllegalStateException(state, ST_CODING);
   1.479 +//        CodingErrorAction ma = malformedInputAction();
   1.480 +//        CodingErrorAction ua = unmappableCharacterAction();
   1.481 +//        try {
   1.482 +//            onMalformedInput(CodingErrorAction.REPORT);
   1.483 +//            onUnmappableCharacter(CodingErrorAction.REPORT);
   1.484 +//            encode(cb);
   1.485 +//        } catch (CharacterCodingException x) {
   1.486 +//            return false;
   1.487 +//        } finally {
   1.488 +//            onMalformedInput(ma);
   1.489 +//            onUnmappableCharacter(ua);
   1.490 +//            reset();
   1.491 +//        }
   1.492 +//        return true;
   1.493 +//    }
   1.494  
   1.495      /**
   1.496       * Tells whether or not this encoder can encode the given character.
   1.497 @@ -923,12 +921,12 @@
   1.498       * @throws  IllegalStateException
   1.499       *          If an encoding operation is already in progress
   1.500       */
   1.501 -    public boolean canEncode(char c) {
   1.502 -        CharBuffer cb = CharBuffer.allocate(1);
   1.503 -        cb.put(c);
   1.504 -        cb.flip();
   1.505 -        return canEncode(cb);
   1.506 -    }
   1.507 +//    public boolean canEncode(char c) {
   1.508 +//        CharBuffer cb = CharBuffer.allocate(1);
   1.509 +//        cb.put(c);
   1.510 +//        cb.flip();
   1.511 +//        return canEncode(cb);
   1.512 +//    }
   1.513  
   1.514      /**
   1.515       * Tells whether or not this encoder can encode the given character
   1.516 @@ -952,14 +950,14 @@
   1.517       * @throws  IllegalStateException
   1.518       *          If an encoding operation is already in progress
   1.519       */
   1.520 -    public boolean canEncode(CharSequence cs) {
   1.521 -        CharBuffer cb;
   1.522 -        if (cs instanceof CharBuffer)
   1.523 -            cb = ((CharBuffer)cs).duplicate();
   1.524 -        else
   1.525 -            cb = CharBuffer.wrap(cs.toString());
   1.526 -        return canEncode(cb);
   1.527 -    }
   1.528 +//    public boolean canEncode(CharSequence cs) {
   1.529 +//        CharBuffer cb;
   1.530 +//        if (cs instanceof CharBuffer)
   1.531 +//            cb = ((CharBuffer)cs).duplicate();
   1.532 +//        else
   1.533 +//            cb = CharBuffer.wrap(cs.toString());
   1.534 +//        return canEncode(cb);
   1.535 +//    }
   1.536  
   1.537  
   1.538