Search in sources :

Example 31 with CharacterCodingException

use of java.nio.charset.CharacterCodingException 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 32 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project erlide_eclipse by erlang.

the class Util method encode.

public static byte[] encode(final String string, final String encoding) {
    final Charset charset = Charset.forName(encoding);
    final CharsetEncoder encoder = charset.newEncoder();
    try {
        final CharBuffer cbuf = CharBuffer.wrap(string);
        final ByteBuffer bbuf = encoder.encode(cbuf);
        return bbuf.array();
    } catch (final CharacterCodingException e) {
        return null;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 33 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project erlide_eclipse by erlang.

the class Util method decode.

public static String decode(final byte[] binaryValue, final Charset charset) {
    final CharsetDecoder decoder = charset.newDecoder();
    try {
        final ByteBuffer bbuf = ByteBuffer.wrap(binaryValue);
        final CharBuffer cbuf = decoder.decode(bbuf);
        return cbuf.toString();
    } catch (final CharacterCodingException e) {
        return null;
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 34 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project erlide_eclipse by erlang.

the class IndexedErlangValue method getBinaryValueString.

private static String getBinaryValueString(final OtpErlangBinary b) {
    final StringBuilder sb = new StringBuilder("<<");
    if (b.size() > 0) {
        final byte[] bytes = b.binaryValue();
        CharBuffer cb = null;
        if (IndexedErlangValue.looksLikeAscii(bytes)) {
            final Charset[] css = { Charsets.UTF_8, Charsets.ISO_8859_1 };
            final Charset[] tryCharsets = css;
            for (final Charset cset : tryCharsets) {
                final CharsetDecoder cd = cset.newDecoder();
                cd.onMalformedInput(CodingErrorAction.REPORT);
                cd.onUnmappableCharacter(CodingErrorAction.REPORT);
                try {
                    cb = cd.decode(ByteBuffer.wrap(bytes));
                    break;
                } catch (final CharacterCodingException e) {
                }
            }
        }
        if (cb != null && cb.length() > 0) {
            sb.append('"').append(cb).append('"');
        } else {
            for (int i = 0, n = bytes.length; i < n; ++i) {
                int j = bytes[i];
                if (j < 0) {
                    j += 256;
                }
                sb.append(j);
                if (i < n - 1) {
                    sb.append(',');
                }
            }
        }
    }
    sb.append(">>");
    return sb.toString();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 35 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project mkgmap by openstreetmap.

the class PolishMapDataSource method recode.

/**
 * Convert the value of a label into a string based on the declared
 * code page in the file.
 *
 * This makes assumptions about the way that the .mp file is written
 * that may not be correct.
 *
 * @param value The string that has been read with ISO-8859-1.
 * @return A possibly different string that is obtained by taking the
 * bytes in the input string and decoding them as if they had the
 * declared code page.
 */
private String recode(String value) {
    if (dec != null) {
        try {
            // Get the bytes that were actually in the file.
            byte[] bytes = value.getBytes(READING_CHARSET);
            ByteBuffer buf = ByteBuffer.wrap(bytes);
            // Decode from bytes with the correct code page.
            CharBuffer out = dec.decode(buf);
            return out.toString();
        } catch (UnsupportedEncodingException e) {
            // Java requires this support, so unlikely to happen
            log.warn("no support for " + READING_CHARSET);
        } catch (CharacterCodingException e) {
            log.error("error decoding label", e);
        }
    }
    return value;
}
Also used : CharBuffer(java.nio.CharBuffer) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)196 ByteBuffer (java.nio.ByteBuffer)114 CharBuffer (java.nio.CharBuffer)48 CharsetDecoder (java.nio.charset.CharsetDecoder)44 IOException (java.io.IOException)34 CharsetEncoder (java.nio.charset.CharsetEncoder)31 CoderResult (java.nio.charset.CoderResult)30 Charset (java.nio.charset.Charset)27 InputStream (java.io.InputStream)9 Date (java.util.Date)9 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)8 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)6 Path (java.nio.file.Path)6 ParseException (java.text.ParseException)6 Test (org.junit.Test)6 CoreException (org.eclipse.core.runtime.CoreException)5 HumanReadableException (com.facebook.buck.util.HumanReadableException)4 OutputStream (java.io.OutputStream)4