/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* 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.
*/
package fedora.server.rest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
/**
* This is taken from org.apache.commons.io.CopyUtils so Apache liciense applied
*/
public class CopyUtils {
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
/**
* Copy bytes from an <code>InputStream</code> to an
* <code>OutputStream</code>.
*
* @param input
* the <code>InputStream</code> to read from
* @param output
* the <code>OutputStream</code> to write to
* @return the number of bytes copied
* @throws IOException
* In case of an I/O problem
*/
public static int copy(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
/**
* Copy chars from a <code>Reader</code> to a <code>Writer</code>.
*
* @param input
* the <code>Reader</code> to read from
* @param output
* the <code>Writer</code> to write to
* @return the number of characters copied
* @throws IOException
* In case of an I/O problem
*/
public static int copy(Reader input, Writer output) throws IOException {
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
}