001 /* JAPI - (Yet another (hopefully) useful) Java API 002 * 003 * Copyright (C) 2004-2006 Christian Hujer 004 * 005 * This program is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU General Public License as 007 * published by the Free Software Foundation; either version 2 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 * 02111-1307, USA. 019 */ 020 021 package net.sf.japi.io; 022 023 import java.io.InputStream; 024 import java.io.IOException; 025 import java.io.OutputStream; 026 027 /** A class with helper methods for In- and Output. 028 * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a> 029 */ 030 public class IOHelper { 031 032 /** Default buffer size. */ 033 public static final int DEFAULT_BUF_SIZE = 8192; 034 035 /** Copies all bytes from one stream to another using a default buffer size (see {@link #DEFAULT_BUF_SIZE}). 036 * @param in InputStream source 037 * @param out OutputStream destination 038 * @throws IOException on i/o problems 039 */ 040 public static void copy(final InputStream in, final OutputStream out) throws IOException { 041 copy(in, out, DEFAULT_BUF_SIZE); 042 } 043 044 /** Copies all bytes from one stream to another using a specified buffer size. 045 * @param in InputStream source 046 * @param out OutputStream destination 047 * @param size buffer size in bytes 048 * @throws IOException on i/o problems 049 */ 050 public static void copy(final InputStream in, final OutputStream out, final int size) throws IOException { 051 final byte[] buf = new byte[size]; 052 int bytesRead; 053 while ((bytesRead = in.read(buf)) != -1) { 054 out.write(buf, 0, bytesRead); 055 } 056 out.flush(); 057 } 058 059 /** Copies all bytes from one stream to another using a bytewise copy. 060 * @param in InputStream source 061 * @param out OutputStream destination 062 * @throws IOException on i/o problems 063 */ 064 public static void copyBW(final InputStream in, final OutputStream out) throws IOException { 065 int byteRead; 066 while ((byteRead = in.read()) != -1) { 067 out.write(byteRead); 068 } 069 } 070 071 /** private constructor, this class shall not be instantiated because it does not contain any instance information. */ 072 private IOHelper() { } 073 074 } // class IOHelper