Search in sources :

Example 6 with ArrayDecoder

use of sun.nio.cs.ArrayDecoder in project Bytecoder by mirkosertic.

the class ZipCoder method toString.

String toString(byte[] ba, int off, 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, off, length, ca);
        if (// malformed
        clen == -1)
            throw new IllegalArgumentException("MALFORMED");
        return new String(ca, 0, clen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba, off, 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 7 with ArrayDecoder

use of sun.nio.cs.ArrayDecoder in project Bytecoder by mirkosertic.

the class StringCoding method decode.

static Result decode(Charset cs, byte[] ba, int off, int len) {
    // is started...
    if (cs == UTF_8) {
        return StringDecoderUTF8.decode(ba, off, len, new Result());
    }
    CharsetDecoder cd = cs.newDecoder();
    // ascii fastpath
    if (cs == ISO_8859_1 || ((cd instanceof ArrayDecoder) && ((ArrayDecoder) cd).isASCIICompatible() && !hasNegatives(ba, off, len))) {
        if (COMPACT_STRINGS) {
            return new Result().with(Arrays.copyOfRange(ba, off, off + len), LATIN1);
        } else {
            return new Result().with(StringLatin1.inflate(ba, off, len), UTF16);
        }
    }
    int en = scale(len, cd.maxCharsPerByte());
    if (len == 0) {
        return new Result().with();
    }
    if (cs.getClass().getClassLoader0() != null && System.getSecurityManager() != null) {
        ba = Arrays.copyOfRange(ba, off, off + len);
        off = 0;
    }
    cd.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
    char[] ca = new char[en];
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
        return new Result().with(ca, 0, clen);
    }
    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) {
        // so this shouldn't happen
        throw new Error(x);
    }
    return new Result().with(ca, 0, cb.position());
}
Also used : ArrayDecoder(sun.nio.cs.ArrayDecoder) CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult) CoderResult(java.nio.charset.CoderResult)

Aggregations

ByteBuffer (java.nio.ByteBuffer)7 CharBuffer (java.nio.CharBuffer)7 CoderResult (java.nio.charset.CoderResult)7 ArrayDecoder (sun.nio.cs.ArrayDecoder)7 CharsetDecoder (java.nio.charset.CharsetDecoder)6 CharacterCodingException (java.nio.charset.CharacterCodingException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 UncheckedIOException (java.io.UncheckedIOException)1