Exemple #1
0
        /*
         * This is a facade method for the encoding operation.
         * <p />
         * This method encodes the remaining character sequence of the given
         * character buffer into a new byte buffer. This method performs a complete
         * encoding operation, resets at first, then encodes, and flushes at last.
         * <p />
         * This method should not be invoked if another encode operation is ongoing.
         *
         * @param in
         *            the input buffer.
         * @return a new <code>ByteBuffer</code> containing the bytes produced by
         *         this encoding operation. The buffer's limit will be the position
         *         of the last byte in the buffer, and the position will be zero.
         * @throws IllegalStateException
         *             if another encoding operation is ongoing.
         * @throws MalformedInputException
         *             if an illegal input character sequence for this charset is
         *             encountered, and the action for malformed error is
         *             {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}
         * @throws UnmappableCharacterException
         *             if a legal but unmappable input character sequence for this
         *             charset is encountered, and the action for unmappable
         *             character error is
         *             {@link CodingErrorAction#REPORT CodingErrorAction.REPORT}.
         *             Unmappable means the Unicode character sequence at the input
         *             buffer's current position cannot be mapped to a equivalent
         *             byte sequence.
         * @throws CharacterCodingException
         *             if other exception happened during the encode operation.
         */
        public ByteBuffer encode(CharBuffer inJ) //throws CharacterCodingException
        {
            if (inJ.remaining() == 0)
            {
                return(ByteBuffer.allocate(0));
            }
            reset();
            int         length = (int)(inJ.remaining() * averBytes);
            ByteBuffer  output = ByteBuffer.allocate(length);
            CoderResult result = null;

            while (true)
            {
                result = encode(inJ, output, false);
                if (result == CoderResult.UNDERFLOW)
                {
                    break;
                }
                else if (result == CoderResult.OVERFLOW)
                {
                    output = allocateMore(output);
                    continue;
                }
                checkCoderResult(result);
            }
            result = encode(inJ, output, true);
            checkCoderResult(result);

            while (true)
            {
                result = flush(output);
                if (result == CoderResult.UNDERFLOW)
                {
                    output.flip();
                    break;
                }
                else if (result == CoderResult.OVERFLOW)
                {
                    output = allocateMore(output);
                    continue;
                }
                checkCoderResult(result);
                output.flip();
                if (result.isMalformed())
                {
                    throw new MalformedInputException(result.length());
                }
                else if (result.isUnmappable())
                {
                    throw new UnmappableCharacterException(result.length());
                }
                break;
            }
            status   = READY;
            finished = true;
            return(output);
        }
Exemple #2
0
 /*
  * checks the result whether it needs to throw CharacterCodingException.
  */
 private void checkCoderResult(CoderResult result)// throws CharacterCodingException
 {
     if (malformAction == CodingErrorAction.REPORT && result.isMalformed())
     {
         throw new MalformedInputException(result.length());
     }
     else if (unmapAction == CodingErrorAction.REPORT && result.isUnmappable())
     {
         throw new UnmappableCharacterException(result.length());
     }
 }
 //throws CharacterCodingException
 /*
  * checks the result whether it needs to throw CharacterCodingException.
  */
 private void checkCoderResult(CoderResult result)
 {
     if (result.isMalformed() && malformAction == CodingErrorAction.REPORT) {
         throw new MalformedInputException(result.length());
     } else if (result.isUnmappable()
             && unmapAction == CodingErrorAction.REPORT) {
         throw new UnmappableCharacterException(result.length());
     }
 }