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