How to read unicode text data using NIO from a SocketChannel

Please note data.compact(); at the bottom of the method readUTF8(). This will save you from loosing available bytes that were not decoded in previous iteration. I hope the following example code to read large text data using NIO will help beginners to NIO.

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;

public class NIOTextReader {
    public static String readUTF8(SocketChannel sc) 
                throws IOException {
        StringBuffer textRead = new StringBuffer();
        final Charset utf8 = Charset.forName("UTF-8");
        CharsetDecoder decoder = utf8.newDecoder();
        decoder.reset();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);

        final ByteBuffer data = ByteBuffer.allocate(1024);
        final CharBuffer chBuffer = CharBuffer.allocate(1024);

        while (true) {
            // read the data into the char buffer
            int r = sc.read(data);

            // check for end of data
            if (r == -1) {
                break;
            }

            // prepares the data for decoding/read
            data.flip();

            CoderResult result = null;
            do {
                // Decode byte data into Character data
                result = decoder.decode(data, chBuffer, false);
                chBuffer.flip();
                textRead.append(chBuffer);
                chBuffer.clear();
                if (result.isUnderflow()) {
                    break;
                }
            } while (result.isOverflow());

            // moves the remaining bytes to the beginning that were
            // not decoded
            data.compact();
        }

        return textRead.toString();
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: