Java Utililty Methods InputStream Copy nio

List of utility methods to do InputStream Copy nio

Description

The list of methods to do InputStream Copy nio are organized into topic(s).

Method

voidcopy(InputStream input, OutputStream output)
copy
byte[] buf = new byte[BUFFER_SIZE];
try {
    int len;
    while ((len = input.read(buf)) != -1) {
        output.write(buf, 0, len);
} catch (IOException e) {
    throw new IllegalStateException(e);
...
voidcopy(InputStream src, File dst)
Copy the content of a stream to a destination file
if (src == null)
    throw new Exception("Invalid source stream");
if (dst == null)
    throw new Exception("Invalid destination file");
BufferedInputStream bis = new BufferedInputStream(src);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dst));
int ch = -1;
while ((ch = bis.read()) != -1) {
...
voidcopy(InputStream stream, File file)
copy
Files.copy(stream, Paths.get(file.toURI()), StandardCopyOption.REPLACE_EXISTING);
voidcopyStream(final InputStream aInStream, final OutputStream aOutStream)
Writes from an input stream to an output stream; you're responsible for closing the InputStream and OutputStream.
final BufferedOutputStream outStream = new BufferedOutputStream(aOutStream);
final BufferedInputStream inStream = new BufferedInputStream(aInStream);
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
int bytesRead = 0;
while (true) {
    bytesRead = inStream.read(buffer);
    if (bytesRead == -1) {
...
InputStreamcopyStream(final InputStream in)
copy Stream
final byte[] bs = streamToByteArray(in);
return new ByteArrayInputStream(bs);
longcopyStream(final InputStream is, final OutputStream os)
copy Stream
ReadableByteChannel rbc = Channels.newChannel(is);
WritableByteChannel wbc = Channels.newChannel(os);
int bytesWritten = 0;
final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
while (rbc.read(buffer) != -1) {
    buffer.flip();
    bytesWritten += wbc.write(buffer);
    buffer.compact();
...
voidcopyStream(InputStream in, OutputStream out)
copy Stream
ReadableByteChannel src = Channels.newChannel(in);
WritableByteChannel dest = Channels.newChannel(out);
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);
voidcopyStream(InputStream input, OutputStream output)
Copia a stream output para a stream output, fechando as duas streans ao final do processo de copia.
final ReadableByteChannel inputChannel = Channels.newChannel(input);
final WritableByteChannel outputChannel = Channels.newChannel(output);
fastChannelCopy(inputChannel, outputChannel);
inputChannel.close();
outputChannel.close();
voidcopyStream(InputStream src, OutputStream dest)
copy Stream
ReadableByteChannel in = Channels.newChannel(src);
WritableByteChannel out = Channels.newChannel(dest);
copyChannel(in, out);
in.close();
out.close();