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;
}
}
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());
}
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);
}
}
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;
}
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;
}
Aggregations