Here you can find the source of getStringContents(ReadableByteChannel channel)
public static String getStringContents(ReadableByteChannel channel) throws IOException
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.charset.StandardCharsets; public class Main { /** Gets String contents from channel and closes it. */ public static String getStringContents(ReadableByteChannel channel) throws IOException { // TODO Checks if a supplier would be nice try {//from w ww .jav a2 s .com ByteBuffer buffer = ByteBuffer.allocate(1024 * 8); StringBuilder sb = new StringBuilder(); int bytesRead = channel.read(buffer); while (bytesRead != -1) { buffer.flip(); CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer); sb.append(charBuffer.toString()); buffer.clear(); bytesRead = channel.read(buffer); } return sb.toString(); } finally { channel.close(); } } }