Search in sources :

Example 96 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 97 with CharacterCodingException

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

use of java.nio.charset.CharacterCodingException in project bigbluebutton by bigbluebutton.

the class BlockStreamProtocolDecoder method tryToParsePolicyRequest.

private boolean tryToParsePolicyRequest(IoSession session, IoBuffer in, ProtocolDecoderOutput out) {
    byte[] message = new byte[POLICY_REQUEST.length];
    if (in.remaining() >= POLICY_REQUEST.length) {
        in.get(message, 0, message.length);
        if (Arrays.equals(message, POLICY_REQUEST)) {
            log.debug("Sending cross domain policy to the user");
            IoBuffer buffer = IoBuffer.allocate(8);
            buffer.setAutoExpand(true);
            try {
                buffer.putString("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>", Charset.forName("UTF-8").newEncoder());
                buffer.put((byte) 0);
            } catch (CharacterCodingException e) {
                e.printStackTrace();
                return false;
            }
            buffer.flip();
            session.write(buffer);
            return true;
        }
    }
    return false;
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) IoBuffer(org.apache.mina.core.buffer.IoBuffer)

Example 100 with CharacterCodingException

use of java.nio.charset.CharacterCodingException in project bigbluebutton by bigbluebutton.

the class BlockStreamProtocolDecoder method decodeRoom.

private String decodeRoom(IoSession session, IoBuffer in) {
    int roomLength = in.get();
    //    	System.out.println("Room length = " + roomLength);
    String room = "";
    try {
        room = in.getString(roomLength, Charset.forName("UTF-8").newDecoder());
        if (session.containsAttribute(ROOM)) {
            String attRoom = (String) session.getAttribute(ROOM);
            if (!attRoom.equals(room)) {
                log.warn(room + " is not the same as room in attribute [" + attRoom + "]");
            }
        }
    } catch (CharacterCodingException e) {
        log.error(e.getMessage());
    }
    return room;
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) Point(java.awt.Point)

Aggregations

CharacterCodingException (java.nio.charset.CharacterCodingException)197 ByteBuffer (java.nio.ByteBuffer)115 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