use of java.nio.charset.CoderResult 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.CoderResult in project checker-framework by typetools.
the class ZipCoder method toString.
String toString(byte[] ba, int length) {
CharsetDecoder cd = decoder().reset();
int len = (int) (length * cd.maxCharsPerByte());
char[] ca = new char[len];
if (len == 0)
return new String(ca);
// REPORT mode.
if (isUTF8 && cd instanceof ArrayDecoder) {
int clen = ((ArrayDecoder) cd).decode(ba, 0, length, ca);
if (// malformed
clen == -1)
throw new IllegalArgumentException("MALFORMED");
return new String(ca, 0, clen);
}
ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
CharBuffer cb = CharBuffer.wrap(ca);
CoderResult cr = cd.decode(bb, cb, true);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
cr = cd.flush(cb);
if (!cr.isUnderflow())
throw new IllegalArgumentException(cr.toString());
return new String(ca, 0, cb.position());
}
use of java.nio.charset.CoderResult in project SimpleFlatMapper by arnaudroger.
the class CharReadingBenchmark method memoryMappedFile.
@Benchmark
public void memoryMappedFile(Blackhole blackhole) throws IOException {
char[] buffer = new char[bufferSize];
try (FileChannel channel = FileChannel.open(file.toPath())) {
MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
CharsetDecoder charsetDecoder = charset.newDecoder();
while (byteBuffer.hasRemaining()) {
CoderResult coderResult = charsetDecoder.decode(byteBuffer, CharBuffer.wrap(buffer), true);
blackhole.consume(buffer);
}
}
}
use of java.nio.charset.CoderResult 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.CoderResult in project liferay-ide by liferay.
the class BndProperties method _convert.
private String _convert(byte[] buffer, Charset charset) throws IOException {
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer bb = ByteBuffer.wrap(buffer);
CharBuffer cb = CharBuffer.allocate(buffer.length * 4);
CoderResult result = decoder.decode(bb, cb, true);
if (!result.isError()) {
return new String(cb.array(), 0, cb.position());
}
throw new CharacterCodingException();
}
Aggregations