Description
Copy an input stream to an output stream.
License
Apache License
Parameter
Parameter | Description |
---|
bufferSize | the size of the buffer |
progress | the progress observer it could be null |
in | the input stream |
out | the output stream |
canStop | if true, the copy can be stopped by interrupting the thread |
Exception
Parameter | Description |
---|
IOException | IOException If an I/O error occurs |
Return
true
if the copy was done,
false
if it was interrupted
Declaration
public static boolean copyStream(int bufferSize,
InputStream in, OutputStream out, boolean canStop)
throws IOException
Method Source Code
//package com.java2s;
/*//from ww w . j a va 2 s . c o m
* Created on 26.04.2005
*
* Copyright 2005-2006 Daniel Jacobi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import java.io.*;
public class Main {
/**
* Copy an input stream to an output stream. Copied from jEdit Project.
*
* @param bufferSize
* the size of the buffer
* @param progress
* the progress observer it could be null
* @param in
* the input stream
* @param out
* the output stream
* @param canStop
* if true, the copy can be stopped by interrupting the thread
* @return <code>true</code> if the copy was done, <code>false</code> if
* it was interrupted
* @throws IOException
* IOException If an I/O error occurs
*/
public static boolean copyStream(int bufferSize, /*ProgressObserver progress,*/
InputStream in, OutputStream out, boolean canStop)
throws IOException {
byte[] buffer = new byte[bufferSize];
int n;
long copied = 0L;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
copied += n;
// if (progress != null)
// progress.setValue(copied);
if (canStop && Thread.interrupted())
return false;
}
return true;
}
/**
* Copy an input stream to an output stream with a buffer of 4096 bytes.
*
* @param progress
* the progress observer it could be null
* @param in
* the input stream
* @param out
* the output stream
* @param canStop
* if true, the copy can be stopped by interrupting the thread
* @return <code>true</code> if the copy was done, <code>false</code> if
* it was interrupted
* @throws IOException
* IOException If an I/O error occurs
*/
public static boolean copyStream(
/*ProgressObserver progress,*/InputStream in, OutputStream out,
boolean canStop) throws IOException {
return copyStream(4096, /*progress,*/in, out, canStop);
}
}
Related
- copyStream(InputStream src, OutputStream dest)
- copyStream(InputStream src, OutputStream dest)
- copyStream(InputStream src, OutputStream dest, boolean closeStreams, String title)
- copyStream(InputStream src, OutputStream osstream)
- CopyStream(InputStream sSource, OutputStream sTarget)
- copyStream(java.io.InputStream in, java.io.OutputStream out)
- copyStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)
- copyStream(java.io.InputStream src, java.io.OutputStream dest)
- copyStream(OutputStream out, InputStream in, int bufsz)