Java InputStream Copy nio copy(InputStream in, OutputStream out)

Here you can find the source of copy(InputStream in, OutputStream out)

Description

copy

License

Apache License

Declaration

public static void copy(InputStream in, OutputStream out) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    private static final int IO_BUFFER_SIZE = 8 * 1024;

    public static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] b = new byte[IO_BUFFER_SIZE];
        int read;
        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);//from  w  ww  .  j  a  v  a 2s  .c  o m
        }
    }

    public static byte[] read(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        copy(in, out);
        out.close();

        return out.toByteArray();
    }

    public static void write(Path path, String string) throws IOException {
        PrintWriter out = null;
        try {
            out = new PrintWriter(Files.newOutputStream(path));
            out.write(string);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}

Related

  1. copy(InputStream in, File f, long startAt)
  2. copy(InputStream in, OutputStream out)
  3. copy(InputStream in, OutputStream out)
  4. copy(InputStream in, OutputStream out)
  5. copy(InputStream in, OutputStream out)