All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.io.StreamUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.io;

import java.io.*;

//------------------------------------------------------------------------------
/**
 Stream utility functions.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library 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 // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class StreamUtil { //########################################################################## // PUBLIC FUNCTIONS //########################################################################## //--------------------------------------------------------------------------- /** Reads the contents of the specified InputStream into a String. @param inStream the source InputStream @throws IOException */ public static String inputStreamToString(InputStream inStream) throws IOException { StringBuilder buffer = new StringBuilder(); Reader reader = new InputStreamReader(inStream); char[] bytes = new char[8 * 1024]; int bytesRead = 0; while ((bytesRead = reader.read(bytes)) > 0) { buffer.append(bytes, 0, bytesRead); } reader.close(); return buffer.toString(); } //--------------------------------------------------------------------------- /** Convenience method that does a null check before closing the stream. @param inStream the InputStream to be closed @throws RuntimeIOException */ public static void close(InputStream inStream) throws RuntimeIOException { if (inStream != null) { try { inStream.close(); } catch (IOException e) { throw new RuntimeIOException("Problem closing the Stream!", e); } } } //--------------------------------------------------------------------------- /** Convenience method that does a null check before closing the stream. @param inStream the OutputStream to be closed @throws RuntimeIOException */ public static void close(OutputStream inStream) throws RuntimeIOException { if (inStream != null) { try { inStream.close(); } catch (IOException e) { throw new RuntimeIOException("Problem closing the Stream!", e); } } } //--------------------------------------------------------------------------- /** Convenience function that does a null check before closing the Reader and wrapps IOExceptions in RuntimeIOExceptions so they don't have to be caught. @param inReader the Reader to be closed @throws RuntimeIOException */ public static void close(Reader inReader) throws RuntimeIOException { if (inReader != null) { try { inReader.close(); } catch (IOException e) { throw new RuntimeIOException("Problem closing the Reader!", e); } } } //--------------------------------------------------------------------------- /** Convenience function that does a null check before closing the Writer and wrapps IOExceptions in RuntimeIOExceptions so they don't have to be caught. @param inWriter the Writer to be closed @throws RuntimeIOException */ public static void close(Writer inWriter) throws RuntimeIOException { if (inWriter != null) { try { inWriter.close(); } catch (IOException e) { throw new RuntimeIOException("Problem closing the Writer!", e); } } } //--------------------------------------------------------------------------- /** Writes the contents of the specified InputStream into the specified File. @param inStream the source InputStream @param inFile the destination File for the stream content @throws IOException */ public static void writeToFile(InputStream inStream, File inFile) throws IOException { BufferedOutputStream outStream = null; try { outStream = new BufferedOutputStream(new FileOutputStream(inFile)); writeToStream(inStream, outStream); } finally { close(outStream); } } //--------------------------------------------------------------------------- /** Writes the contents of the specified InputStream into the specified OutputStream. @param inStream the source InputStream @param inOutStream the destination OutputStream @throws IOException */ public static void writeToStream(InputStream inStream, OutputStream inOutStream) throws IOException { try { byte[] bytes = new byte[8 * 1024]; int bytesRead = 0; while ((bytesRead = inStream.read(bytes)) > 0) { inOutStream.write(bytes, 0, bytesRead); } } finally { close(inStream); } } //--------------------------------------------------------------------------- /** Reads bytes from the specified InputStream into the specified byte[]. @param inStream the source InputStream for the bytes @param inByteArray the byte[] to be filled @return the number of bytes read @throws IOException */ public static int fillByteArray(InputStream inStream, byte[] inByteArray) throws IOException { int position = 0; while (position < inByteArray.length) { int bytesRead = inStream.read(inByteArray, position, inByteArray.length - position); if (-1 == bytesRead) { break; } else { position += bytesRead; } } return position; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy