use of java.nio.charset.CharsetDecoder in project kripton by xcesco.
the class TestRuntimeGit80 method readMessage.
private Message readMessage(BinderContext binder, String fileName, CodingErrorAction report) throws FileNotFoundException {
File input = new File(fileName);
CharsetDecoder utf8Decoder = StandardCharsets.UTF_8.newDecoder();
utf8Decoder.onMalformedInput(report);
utf8Decoder.onUnmappableCharacter(report);
InputStreamReader targetStream = new InputStreamReader(new FileInputStream(input), utf8Decoder);
return binder.parse(targetStream, Message.class);
}
use of java.nio.charset.CharsetDecoder in project apm-agent-java by elastic.
the class IOUtils method readUtf8Stream.
/**
* Reads the provided {@link InputStream} into the {@link CharBuffer} without causing allocations.
* <p>
* The {@link InputStream} is assumed to yield an UTF-8 encoded string.
* </p>
* <p>
* If the {@link InputStream} yields more chars than the {@link CharBuffer#limit()} of the provided {@link CharBuffer},
* the rest of the input is silently ignored.
* </p>
*
* @param is the source {@link InputStream}, which should be encoded with UTF-8.
* @param charBuffer the {@link CharBuffer} the {@link InputStream} should be written into
* @return {@code true}, if the input stream could be decoded with the UTF-8 charset, {@code false} otherwise.
* @throws IOException in case of errors reading from the provided {@link InputStream}
*/
public static boolean readUtf8Stream(final InputStream is, final CharBuffer charBuffer) throws IOException {
// to be compatible with Java 8, we have to cast to buffer because of different return types
final ByteBuffer buffer = threadLocalByteBuffer.get();
final CharsetDecoder charsetDecoder = threadLocalCharsetDecoder.get();
try {
final byte[] bufferArray = buffer.array();
for (int read = is.read(bufferArray); read != -1; read = is.read(bufferArray)) {
((Buffer) buffer).limit(read);
final CoderResult coderResult = charsetDecoder.decode(buffer, charBuffer, true);
((Buffer) buffer).clear();
if (coderResult.isError()) {
// this is not UTF-8
((Buffer) charBuffer).clear();
return false;
} else if (coderResult.isOverflow()) {
// stream yields more chars than the charBuffer can hold
break;
}
}
charsetDecoder.flush(charBuffer);
return true;
} finally {
((Buffer) charBuffer).flip();
((Buffer) buffer).clear();
charsetDecoder.reset();
is.close();
}
}
use of java.nio.charset.CharsetDecoder in project aion by aionnetwork.
the class Slices method decodeString.
public static String decodeString(ByteBuffer src, Charset charset) {
CharsetDecoder decoder = getDecoder(charset);
CharBuffer dst = CharBuffer.allocate((int) ((double) src.remaining() * decoder.maxCharsPerByte()));
try {
CoderResult cr = decoder.decode(src, dst, true);
if (!cr.isUnderflow()) {
cr.throwException();
}
cr = decoder.flush(dst);
if (!cr.isUnderflow()) {
cr.throwException();
}
} catch (CharacterCodingException x) {
throw new IllegalStateException(x);
}
return dst.flip().toString();
}
use of java.nio.charset.CharsetDecoder in project java-runtime-decompiler by pmikova.
the class AbstractAgentNeedingTest method readBinaryAsString.
public static String readBinaryAsString(FileInputStream input, String charBase, CodingErrorAction action) throws IOException {
CharsetDecoder decoder = Charset.forName(charBase).newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
InputStreamReader reader = new InputStreamReader(input, decoder);
StringBuilder sb = new StringBuilder();
while (true) {
int i = reader.read();
if (i < 0) {
break;
}
sb.append((char) i);
}
reader.close();
return sb.toString();
}
use of java.nio.charset.CharsetDecoder in project appengine-gcs-client by GoogleCloudPlatform.
the class GcsInputChannelTest method runTest.
private String runTest(GcsInputChannel channel, int readSize) throws IOException {
final StringBuffer contents = new StringBuffer();
try {
final ByteBuffer buffer = ByteBuffer.allocateDirect(readSize);
int read = 0;
final CharsetDecoder decoder = UTF_8.newDecoder();
while (read >= 0) {
read = channel.read(buffer);
buffer.flip();
contents.append(decoder.decode(buffer));
buffer.rewind();
buffer.limit(buffer.capacity());
}
} finally {
channel.close();
}
return contents.toString();
}
Aggregations