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.*;
import java.util.ArrayList;
import java.util.List;

//------------------------------------------------------------------------------
/**
 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 { private static final char STX = 2; private static final char ETX = 3; //########################################################################## // PUBLIC FUNCTIONS //########################################################################## //--------------------------------------------------------------------------- /** Reads the contents of the specified InputStream into a byte[]. @param inStream the source InputStream @throws IOException */ public static byte[] inputStreamToBytes(InputStream inStream) throws IOException { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); BufferedOutputStream outStream = null; try { outStream = new BufferedOutputStream(byteStream); writeToStream(inStream, outStream); } finally { close(outStream); } return byteStream.toByteArray(); } //--------------------------------------------------------------------------- /** 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(); } //--------------------------------------------------------------------------- /** Reads the contents of an STX/ETX-framed packet. @param inStream the source InputStream @throws IOException */ public static String readSTX_ETX_Packet(InputStream inStream) throws IOException { StringBuilder buffer = new StringBuilder(); int byteRead; int length = 0; while ((byteRead = inStream.read()) > 0) { if (ETX == byteRead) { break; } else if (0 == length) { if (byteRead != STX) { throw new RuntimeIOException("No STX char found at the start of the packet!"); } } else { buffer.append((char) byteRead); } length++; } return buffer.toString(); } //--------------------------------------------------------------------------- /** Reads the contents of STX/ETX-framed packets. @param inStream the source InputStream @throws IOException */ public static String[] readSTX_ETX_Packets(InputStream inStream) throws IOException { List packets = new ArrayList<>(2); StringBuilder buffer = new StringBuilder(); int byteRead; boolean inPacket = false; while (inStream.available() > 0 && (byteRead = inStream.read()) > 0) { if (ETX == byteRead) { packets.add(buffer.toString()); buffer.setLength(0); inPacket = false; } else if (! inPacket) { if (byteRead == STX) { inPacket = true; } else if (! Character.isWhitespace(byteRead)) { throw new RuntimeIOException("No STX char found at the start of the packet!"); } } else { buffer.append((char) byteRead); } } return packets.toArray(new String[] {}); } //--------------------------------------------------------------------------- /** Reads the contents of the specified Reader into a String. @param inReader the source Reader @throws IOException */ public static String readerToString(Reader inReader) throws IOException { StringBuilder buffer = new StringBuilder(); char[] bytes = new char[8 * 1024]; int bytesRead = 0; while ((bytesRead = inReader.read(bytes)) > 0) { buffer.append(bytes, 0, bytesRead); } inReader.close(); return buffer.toString(); } //--------------------------------------------------------------------------- public static List readLines(BufferedReader inReader) throws IOException { List lines = new ArrayList<>(); String line; while ((line = inReader.readLine()) != null) { lines.add(line); } return lines; } //--------------------------------------------------------------------------- /** 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