Search in sources :

Example 46 with CoderResult

use of java.nio.charset.CoderResult in project checker-framework by typetools.

the class StringCoding method encode.

static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca = Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder) ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) ArrayEncoder(sun.nio.cs.ArrayEncoder) CoderResult(java.nio.charset.CoderResult)

Example 47 with CoderResult

use of java.nio.charset.CoderResult in project checker-framework by typetools.

the class ZipCoder method toString.

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int) (length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    // REPORT mode.
    if (isUTF8 && cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder) cd).decode(ba, 0, length, ca);
        if (// malformed
        clen == -1)
            throw new IllegalArgumentException("MALFORMED");
        return new String(ca, 0, clen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
Also used : ArrayDecoder(sun.nio.cs.ArrayDecoder) CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 48 with CoderResult

use of java.nio.charset.CoderResult in project SimpleFlatMapper by arnaudroger.

the class CharReadingBenchmark method memoryMappedFile.

@Benchmark
public void memoryMappedFile(Blackhole blackhole) throws IOException {
    char[] buffer = new char[bufferSize];
    try (FileChannel channel = FileChannel.open(file.toPath())) {
        MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        CharsetDecoder charsetDecoder = charset.newDecoder();
        while (byteBuffer.hasRemaining()) {
            CoderResult coderResult = charsetDecoder.decode(byteBuffer, CharBuffer.wrap(buffer), true);
            blackhole.consume(buffer);
        }
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) CoderResult(java.nio.charset.CoderResult) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 49 with CoderResult

use of java.nio.charset.CoderResult in project spf4j by zolyfarkas.

the class Strings method decode.

@SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
public static String decode(final CharsetDecoder cd, final byte[] ba, final int off, final int len) {
    if (len == 0) {
        return "";
    }
    int en = (int) (len * (double) cd.maxCharsPerByte());
    char[] ca = TLScratch.getCharsTmp(en);
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
        return new String(ca, 0, clen);
    }
    cd.reset();
    ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
    CharBuffer cb = CharBuffer.wrap(ca);
    try {
        CoderResult cr = cd.decode(bb, cb, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = cd.flush(cb);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new UncheckedIOException(x);
    }
    return new String(ca, 0, cb.position());
}
Also used : ArrayDecoder(sun.nio.cs.ArrayDecoder) CharBuffer(java.nio.CharBuffer) UncheckedIOException(java.io.UncheckedIOException) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 50 with CoderResult

use of java.nio.charset.CoderResult in project liferay-ide by liferay.

the class BndProperties method _convert.

private String _convert(byte[] buffer, Charset charset) throws IOException {
    CharsetDecoder decoder = charset.newDecoder();
    ByteBuffer bb = ByteBuffer.wrap(buffer);
    CharBuffer cb = CharBuffer.allocate(buffer.length * 4);
    CoderResult result = decoder.decode(bb, cb, true);
    if (!result.isError()) {
        return new String(cb.array(), 0, cb.position());
    }
    throw new CharacterCodingException();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) 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