Search in sources :

Example 96 with CharsetDecoder

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);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 97 with CharsetDecoder

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();
    }
}
Also used : CharBuffer(java.nio.CharBuffer) Buffer(java.nio.Buffer) ByteBuffer(java.nio.ByteBuffer) CharsetDecoder(java.nio.charset.CharsetDecoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 98 with CharsetDecoder

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 99 with CharsetDecoder

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) InputStreamReader(java.io.InputStreamReader)

Example 100 with CharsetDecoder

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();
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetDecoder (java.nio.charset.CharsetDecoder)332 CharBuffer (java.nio.CharBuffer)156 ByteBuffer (java.nio.ByteBuffer)136 Charset (java.nio.charset.Charset)88 CharacterCodingException (java.nio.charset.CharacterCodingException)84 CoderResult (java.nio.charset.CoderResult)67 IOException (java.io.IOException)38 InputStreamReader (java.io.InputStreamReader)38 BufferedReader (java.io.BufferedReader)22 Reader (java.io.Reader)21 FileInputStream (java.io.FileInputStream)16 CharsetEncoder (java.nio.charset.CharsetEncoder)14 Test (org.junit.Test)12 FileChannel (java.nio.channels.FileChannel)11 InputStream (java.io.InputStream)10 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)10 Buffer (java.nio.Buffer)9 Test (org.junit.jupiter.api.Test)9 ArrayDecoder (sun.nio.cs.ArrayDecoder)9 MalformedInputException (java.nio.charset.MalformedInputException)8