001package com.hfg.util.io;
002
003import java.io.*;
004
005//------------------------------------------------------------------------------
006/**
007 Stream utility functions.
008 <div>
009  @author J. Alex Taylor, hairyfatguy.com
010 </div>
011 */
012//------------------------------------------------------------------------------
013// com.hfg XML/HTML Coding Library
014//
015// This library is free software; you can redistribute it and/or
016// modify it under the terms of the GNU Lesser General Public
017// License as published by the Free Software Foundation; either
018// version 2.1 of the License, or (at your option) any later version.
019//
020// This library is distributed in the hope that it will be useful,
021// but WITHOUT ANY WARRANTY; without even the implied warranty of
022// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
023// Lesser General Public License for more details.
024//
025// You should have received a copy of the GNU Lesser General Public
026// License along with this library; if not, write to the Free Software
027// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
028//
029// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
030// jataylor@hairyfatguy.com
031//------------------------------------------------------------------------------
032
033public class StreamUtil
034{
035   //##########################################################################
036   // PUBLIC FUNCTIONS
037   //##########################################################################
038
039   //---------------------------------------------------------------------------
040   /**
041    Reads the contents of the specified InputStream into a String.
042    @param inStream the source InputStream
043    @throws IOException
044    */
045   public static String inputStreamToString(InputStream inStream)
046   throws IOException
047   {
048      StringBuilder buffer = new StringBuilder();
049
050      Reader reader = new InputStreamReader(inStream);
051      char[] bytes = new char[8 * 1024];
052      int bytesRead = 0;
053      while ((bytesRead = reader.read(bytes)) > 0)
054      {
055         buffer.append(bytes, 0, bytesRead);
056      }
057
058      reader.close();
059
060      return buffer.toString();
061   }
062
063   //---------------------------------------------------------------------------
064   /**
065    Reads the contents of the specified Reader into a String.
066    @param inReader the source Reader
067    @throws IOException
068    */
069   public static String readerToString(Reader inReader)
070   throws IOException
071   {
072      StringBuilder buffer = new StringBuilder();
073
074      char[] bytes = new char[8 * 1024];
075      int bytesRead = 0;
076      while ((bytesRead = inReader.read(bytes)) > 0)
077      {
078         buffer.append(bytes, 0, bytesRead);
079      }
080
081      inReader.close();
082
083      return buffer.toString();
084   }
085
086   //---------------------------------------------------------------------------
087   /**
088    Convenience method that does a null check before closing the stream.
089    @param inStream the InputStream to be closed
090    @throws RuntimeIOException
091    */
092   public static void close(InputStream inStream)
093         throws RuntimeIOException
094   {
095      if (inStream != null)
096      {
097         try
098         {
099            inStream.close();
100         }
101         catch (IOException e)
102         {
103            throw new RuntimeIOException("Problem closing the Stream!", e);
104         }
105      }
106   }
107
108   //---------------------------------------------------------------------------
109   /**
110    Convenience method that does a null check before closing the stream.
111    @param inStream the OutputStream to be closed
112    @throws RuntimeIOException
113    */
114   public static void close(OutputStream inStream)
115         throws RuntimeIOException
116   {
117      if (inStream != null)
118      {
119         try
120         {
121            inStream.close();
122         }
123         catch (IOException e)
124         {
125            throw new RuntimeIOException("Problem closing the Stream!", e);
126         }
127      }
128   }
129
130   //---------------------------------------------------------------------------
131   /**
132    Convenience function that does a null check before closing the Reader and
133    wrapps IOExceptions in RuntimeIOExceptions so they don't have to be caught.
134    @param inReader the Reader to be closed
135    @throws RuntimeIOException
136    */
137   public static void close(Reader inReader)
138         throws RuntimeIOException
139   {
140      if (inReader != null)
141      {
142         try
143         {
144            inReader.close();
145         }
146         catch (IOException e)
147         {
148            throw new RuntimeIOException("Problem closing the Reader!", e);
149         }
150      }
151   }
152
153   //---------------------------------------------------------------------------
154   /**
155    Convenience function that does a null check before closing the Writer and
156    wrapps IOExceptions in RuntimeIOExceptions so they don't have to be caught.
157    @param inWriter the Writer to be closed
158    @throws RuntimeIOException
159    */
160   public static void close(Writer inWriter)
161         throws RuntimeIOException
162   {
163      if (inWriter != null)
164      {
165         try
166         {
167            inWriter.close();
168         }
169         catch (IOException e)
170         {
171            throw new RuntimeIOException("Problem closing the Writer!", e);
172         }
173      }
174   }
175
176   //---------------------------------------------------------------------------
177   /**
178    Writes the contents of the specified InputStream into the specified File.
179    @param inStream the source InputStream
180    @param inFile the destination File for the stream content
181    @throws IOException
182    */
183   public static void writeToFile(InputStream inStream, File inFile)
184   throws IOException
185   {
186      BufferedOutputStream outStream = null;
187      try
188      {
189         outStream = new BufferedOutputStream(new FileOutputStream(inFile));
190
191         writeToStream(inStream, outStream);
192      }
193      finally
194      {
195         close(outStream);
196      }
197   }
198
199   //---------------------------------------------------------------------------
200   /**
201    Writes the contents of the specified InputStream into the specified OutputStream.
202    @param inStream the source InputStream
203    @param inOutStream the destination OutputStream
204    @throws IOException
205    */
206   public static void writeToStream(InputStream inStream, OutputStream inOutStream)
207         throws IOException
208   {
209      try
210      {
211         byte[] bytes = new byte[8 * 1024];
212         int bytesRead = 0;
213         while ((bytesRead = inStream.read(bytes)) > 0)
214         {
215            inOutStream.write(bytes, 0, bytesRead);
216         }
217      }
218      finally
219      {
220         close(inStream);
221      }
222   }
223
224   //---------------------------------------------------------------------------
225   /**
226    Reads bytes from the specified InputStream into the specified byte[].
227    @param inStream the source InputStream for the bytes
228    @param inByteArray the byte[] to be filled
229    @return the number of bytes read
230    @throws IOException
231    */
232   public static int fillByteArray(InputStream inStream, byte[] inByteArray)
233         throws IOException
234   {
235      int position = 0;
236      while (position < inByteArray.length)
237      {
238         int bytesRead = inStream.read(inByteArray, position, inByteArray.length - position);
239         if (-1 == bytesRead)
240         {
241            break;
242         }
243         else
244         {
245            position += bytesRead;
246         }
247      }
248
249      return position;
250   }
251}