Java File Read via ByteBuffer read(Reader reader)

Here you can find the source of read(Reader reader)

Description

read

License

Open Source License

Declaration

public static char[] read(Reader reader) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Main {
    public static byte[] read(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream(in.available());
        transfer(in, out);// w  w  w .j  a  va 2  s.c  o  m
        return out.toByteArray();
    }

    public static char[] read(Reader reader) throws IOException {
        CharArrayWriter writer = new CharArrayWriter();
        transfer(reader, writer);
        return writer.toCharArray();
    }

    public static void transfer(InputStream in, OutputStream out) throws IOException {
        transfer(in, out, -1L);
    }

    public static void transfer(InputStream in, OutputStream out, long limit) throws IOException {
        byte buf[] = new byte[1024];
        long total = 0L;
        do {
            int readcount;
            if ((readcount = in.read(buf)) == -1)
                break;
            if (limit != -1L && total + (long) readcount > limit)
                readcount = (int) (limit - total);
            if (readcount > 0)
                out.write(buf, 0, readcount);
            total += readcount;
        } while (limit == -1L || total < limit);
        out.flush();
    }

    public static void transfer(InputStream in, SocketChannel out) throws IOException {
        byte buf[] = new byte[1024];
        ByteBuffer bbuf = ByteBuffer.allocate(1024);
        int len;
        while ((len = in.read(buf)) != -1) {
            bbuf.put(buf, 0, len);
            bbuf.flip();
            for (; bbuf.remaining() > 0; out.write(bbuf))
                ;
            bbuf.clear();
        }
    }

    public static void transfer(Reader reader, Writer writer) throws IOException {
        transfer(reader, writer, -1L);
    }

    public static void transfer(Reader reader, Writer writer, long limit) throws IOException {
        char buf[] = new char[1024];
        long total = 0L;
        do {
            int readcount;
            if ((readcount = reader.read(buf)) == -1)
                break;
            if (limit != -1L && total + (long) readcount > limit)
                readcount = (int) (limit - total);
            if (readcount > 0)
                writer.write(buf, 0, readcount);
            total += readcount;
        } while (limit == -1L || total < limit);
        writer.flush();
    }
}

Related

  1. read(final FileChannel channel)
  2. read(final Reader input, final char[] buffer, final int offset, final int length)
  3. read(InputStream stream)
  4. read(Path file)
  5. read(Path file, int pos, int length)
  6. read24BitInteger(byte[] threeBytes, ByteOrder order)
  7. read_file(File input)
  8. readAiffHeader(DataInputStream inStrm, FileChannel fc)
  9. readAndTransfer(final String dir)