001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library 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 library is distributed in the hope that it will be useful, 011 but 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 library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: StreamUtils.java,v $ 023 Revision 1.2 2004/05/05 21:36:35 markl 024 comment block updates 025 026 Revision 1.1 2004/03/16 06:15:50 markl 027 moved from kiwi.util. 028 029 Revision 1.2 2003/01/19 09:42:39 markl 030 Javadoc & comment header updates. 031 032 Revision 1.1 2001/08/28 21:37:33 markl 033 Split out stream methods into their own class. 034 ---------------------------------------------------------------------------- 035*/ 036 037package kiwi.io; 038 039import java.io.*; 040 041/** This class consists of several convenience routines for reading and 042 * writing streams. The methods are all static. 043 * 044 * @since Kiwi 1.3.1 045 * 046 * @author Mark Lindner 047 */ 048 049public final class StreamUtils 050 { 051 /** The data transfer block size. */ 052 public static final int blockSize = 4096; 053 054 /** Read all of the data from a stream, writing it to another stream. Reads 055 * data from the input stream and writes it to the output stream, until no 056 * more data is available. 057 * 058 * @param input The input stream. 059 * @param output The output stream. 060 * 061 * @exception java.io.IOException If an error occurred while reading from 062 * the stream. 063 */ 064 065 public static final OutputStream readStreamToStream(InputStream input, 066 OutputStream output) 067 throws IOException 068 { 069 byte buf[] = new byte[blockSize]; 070 int b; 071 072 while((b = input.read(buf)) > 0) 073 output.write(buf, 0, b); 074 075 return(output); 076 } 077 078 /** Read all of the data from a stream, returning the contents as a 079 * <code>String</code>. Note that this method is not unicode-aware. 080 * 081 * @param input The stream to read from. 082 * 083 * @return The contents of the stream, as a <code>String</code>. 084 * 085 * @exception java.io.IOException If an error occurred while reading from 086 * the stream. 087 */ 088 089 public static final String readStreamToString(InputStream input) 090 throws IOException 091 { 092 return(readStream(input).toString()); 093 } 094 095 /** Write a string to a stream. Note that this method is not unicode-aware. 096 * 097 * @exception java.io.IOException If an error occurred while writing to the 098 * stream. 099 * 100 * @param s The string to write. 101 * @param output The stream to write it to. 102 */ 103 104 public static final void writeStringToStream(String s, OutputStream output) 105 throws IOException 106 { 107 InputStream input = new ByteArrayInputStream(s.getBytes()); 108 109 readStreamToStream(input, output); 110 } 111 112 /** Read all of the data from a stream, returning the contents as a 113 * <code>byte</code> array. 114 * 115 * @param input The stream to read from. 116 * 117 * @return The contents of the stream, as a <code>byte</code> array. 118 * 119 * @exception java.io.IOException If an error occurred while reading from 120 * the stream. 121 */ 122 123 public static final byte[] readStreamToByteArray(InputStream input) 124 throws IOException 125 { 126 return(readStream(input).toByteArray()); 127 } 128 129 /* Fully read an <code>InputStream</code>, writing the data read to a 130 * <code>ByteArrayOutputStream. Returns a reference to the resulting stream. 131 */ 132 133 private static final ByteArrayOutputStream readStream(InputStream input) 134 throws IOException 135 { 136 ByteArrayOutputStream output = new ByteArrayOutputStream(blockSize); 137 138 readStreamToStream(input, output); 139 140 return(output); 141 } 142 143 } 144 145/* end of source file */