Java File Read via ByteBuffer copyStream(BufferedReader reader, BufferedWriter writer)

Here you can find the source of copyStream(BufferedReader reader, BufferedWriter writer)

Description

copy Stream

License

Apache License

Declaration

public static void copyStream(BufferedReader reader, BufferedWriter writer) throws IOException 

Method Source Code


//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.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    public static void copyStream(BufferedReader reader, BufferedWriter writer) throws IOException {
        String line = null;//from  w  w w  .ja v a2s  .  c  o m
        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);
    }
}

Related

  1. ClientReadWithBlock(SocketChannel sc)
  2. ClientReadWithWait(SocketChannel sc)
  3. copy(final ReadableByteChannel src, final WritableByteChannel dest)
  4. copy(final ReadableByteChannel srcChannel, final WritableByteChannel destChannel)
  5. copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)
  6. count(final ReadableByteChannel src)
  7. getReadBuffer(File file)
  8. getStringContents(ReadableByteChannel channel)
  9. largeFileReader(String filename)