// 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());
 }
Exemple #2
0
 /// <exception cref="System.IO.IOException"></exception>
 private void drainEncoder()
 {
     // Strictly speaking, I think it's part of the CharsetEncoder contract that you call
     // encode with endOfInput true before flushing. Our ICU-based implementations don't
     // actually need this, and you'd hope that any reasonable implementation wouldn't either.
     // CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway!
     java.nio.CharBuffer chars = java.nio.CharBuffer.allocate(0);
     while (true)
     {
         java.nio.charset.CoderResult result = encoder.encode(chars, bytes, true);
         if (result.isError())
         {
             result.throwException();
         }
         else
         {
             if (result.isOverflow())
             {
                 flushBytes(false);
                 continue;
             }
         }
         break;
     }
     // Some encoders (such as ISO-2022-JP) have stuff to write out after all the
     // characters (such as shifting back into a default state). In our implementation,
     // this is actually the first time ICU is told that we've run out of input.
     java.nio.charset.CoderResult result_1 = encoder.flush(bytes);
     while (!result_1.isUnderflow())
     {
         if (result_1.isOverflow())
         {
             flushBytes(false);
             result_1 = encoder.flush(bytes);
         }
         else
         {
             result_1.throwException();
         }
     }
 }
Exemple #3
0
 /// <exception cref="System.IO.IOException"></exception>
 private void convert(java.nio.CharBuffer chars)
 {
     while (true)
     {
         java.nio.charset.CoderResult result = encoder.encode(chars, bytes, false);
         if (result.isOverflow())
         {
             // Make room and try again.
             flushBytes(false);
             continue;
         }
         else
         {
             if (result.isError())
             {
                 result.throwException();
             }
         }
         break;
     }
 }
Exemple #4
0
 public virtual void flush()
 {
     //Log.i("PackageManager", "flush mPos=" + mPos);
     if (mPos > 0)
     {
         if (mOutputStream != null)
         {
             java.nio.CharBuffer          charBuffer = java.nio.CharBuffer.wrap(mText, 0, mPos);
             java.nio.charset.CoderResult result     = mCharset.encode(charBuffer, mBytes, true);
             while (true)
             {
                 if (result.isError())
                 {
                     throw new System.IO.IOException(result.ToString());
                 }
                 else
                 {
                     if (result.isOverflow())
                     {
                         flushBytes();
                         result = mCharset.encode(charBuffer, mBytes, true);
                         continue;
                     }
                 }
                 break;
             }
             flushBytes();
             mOutputStream.flush();
         }
         else
         {
             mWriter.write(mText, 0, mPos);
             mWriter.flush();
         }
         mPos = 0;
     }
 }