Search in sources :

Example 36 with CoderResult

use of java.nio.charset.CoderResult in project XobotOS by xamarin.

the class Manifest method writeEntry.

private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
    String nameString = name.getName();
    os.write(nameString.getBytes(Charsets.US_ASCII));
    os.write(VALUE_SEPARATOR);
    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);
    CharBuffer cBuf = CharBuffer.wrap(value);
    while (true) {
        CoderResult r = encoder.encode(cBuf, bBuf, true);
        if (CoderResult.UNDERFLOW == r) {
            r = encoder.flush(bBuf);
        }
        os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
        os.write(LINE_SEPARATOR);
        if (CoderResult.UNDERFLOW == r) {
            break;
        }
        os.write(' ');
        bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Example 37 with CoderResult

use of java.nio.charset.CoderResult in project android_frameworks_base by DirtyUnicorns.

the class FastXmlSerializer method flush.

public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new 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;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Example 38 with CoderResult

use of java.nio.charset.CoderResult in project android_frameworks_base by DirtyUnicorns.

the class LoggingPrintStream method write.

@Override
public synchronized void write(byte[] bytes, int start, int count) {
    if (decoder == null) {
        encodedBytes = ByteBuffer.allocate(80);
        decodedChars = CharBuffer.allocate(80);
        decoder = Charset.defaultCharset().newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    int end = start + count;
    while (start < end) {
        // copy some bytes from the array to the long-lived buffer. This
        // way, if we end with a partial character we don't lose it.
        int numBytes = Math.min(encodedBytes.remaining(), end - start);
        encodedBytes.put(bytes, start, numBytes);
        start += numBytes;
        encodedBytes.flip();
        CoderResult coderResult;
        do {
            // decode bytes from the byte buffer into the char buffer
            coderResult = decoder.decode(encodedBytes, decodedChars, false);
            // copy chars from the char buffer into our string builder
            decodedChars.flip();
            builder.append(decodedChars);
            decodedChars.clear();
        } while (coderResult.isOverflow());
        encodedBytes.compact();
    }
    flush(false);
}
Also used : CoderResult(java.nio.charset.CoderResult)

Example 39 with CoderResult

use of java.nio.charset.CoderResult in project android_frameworks_base by AOSPA.

the class LoggingPrintStream method write.

@Override
public synchronized void write(byte[] bytes, int start, int count) {
    if (decoder == null) {
        encodedBytes = ByteBuffer.allocate(80);
        decodedChars = CharBuffer.allocate(80);
        decoder = Charset.defaultCharset().newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    int end = start + count;
    while (start < end) {
        // copy some bytes from the array to the long-lived buffer. This
        // way, if we end with a partial character we don't lose it.
        int numBytes = Math.min(encodedBytes.remaining(), end - start);
        encodedBytes.put(bytes, start, numBytes);
        start += numBytes;
        encodedBytes.flip();
        CoderResult coderResult;
        do {
            // decode bytes from the byte buffer into the char buffer
            coderResult = decoder.decode(encodedBytes, decodedChars, false);
            // copy chars from the char buffer into our string builder
            decodedChars.flip();
            builder.append(decodedChars);
            decodedChars.clear();
        } while (coderResult.isOverflow());
        encodedBytes.compact();
    }
    flush(false);
}
Also used : CoderResult(java.nio.charset.CoderResult)

Example 40 with CoderResult

use of java.nio.charset.CoderResult in project android_frameworks_base by ResurrectionRemix.

the class FastPrintWriter method flushLocked.

private void flushLocked() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (!mIoError) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytesLocked();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            if (!mIoError) {
                flushBytesLocked();
                mOutputStream.flush();
            }
        } else if (mWriter != null) {
            if (!mIoError) {
                mWriter.write(mText, 0, mPos);
                mWriter.flush();
            }
        } else {
            int nonEolOff = 0;
            final int sepLen = mSeparator.length();
            final int len = sepLen < mPos ? sepLen : mPos;
            while (nonEolOff < len && mText[mPos - 1 - nonEolOff] == mSeparator.charAt(mSeparator.length() - 1 - nonEolOff)) {
                nonEolOff++;
            }
            if (nonEolOff >= mPos) {
                mPrinter.println("");
            } else {
                mPrinter.println(new String(mText, 0, mPos - nonEolOff));
            }
        }
        mPos = 0;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Aggregations

CoderResult (java.nio.charset.CoderResult)123 CharBuffer (java.nio.CharBuffer)81 ByteBuffer (java.nio.ByteBuffer)35 IOException (java.io.IOException)25 CharsetDecoder (java.nio.charset.CharsetDecoder)25 Charset (java.nio.charset.Charset)15 CharacterCodingException (java.nio.charset.CharacterCodingException)13 CharsetEncoder (java.nio.charset.CharsetEncoder)13 BufferUnderflowException (java.nio.BufferUnderflowException)3 BufferOverflowException (java.nio.BufferOverflowException)2 CloseReason (javax.websocket.CloseReason)2 ArrayDecoder (sun.nio.cs.ArrayDecoder)2 ArrayEncoder (sun.nio.cs.ArrayEncoder)2 JSONException (com.alibaba.fastjson.JSONException)1 SimpleDiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition)1 ParseException (io.druid.java.util.common.parsers.ParseException)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Cipher (javax.crypto.Cipher)1