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