Here you can find the source of toString(Reader reader)
public static String toString(Reader reader) throws IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.StringWriter; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; public class Main { public static String toString(Reader reader) throws IOException { StringWriter sw = new StringWriter(); copyStream(reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader), new BufferedWriter(sw)); return sw.toString(); }//from w ww. j a va 2 s .co m public static void copyStream(BufferedReader reader, BufferedWriter writer) throws IOException { String line = null; while ((line = reader.readLine()) != null) { writer.write(line); } writer.flush(); } public static void copyStream(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { buffer.flip(); dest.write(buffer); buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } } public static void copyStream(InputStream in, OutputStream out) throws IOException { final ReadableByteChannel inputChannel = Channels.newChannel(in); final WritableByteChannel outputChannel = Channels.newChannel(out); copyStream(inputChannel, outputChannel); } }