Search in sources :

Example 41 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project dkpro-lab by dkpro.

the class Encode method decodeBytes.

private static String decodeBytes(String enc, CharsetDecoder decoder) {
    Matcher matcher = encodedChars.matcher(enc);
    StringBuffer buf = new StringBuffer();
    ByteBuffer bytes = ByteBuffer.allocate(enc.length() / 3);
    while (matcher.find()) {
        int b = Integer.parseInt(matcher.group(1), 16);
        bytes.put((byte) b);
    }
    bytes.flip();
    try {
        return decoder.decode(bytes).toString();
    } catch (CharacterCodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Matcher(java.util.regex.Matcher) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 42 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project incubator-rya by apache.

the class ColumnPrefixes method concat.

private static Text concat(Text prefix, String str) {
    Text temp = new Text(prefix);
    try {
        ByteBuffer buffer = Text.encode(str, false);
        temp.append(buffer.array(), 0, buffer.limit());
    } catch (CharacterCodingException cce) {
        throw new IllegalArgumentException(cce);
    }
    return temp;
}
Also used : Text(org.apache.hadoop.io.Text) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 43 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project Bytecoder by mirkosertic.

the class URI method encode.

// Encodes all characters >= \u0080 into escaped, normalized UTF-8 octets,
// assuming that s is otherwise legal
// 
private static String encode(String s) {
    int n = s.length();
    if (n == 0)
        return s;
    // First check whether we actually need to encode
    for (int i = 0; ; ) {
        if (s.charAt(i) >= '\u0080')
            break;
        if (++i >= n)
            return s;
    }
    String ns = Normalizer.normalize(s, Normalizer.Form.NFC);
    ByteBuffer bb = null;
    try {
        bb = ThreadLocalCoders.encoderFor("UTF-8").encode(CharBuffer.wrap(ns));
    } catch (CharacterCodingException x) {
        assert false;
    }
    StringBuilder sb = new StringBuilder();
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (b >= 0x80)
            appendEscape(sb, (byte) b);
        else
            sb.append((char) b);
    }
    return sb.toString();
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Example 44 with CharacterCodingException

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

Example 45 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project jackrabbit-oak by apache.

the class FileIOUtilsTest method getRandomTestString.

private static String getRandomTestString() throws Exception {
    boolean valid = false;
    StringBuilder buffer = new StringBuilder();
    while (!valid) {
        int length = RANDOM.nextInt(40);
        for (int i = 0; i < length; i++) {
            buffer.append((char) (RANDOM.nextInt(Character.MAX_VALUE)));
        }
        String s = buffer.toString();
        CharsetEncoder encoder = Charset.forName(UTF_8.toString()).newEncoder();
        try {
            encoder.encode(CharBuffer.wrap(s));
            valid = true;
        } catch (CharacterCodingException e) {
            buffer = new StringBuilder();
        }
    }
    return buffer.toString();
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder)

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