public override java.lang.CharSequence SubSequence(int start, int end)
 {
     checkStartEndRemaining(start, end);
     java.nio.CharBuffer result = duplicate();
     result.limit(_position + end);
     result.position(_position + start);
     return(result);
 }
Esempio n. 2
0
 /// <summary>
 /// Encodes characters starting at the current position of the given input
 /// buffer, and writes the equivalent byte sequence into the given output
 /// buffer from its current position.
 /// </summary>
 /// <remarks>
 /// Encodes characters starting at the current position of the given input
 /// buffer, and writes the equivalent byte sequence into the given output
 /// buffer from its current position.
 /// <p>
 /// The buffers' position will be changed with the reading and writing
 /// operation, but their limits and marks will be kept intact.
 /// <p>
 /// A <code>CoderResult</code> instance will be returned according to
 /// following rules:
 /// <ul>
 /// <li>A
 /// <see cref="CoderResult.malformedForLength(int)">malformed input</see>
 /// result
 /// indicates that some malformed input error was encountered, and the
 /// erroneous characters start at the input buffer's position and their
 /// number can be got by result's
 /// <see cref="CoderResult.length()">length</see>
 /// . This
 /// kind of result can be returned only if the malformed action is
 /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see>
 /// .</li>
 /// <li>
 /// <see cref="CoderResult.UNDERFLOW">CoderResult.UNDERFLOW</see>
 /// indicates that
 /// as many characters as possible in the input buffer have been encoded. If
 /// there is no further input and no characters left in the input buffer then
 /// this task is complete. If this is not the case then the client should
 /// call this method again supplying some more input characters.</li>
 /// <li>
 /// <see cref="CoderResult.OVERFLOW">CoderResult.OVERFLOW</see>
 /// indicates that the
 /// output buffer has been filled, while there are still some characters
 /// remaining in the input buffer. This method should be invoked again with a
 /// non-full output buffer.</li>
 /// <li>A
 /// <see cref="CoderResult.unmappableForLength(int)">unmappable character</see>
 /// result indicates that some unmappable character error was encountered,
 /// and the erroneous characters start at the input buffer's position and
 /// their number can be got by result's
 /// <see cref="CoderResult.length()">length</see>
 /// .
 /// This kind of result can be returned only on
 /// <see cref="CodingErrorAction.REPORT">CodingErrorAction.REPORT</see>
 /// .</li>
 /// </ul>
 /// <p>
 /// The <code>endOfInput</code> parameter indicates if the invoker can
 /// provider further input. This parameter is true if and only if the
 /// characters in the current input buffer are all inputs for this encoding
 /// operation. Note that it is common and won't cause an error if the invoker
 /// sets false and then has no more input available, while it may cause an
 /// error if the invoker always sets true in several consecutive invocations.
 /// This would make the remaining input to be treated as malformed input.
 /// input.
 /// <p>
 /// This method invokes the
 /// <see cref="encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer)">encodeLoop</see>
 /// method to
 /// implement the basic encode logic for a specific charset.
 /// </remarks>
 /// <param name="in">the input buffer.</param>
 /// <param name="out">the output buffer.</param>
 /// <param name="endOfInput">true if all the input characters have been provided.</param>
 /// <returns>a <code>CoderResult</code> instance indicating the result.</returns>
 /// <exception cref="System.InvalidOperationException">
 /// if the encoding operation has already started or no more
 /// input is needed in this encoding process.
 /// </exception>
 /// <exception cref="CoderMalfunctionError">
 /// If the
 /// <see cref="encodeLoop(java.nio.CharBuffer, java.nio.ByteBuffer)">encodeLoop</see>
 /// method threw an <code>BufferUnderflowException</code> or
 /// <code>BufferUnderflowException</code>.
 /// </exception>
 public java.nio.charset.CoderResult encode(java.nio.CharBuffer @in, java.nio.ByteBuffer
                                            @out, bool endOfInput)
 {
     // If the previous step is encode(CharBuffer), then no more input is needed
     // thus endOfInput should not be false
     if (status == READY && finished && !endOfInput)
     {
         throw new System.InvalidOperationException();
     }
     if ((status == FLUSH) || (!endOfInput && status == END))
     {
         throw new System.InvalidOperationException();
     }
     java.nio.charset.CoderResult result;
     while (true)
     {
         try
         {
             result = encodeLoop(@in, @out);
         }
         catch (java.nio.BufferOverflowException e)
         {
             throw new java.nio.charset.CoderMalfunctionError(e);
         }
         catch (java.nio.BufferUnderflowException e)
         {
             throw new java.nio.charset.CoderMalfunctionError(e);
         }
         if (result == java.nio.charset.CoderResult.UNDERFLOW)
         {
             status = endOfInput ? END : ONGOING;
             if (endOfInput)
             {
                 int remaining = @in.remaining();
                 if (remaining > 0)
                 {
                     result = java.nio.charset.CoderResult.malformedForLength(remaining);
                 }
                 else
                 {
                     return(result);
                 }
             }
             else
             {
                 return(result);
             }
         }
         else
         {
             if (result == java.nio.charset.CoderResult.OVERFLOW)
             {
                 status = endOfInput ? END : ONGOING;
                 return(result);
             }
         }
         java.nio.charset.CodingErrorAction action = _malformedInputAction;
         if (result.isUnmappable())
         {
             action = _unmappableCharacterAction;
         }
         // If the action is IGNORE or REPLACE, we should continue
         // encoding.
         if (action == java.nio.charset.CodingErrorAction.REPLACE)
         {
             if (@out.remaining() < replacementBytes.Length)
             {
                 return(java.nio.charset.CoderResult.OVERFLOW);
             }
             @out.put(replacementBytes);
         }
         else
         {
             if (action != java.nio.charset.CodingErrorAction.IGNORE)
             {
                 return(result);
             }
         }
         @in.position(@in.position() + result.length());
     }
 }
Esempio n. 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);
     }
 }