use of java.nio.charset.CharsetEncoder in project Bytecoder by mirkosertic.
the class ZipCoder method getBytes.
byte[] getBytes(String s) {
CharsetEncoder ce = encoder().reset();
char[] ca = s.toCharArray();
int len = (int) (ca.length * ce.maxBytesPerChar());
byte[] ba = new byte[len];
if (len == 0)
return ba;
// CodingErrorAction.REPLACE mode.
if (isUTF8 && ce instanceof ArrayEncoder) {
int blen = ((ArrayEncoder) ce).encode(ca, 0, ca.length, ba);
if (// malformed
blen == -1)
throw new IllegalArgumentException("MALFORMED");
return Arrays.copyOf(ba, blen);
}
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca);
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
cr = ce.flush(bb);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
if (// defensive copy?
bb.position() == ba.length)
return ba;
else
return Arrays.copyOf(ba, bb.position());
}
use of java.nio.charset.CharsetEncoder in project jackrabbit-oak by apache.
the class UTF8Encoder method encodeAsByteArray.
/**
* Like {@link String#getBytes(java.nio.charset.Charset)} (with "UTF-8"),
* except that invalid character sequences (such as unpaired surrogates) are
* reported as exceptions (see {@link CodingErrorAction#REPORT}, instead of
* being silently replaced by a replacement character as it would happen
* otherwise.
*
* @param input
* String to encode
* @return String encoded using {@link StandardCharsets#UTF_8}
* @throws IOException
* on encoding error
*/
public static byte[] encodeAsByteArray(String input) throws IOException {
CharsetEncoder e = CSE.get();
e.reset();
return bytes(e.encode(CharBuffer.wrap(input.toCharArray())));
}
use of java.nio.charset.CharsetEncoder in project jackrabbit-oak by apache.
the class UTF8Encoder method canEncode.
/**
* @see {@link CharsetEncoder#canEncode(CharSequence)
*/
public static boolean canEncode(CharSequence input) {
CharsetEncoder e = CSE.get();
e.reset();
return e.canEncode(input);
}
use of java.nio.charset.CharsetEncoder 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();
}
use of java.nio.charset.CharsetEncoder in project jackrabbit-oak by apache.
the class CharsetEncodingUtils method encodeAsUTF8.
/**
* Like {@link String#getBytes(java.nio.charset.Charset)} (with "UTF-8"),
* except that encoding problems (like unpaired surrogates) are reported as
* exceptions (see {@link CodingErrorAction#REPORT}, instead of being
* silently replaces as it would happen otherwise.
*
* @param input
* String to encode
* @return String encoded using {@link StandardCharsets#UTF_8}
* @throws IOException
* on encoding error
*/
public static byte[] encodeAsUTF8(String input) throws IOException {
CharsetEncoder e = CSE.get();
e.reset();
return bytes(e.encode(CharBuffer.wrap(input.toCharArray())));
}
Aggregations