From: David ‘Bombe’ Roden Date: Sat, 5 Apr 2008 21:04:46 +0000 (+0000) Subject: add improved stream copier X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a689a44df847282b2ea40e917f178976e7c80682;p=jSite2.git add improved stream copier git-svn-id: http://trooper/svn/projects/jSite/trunk@616 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/util/io/StreamCopier.java b/src/net/pterodactylus/util/io/StreamCopier.java new file mode 100644 index 0000000..d1d0b11 --- /dev/null +++ b/src/net/pterodactylus/util/io/StreamCopier.java @@ -0,0 +1,124 @@ +/* + * jSite2 - StreamCopier.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.util.io; + +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Helper class that copies bytes from an {@link InputStream} to an + * {@link OutputStream}. + * + * @author David Roden + * @version $Id$ + */ +public class StreamCopier { + + /** Default buffer size is 64k. */ + private static final int DEFAULT_BUFFER_SIZE = 1 << 16; + + /** The current buffer size. */ + private static int bufferSize = DEFAULT_BUFFER_SIZE; + + /** + * Sets the buffer size for following transfers. + * + * @param bufferSize + * The new buffer size + */ + public static void setBufferSize(int bufferSize) { + StreamCopier.bufferSize = bufferSize; + } + + /** + * Copies length bytes from the source input stream to the + * destination output stream. If length is -1 + * as much bytes as possible will be copied (i.e. until + * {@link InputStream#read()} returns -1 to signal the end of + * the stream). + * + * @param source + * The input stream to read from + * @param destination + * The output stream to write to + * @param length + * The number of bytes to copy + * @throws IOException + * if an I/O error occurs + */ + public static void copy(InputStream source, OutputStream destination, long length) throws IOException { + long remaining = length; + byte[] buffer = new byte[bufferSize]; + int read = 0; + while ((remaining == -1) || (remaining > 0)) { + read = source.read(buffer, 0, ((remaining > bufferSize) || (remaining == -1)) ? bufferSize : (int) remaining); + if (read == -1) { + if (length == -1) { + return; + } + throw new EOFException("stream reached eof"); + } + destination.write(buffer, 0, read); + remaining -= read; + } + } + + /** + * Copies as much bytes as possible (i.e. until {@link InputStream#read()} + * returns -1) from the source input stream to the + * destination output stream. + * + * @param source + * The input stream to read from + * @param destination + * The output stream to write to + * @throws IOException + * if an I/O error occurs + */ + public static void copy(InputStream source, OutputStream destination) throws IOException { + copy(source, destination, -1); + } + + /** + * Finds the length of the input stream by reading until + * {@link InputStream#read(byte[])} returns -1. + * + * @param source + * The input stream to measure + * @return The length of the input stream in bytes + * @throws IOException + * if an I/O error occurs + */ + public static long findLength(InputStream source) throws IOException { + long length = 0; + byte[] buffer = new byte[bufferSize]; + int read = 0; + while (read != -1) { + read = source.read(buffer); + if (read != -1) { + length += read; + } + } + return length; + } + +}