use of java.nio.charset.CharsetEncoder in project robovm by robovm.
the class OldCharsetEncoderDecoderBufferTest method testEncoderInputBuffer.
/* Checks for a buffer corruption that happens in ICU
* (CharsetEncoderICU) when an encode operation
* is done first with an in-buffer with hasArray()==true, and next with an in-buffer with
* hasArray()==false. In that situation ICU may overwrite the array of the first in-buffer.
*/
public void testEncoderInputBuffer() {
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
ByteBuffer out = ByteBuffer.wrap(new byte[10]);
char[] inArray = { 'a', 'b' };
CharBuffer inWithArray = CharBuffer.wrap(inArray);
assertTrue(inWithArray.hasArray());
encoder.encode(inWithArray, out, false);
assertEquals('a', inArray[0]);
assertEquals('b', inArray[1]);
CharBuffer inWithoutArray = CharBuffer.wrap("x");
assertFalse(inWithoutArray.hasArray());
encoder.encode(inWithoutArray, out, true);
// check whether the second decode corrupted the first buffer
assertEquals('a', inArray[0]);
assertEquals('b', inArray[1]);
}
use of java.nio.charset.CharsetEncoder in project frostwire by frostwire.
the class EncodedText method stringToUnicodeBytes.
public static byte[] stringToUnicodeBytes(String s, String characterSet) {
Charset charset = Charset.forName(characterSet);
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer byteBuffer;
try {
byteBuffer = encoder.encode(CharBuffer.wrap(s));
return BufferTools.copyBuffer(byteBuffer.array(), 0, byteBuffer.limit());
} catch (CharacterCodingException e) {
return stringToUTF8Bytes(s);
}
}
use of java.nio.charset.CharsetEncoder in project jdk8u_jdk by JetBrains.
the class ZipCoder method encoder.
private CharsetEncoder encoder() {
CharsetEncoder enc = encTL.get();
if (enc == null) {
enc = cs.newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
encTL.set(enc);
}
return enc;
}
use of java.nio.charset.CharsetEncoder in project jdk8u_jdk by JetBrains.
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;
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 intellij-plugins by JetBrains.
the class FlexDefinitionProcessor method parseCPoolAndRename.
private static void parseCPoolAndRename(String from, ByteBuffer buffer) {
buffer.position(buffer.position() + 4);
int n = AbcUtil.readU32(buffer);
while (n-- > 1) {
AbcUtil.readU32(buffer);
}
n = AbcUtil.readU32(buffer);
while (n-- > 1) {
AbcUtil.readU32(buffer);
}
n = AbcUtil.readU32(buffer);
if (n != 0) {
buffer.position(buffer.position() + ((n - 1) * 8));
}
n = AbcUtil.readU32(buffer);
final CharsetEncoder charsetEncoder = StandardCharsets.UTF_8.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
o: while (n-- > 1) {
int l = AbcUtil.readU32(buffer);
buffer.limit(buffer.position() + l);
buffer.mark();
final CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer);
buffer.limit(buffer.capacity());
int index = 0;
do {
index = CharArrayUtil.indexOf(charBuffer, from, index);
if (index == -1 || (index > 0 && !isSpecialSymbol(charBuffer.get(index - 1)))) {
continue o;
}
charBuffer.put(index, OVERLOADED_AND_BACKED_CLASS_MARK);
index += from.length();
} while (index < charBuffer.length());
buffer.reset();
charsetEncoder.encode(charBuffer, buffer, true);
charsetEncoder.reset();
}
}
Aggregations