Search in sources :

Example 61 with CoderResult

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

the class InputStreamReader method read.

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
    if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }
    while (!done && !cb.hasRemaining()) {
        int read = 0;
        int pos = bb.position();
        while ((read = rbc.read(bb)) == 0) {
        // blocking read
        }
        bb.flip();
        cb.clear();
        CoderResult cr = cd.decode(bb, cb, read == -1);
        checkResult(cr);
        if (read == -1) {
            cr = cd.flush(cb);
            checkResult(cr);
            done = true;
        }
        bytesProcessed += bb.position() - pos;
        if (bb.position() != read + pos) {
            bb.compact();
        } else {
            bb.clear();
        }
        cb.flip();
    }
    len = Math.min(len, cb.remaining());
    if (len == 0 && done) {
        return -1;
    }
    cb.get(cbuf, off, len);
    return len;
}
Also used : CoderResult(java.nio.charset.CoderResult)

Example 62 with CoderResult

use of java.nio.charset.CoderResult in project be5 by DevelopmentOnTheEdge.

the class ProjectFileSystem method read.

public static String read(final Path file) throws ReadException {
    if (!Files.exists(file)) {
        throw new ReadException(file, ReadException.LEE_NOT_FOUND);
    }
    if (!Files.isRegularFile(file)) {
        throw new ReadException(file, ReadException.LEE_NOT_A_FILE);
    }
    final byte[] bytes;
    try {
        bytes = readBytes(file);
    } catch (Exception e) {
        throw new ReadException(e, file, ReadException.LEE_UNREADABLE);
    }
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    char[] resultArray = new char[(int) (bytes.length * decoder.maxCharsPerByte() + 1)];
    CharBuffer decoded = CharBuffer.wrap(resultArray);
    CoderResult result = decoder.decode(buffer, decoded, true);
    try {
        if (!result.isUnderflow())
            result.throwException();
        result = decoder.flush(decoded);
        if (!result.isUnderflow())
            result.throwException();
    } catch (UnmappableCharacterException e) {
        throw new ReadException(new Exception("Unmappable character at " + calcPosition(decoded)), file, ReadException.LEE_ENCODING_ERROR);
    } catch (CharacterCodingException e) {
        throw new ReadException(new Exception("Malformed character at " + calcPosition(decoded)), file, ReadException.LEE_ENCODING_ERROR);
    }
    int start = 0;
    if (// Ignore BOM
    resultArray.length > 0 && resultArray[0] == '\uFEFF')
        start++;
    return new String(resultArray, start, decoded.position() - start);
}
Also used : ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) IOException(java.io.IOException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) UncheckedIOException(java.io.UncheckedIOException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) CoderResult(java.nio.charset.CoderResult)

Example 63 with CoderResult

use of java.nio.charset.CoderResult in project UnityModManager by xausky.

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

use of java.nio.charset.CoderResult in project ceylon by eclipse.

the class BaseFileManager method decode.

public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
    String encodingName = getEncodingName();
    CharsetDecoder decoder;
    try {
        decoder = getDecoder(encodingName, ignoreEncodingErrors);
    } catch (IllegalCharsetNameException e) {
        log.error("unsupported.encoding", encodingName);
        return (CharBuffer) CharBuffer.allocate(1).flip();
    } catch (UnsupportedCharsetException e) {
        log.error("unsupported.encoding", encodingName);
        return (CharBuffer) CharBuffer.allocate(1).flip();
    }
    // slightly overestimate the buffer size to avoid reallocation.
    float factor = decoder.averageCharsPerByte() * 0.8f + decoder.maxCharsPerByte() * 0.2f;
    CharBuffer dest = CharBuffer.allocate(10 + (int) (inbuf.remaining() * factor));
    while (true) {
        CoderResult result = decoder.decode(inbuf, dest, true);
        dest.flip();
        if (result.isUnderflow()) {
            // make sure there is at least one extra character
            if (dest.limit() == dest.capacity()) {
                dest = CharBuffer.allocate(dest.capacity() + 1).put(dest);
                dest.flip();
            }
            return dest;
        } else if (result.isOverflow()) {
            // buffer too small; expand
            int newCapacity = 10 + dest.capacity() + (int) (inbuf.remaining() * decoder.maxCharsPerByte());
            dest = CharBuffer.allocate(newCapacity).put(dest);
        } else if (result.isMalformed() || result.isUnmappable()) {
            // report coding error (warn only pre 1.5)
            if (!getSource().allowEncodingErrors()) {
                log.error(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name());
            } else {
                log.warning(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name());
            }
            // skip past the coding error
            inbuf.position(inbuf.position() + result.length());
            // undo the flip() to prepare the output buffer
            // for more translation
            dest.position(dest.limit());
            dest.limit(dest.capacity());
            // backward compatible
            dest.put((char) 0xfffd);
        } else {
            throw new AssertionError(result);
        }
    }
// unreached
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) SimpleDiagnosticPosition(org.eclipse.ceylon.langtools.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition) CoderResult(java.nio.charset.CoderResult)

Example 65 with CoderResult

use of java.nio.charset.CoderResult in project bson4jackson by michel-kraemer.

the class DynamicOutputBuffer method putUTF8.

/**
 * Puts the given string as UTF-8 into the buffer at the
 * given position. This method does not increase the write position.
 * @param pos the position where to put the string
 * @param s the string to put
 * @return the number of UTF-8 bytes put
 */
public int putUTF8(int pos, String s) {
    ByteBuffer minibb = null;
    CharsetEncoder enc = getUTF8Encoder();
    CharBuffer in = CharBuffer.wrap(s);
    int pos2 = pos;
    ByteBuffer bb = getBuffer(pos2);
    int index = pos2 % _bufferSize;
    bb.position(index);
    while (in.remaining() > 0) {
        CoderResult res = enc.encode(in, bb, true);
        // flush minibb first
        if (bb == minibb) {
            bb.flip();
            while (bb.remaining() > 0) {
                putByte(pos2, bb.get());
                ++pos2;
            }
        } else {
            pos2 += bb.position() - index;
        }
        if (res.isOverflow()) {
            if (bb.remaining() > 0) {
                // exceeded buffer boundaries; write to a small temporary buffer
                if (minibb == null) {
                    minibb = ByteBuffer.allocate(4);
                }
                minibb.rewind();
                bb = minibb;
                index = 0;
            } else {
                bb = getBuffer(pos2);
                index = pos2 % _bufferSize;
                bb.position(index);
            }
        } else if (res.isError()) {
            try {
                res.throwException();
            } catch (CharacterCodingException e) {
                throw new RuntimeException("Could not encode string", e);
            }
        }
    }
    adaptSize(pos2);
    return pos2 - pos;
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) 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