Description
The base for all other methods in this class.
License
Open Source License
Parameter
Parameter | Description |
---|
from | The source stream |
to | The destination stream |
blockSize | The size of the buffer to be used |
Exception
Parameter | Description |
---|
IOException | See InputStream#read(byte[]) andOutputStream#write(byte[],int,int). |
Declaration
public static void copyStreams(InputStream from, OutputStream to, int blockSize) throws IOException
Method Source Code
//package com.java2s;
/*/* w w w . j a v a2 s . c om*/
* The MIT License (MIT)
*
* Copyright (c) 2014 Nelson Crosby
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import java.io.*;
public class Main {
/**
* The default block size in bytes.
* Given a method {@code streamAction(...)} whose parameter list does
* not end with {@code int blockSize}, wrappers are provided that call
* {@code streamAction(..., DEFAULT_BLOCK_SIZE)}.
*/
public static final int DEFAULT_BLOCK_SIZE = 1024;
/**
* The base for all other methods in this class.
*
* It copies all remaining bytes until the EOF from {@code from} to
* {@code to}. This is done in buffers of {@code blockSize}
* bytes.
*
* This method closes both files at the end.
*
* @param from The source stream
* @param to The destination stream
* @param blockSize The size of the buffer to be used
* @throws IOException See {@link InputStream#read(byte[])} and
* {@link OutputStream#write(byte[], int, int)}.
*/
public static void copyStreams(InputStream from, OutputStream to, int blockSize) throws IOException {
byte[] buffer = new byte[blockSize];
int bytesRead;
while ((bytesRead = from
.read(buffer) /* Read bytes into buffer */) != -1 /* Check that EOF not reached */) {
// Write buffer into destination
to.write(buffer, 0, bytesRead);
}
from.close();
to.close();
}
/**
* Wrapper for {@link #copyStreams} using {@value #DEFAULT_BLOCK_SIZE}.
*
* @param from The source stream
* @param to The destination stream
* @throws IOException
* @see #copyStreams
*/
public static void copyStreams(InputStream from, OutputStream to) throws IOException {
copyStreams(from, to, DEFAULT_BLOCK_SIZE);
}
}
Related
- copyStreamIoe(final InputStream is, final OutputStream os)
- copyStreamNoClose(InputStream in, OutputStream out)
- copyStreamPortion(java.io.InputStream inputStream, java.io.OutputStream outputStream, int portionSize, int bufferSize)
- copyStreams(final InputStream in, final OutputStream out)
- copyStreams(final InputStream input, final OutputStream output)
- copyStreams(InputStream in, OutputStream out)
- copyStreams(InputStream in, OutputStream out)
- copyStreams(InputStream in, OutputStream out)
- copyStreams(InputStream in, OutputStream out, int buf)