示例#1
0
 /// <exception cref="java.nio.charset.CharacterCodingException"></exception>
 private void checkCoderResult(java.nio.charset.CoderResult result)
 {
     if (result.isMalformed() && _malformedInputAction == java.nio.charset.CodingErrorAction
         .REPORT)
     {
         throw new java.nio.charset.MalformedInputException(result.length());
     }
     else
     {
         if (result.isUnmappable() && _unmappableCharacterAction == java.nio.charset.CodingErrorAction
             .REPORT)
         {
             throw new java.nio.charset.UnmappableCharacterException(result.length());
         }
     }
 }
示例#2
0
        /// <summary>This is a facade method for the encoding operation.</summary>
        /// <remarks>
        /// 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.
        /// </remarks>
        /// <param name="in">the input buffer.</param>
        /// <returns>
        /// 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.
        /// </returns>
        /// <exception cref="System.InvalidOperationException">if another encoding operation is ongoing.
        ///     </exception>
        /// <exception cref="MalformedInputException">
        /// if an illegal input character sequence for this charset is
        /// encountered, and the action for malformed error is
        /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see>
        /// </exception>
        /// <exception cref="UnmappableCharacterException">
        /// if a legal but unmappable input character sequence for this
        /// charset is encountered, and the action for unmappable
        /// character error is
        /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see>
        /// .
        /// Unmappable means the Unicode character sequence at the input
        /// buffer's current position cannot be mapped to a equivalent
        /// byte sequence.
        /// </exception>
        /// <exception cref="CharacterCodingException">if other exception happened during the encode operation.
        ///     </exception>
        /// <exception cref="java.nio.charset.CharacterCodingException"></exception>
        public java.nio.ByteBuffer encode(java.nio.CharBuffer @in)
        {
            if (@in.remaining() == 0)
            {
                return(java.nio.ByteBuffer.allocate(0));
            }
            reset();
            int length = (int)(@in.remaining() * _averageBytesPerChar);

            java.nio.ByteBuffer          output = java.nio.ByteBuffer.allocate(length);
            java.nio.charset.CoderResult result = null;
            while (true)
            {
                result = encode(@in, output, false);
                if (result == java.nio.charset.CoderResult.UNDERFLOW)
                {
                    break;
                }
                else
                {
                    if (result == java.nio.charset.CoderResult.OVERFLOW)
                    {
                        output = allocateMore(output);
                        continue;
                    }
                }
                checkCoderResult(result);
            }
            result = encode(@in, output, true);
            checkCoderResult(result);
            while (true)
            {
                result = flush(output);
                if (result == java.nio.charset.CoderResult.UNDERFLOW)
                {
                    output.flip();
                    break;
                }
                else
                {
                    if (result == java.nio.charset.CoderResult.OVERFLOW)
                    {
                        output = allocateMore(output);
                        continue;
                    }
                }
                checkCoderResult(result);
                output.flip();
                if (result.isMalformed())
                {
                    throw new java.nio.charset.MalformedInputException(result.length());
                }
                else
                {
                    if (result.isUnmappable())
                    {
                        throw new java.nio.charset.UnmappableCharacterException(result.length());
                    }
                }
                break;
            }
            status   = READY;
            finished = true;
            return(output);
        }
示例#3
0
 public override int read(char[] buffer, int offset, int length)
 {
     lock (@lock)
     {
         if (!isOpen())
         {
             throw new System.IO.IOException("InputStreamReader is closed");
         }
         java.util.Arrays.checkOffsetAndCount(buffer.Length, offset, length);
         if (length == 0)
         {
             return(0);
         }
         java.nio.CharBuffer          @out   = java.nio.CharBuffer.wrap(buffer, offset, length);
         java.nio.charset.CoderResult result = java.nio.charset.CoderResult.UNDERFLOW;
         // bytes.remaining() indicates number of bytes in buffer
         // when 1-st time entered, it'll be equal to zero
         bool needInput = !bytes.hasRemaining();
         while (@out.hasRemaining())
         {
             // fill the buffer if needed
             if (needInput)
             {
                 try
                 {
                     if (@in.available() == 0 && @out.position() > offset)
                     {
                         // we could return the result without blocking read
                         break;
                     }
                 }
                 catch (System.IO.IOException)
                 {
                 }
                 // available didn't work so just try the read
                 int desiredByteCount = bytes.capacity() - bytes.limit();
                 int off             = bytes.arrayOffset() + bytes.limit();
                 int actualByteCount = @in.read(((byte[])bytes.array()), off, desiredByteCount);
                 if (actualByteCount == -1)
                 {
                     endOfInput = true;
                     break;
                 }
                 else
                 {
                     if (actualByteCount == 0)
                     {
                         break;
                     }
                 }
                 bytes.limit(bytes.limit() + actualByteCount);
                 needInput = false;
             }
             // decode bytes
             result = decoder.decode(bytes, @out, false);
             if (result.isUnderflow())
             {
                 // compact the buffer if no space left
                 if (bytes.limit() == bytes.capacity())
                 {
                     bytes.compact();
                     bytes.limit(bytes.position());
                     bytes.position(0);
                 }
                 needInput = true;
             }
             else
             {
                 break;
             }
         }
         if (result == java.nio.charset.CoderResult.UNDERFLOW && endOfInput)
         {
             result = decoder.decode(bytes, @out, true);
             decoder.flush(@out);
             decoder.reset();
         }
         if (result.isMalformed() || result.isUnmappable())
         {
             result.throwException();
         }
         return(@out.position() - offset == 0 ? -1 : @out.position() - offset);
     }
 }