Exemplo n.º 1
0
 // default implementation is empty
 /// <summary>
 /// Checks if the given argument is legal as this encoder's replacement byte
 /// array.
 /// </summary>
 /// <remarks>
 /// Checks if the given argument is legal as this encoder's replacement byte
 /// array.
 /// The given byte array is legal if and only if it can be decode into
 /// sixteen bits Unicode characters.
 /// This method can be overridden for performance improvement.
 /// </remarks>
 /// <param name="replacement">the given byte array to be checked.</param>
 /// <returns>
 /// true if the the given argument is legal as this encoder's
 /// replacement byte array.
 /// </returns>
 public virtual bool isLegalReplacement(byte[] replacement_1)
 {
     if (decoder == null)
     {
         decoder = cs.newDecoder();
         decoder.onMalformedInput(java.nio.charset.CodingErrorAction.REPORT);
         decoder.onUnmappableCharacter(java.nio.charset.CodingErrorAction.REPORT);
     }
     java.nio.ByteBuffer @in  = java.nio.ByteBuffer.wrap(replacement_1);
     java.nio.CharBuffer @out = java.nio.CharBuffer.allocate((int)(replacement_1.Length
                                                                   * decoder.maxCharsPerByte()));
     java.nio.charset.CoderResult result = decoder.decode(@in, @out, true);
     return(!result.isError());
 }
Exemplo n.º 2
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);
     }
 }