Search in sources :

Example 96 with CoderResult

use of java.nio.charset.CoderResult in project ADWLauncher2 by boombuler.

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 97 with CoderResult

use of java.nio.charset.CoderResult in project gradle by gradle.

the class StreamByteBuffer method readAsCharBuffer.

private CharBuffer readAsCharBuffer(Charset charset) throws CharacterCodingException {
    CharsetDecoder decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    CharBuffer charbuffer = CharBuffer.allocate(totalBytesUnread());
    ByteBuffer buf = null;
    boolean wasUnderflow = false;
    ByteBuffer nextBuf = null;
    boolean needsFlush = false;
    while (hasRemaining(nextBuf) || hasRemaining(buf) || prepareRead() != -1) {
        if (hasRemaining(buf)) {
            // handle decoding underflow, multi-byte unicode character at buffer chunk boundary
            if (!wasUnderflow) {
                throw new IllegalStateException("Unexpected state. Buffer has remaining bytes without underflow in decoding.");
            }
            if (!hasRemaining(nextBuf) && prepareRead() != -1) {
                nextBuf = currentReadChunk.readToNioBuffer();
            }
            // copy one by one until the underflow has been resolved
            buf = ByteBuffer.allocate(buf.remaining() + 1).put(buf);
            buf.put(nextBuf.get());
            buf.flip();
        } else {
            if (hasRemaining(nextBuf)) {
                buf = nextBuf;
            } else if (prepareRead() != -1) {
                buf = currentReadChunk.readToNioBuffer();
                if (!hasRemaining(buf)) {
                    throw new IllegalStateException("Unexpected state. Buffer is empty.");
                }
            }
            nextBuf = null;
        }
        boolean endOfInput = !hasRemaining(nextBuf) && prepareRead() == -1;
        int bufRemainingBefore = buf.remaining();
        CoderResult result = decoder.decode(buf, charbuffer, false);
        if (bufRemainingBefore > buf.remaining()) {
            needsFlush = true;
        }
        if (endOfInput) {
            result = decoder.decode(ByteBuffer.allocate(0), charbuffer, true);
            if (!result.isUnderflow()) {
                result.throwException();
            }
            break;
        }
        wasUnderflow = result.isUnderflow();
    }
    if (needsFlush) {
        CoderResult result = decoder.flush(charbuffer);
        if (!result.isUnderflow()) {
            result.throwException();
        }
    }
    clear();
    // push back remaining bytes of multi-byte unicode character
    while (hasRemaining(buf)) {
        byte b = buf.get();
        try {
            getOutputStream().write(b);
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }
    }
    charbuffer.flip();
    return charbuffer;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 98 with CoderResult

use of java.nio.charset.CoderResult in project j2objc by google.

the class OutputStreamWriter method drainEncoder.

private void drainEncoder() throws IOException {
    // 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!
    CharBuffer chars = CharBuffer.allocate(0);
    while (true) {
        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.
    CoderResult result = encoder.flush(bytes);
    while (!result.isUnderflow()) {
        if (result.isOverflow()) {
            flushBytes(false);
            result = encoder.flush(bytes);
        } else {
            result.throwException();
        }
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Example 99 with CoderResult

use of java.nio.charset.CoderResult in project j2objc by google.

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(StandardCharsets.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 100 with CoderResult

use of java.nio.charset.CoderResult in project j2objc by google.

the class CharsetEncoderTest method testSurrogatePairAllAtOnce.

public void testSurrogatePairAllAtOnce() throws Exception {
    // okay: surrogate pair seen all at once is decoded to U+20b9f.
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '�', '�' }), bb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(4, bb.position());
    assertEquals((byte) 0x00, bb.get(0));
    assertEquals((byte) 0x02, bb.get(1));
    assertEquals((byte) 0x0b, bb.get(2));
    assertEquals((byte) 0x9f, bb.get(3));
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Aggregations

CoderResult (java.nio.charset.CoderResult)188 CharBuffer (java.nio.CharBuffer)121 ByteBuffer (java.nio.ByteBuffer)70 CharsetDecoder (java.nio.charset.CharsetDecoder)40 IOException (java.io.IOException)33 CharacterCodingException (java.nio.charset.CharacterCodingException)30 CharsetEncoder (java.nio.charset.CharsetEncoder)25 Charset (java.nio.charset.Charset)15 ArrayDecoder (sun.nio.cs.ArrayDecoder)7 ArrayEncoder (sun.nio.cs.ArrayEncoder)7 JSONException (com.alibaba.fastjson.JSONException)3 BufferUnderflowException (java.nio.BufferUnderflowException)3 CloseReason (jakarta.websocket.CloseReason)2 UncheckedIOException (java.io.UncheckedIOException)2 BufferOverflowException (java.nio.BufferOverflowException)2 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 CloseReason (javax.websocket.CloseReason)2 InvalidConfigurationException (org.bukkit.configuration.InvalidConfigurationException)2