001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.commons.util; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.OutputStream; 022import java.io.Reader; 023import java.io.Writer; 024 025/** 026 * Utility methods for operating with <tt>java.io.*</tt> classes. 027 * 028 * @author Malte Isberner <malte.isberner@gmail.com> 029 * 030 */ 031public abstract class IOUtil { 032 033 034 /** 035 * Skips the content of the stream as long as there is data available. 036 * Afterwards, the stream is closed. 037 * @param is the input stream. 038 * @throws IOException if an I/O error occurs. 039 */ 040 public static void skip(InputStream is) throws IOException { 041 while(is.available() > 0) 042 is.skip(Long.MAX_VALUE); 043 is.close(); 044 } 045 046 /** 047 * Copies all data from the given input stream to the given output stream. 048 * @param is the input stream. 049 * @param os the output stream. 050 * @param close <code>true</code> if both streams are closed afterwards, 051 * <code>false</code> otherwise. 052 * @throws IOException if an I/O error occurs. 053 */ 054 public static void copy(InputStream is, OutputStream os, boolean close) throws IOException { 055 byte[] buf = new byte[8192]; 056 int len; 057 try { 058 while((len = is.read(buf)) != -1) 059 os.write(buf, 0, len); 060 } 061 finally { 062 if(close) { 063 try { is.close(); } catch(IOException e) {} 064 try { os.close(); } catch(IOException e) {} 065 } 066 } 067 } 068 069 /** 070 * Copies all data from the given input stream to the given output stream 071 * and closes the streams. 072 * Convenience method, same as <code>copy(is, os, true)</code>. 073 * @param is the input stream. 074 * @param os the output stream. 075 * @throws IOException if an I/O error occurs. 076 * @see #copy(InputStream, OutputStream, boolean) 077 */ 078 public static void copy(InputStream is, OutputStream os) throws IOException { 079 copy(is, os, true); 080 } 081 082 /** 083 * Copies all text from the given reader to the given writer. 084 * @param r the reader. 085 * @param w the writer. 086 * @param close <code>true</code> if both reader and writer are closed 087 * afterwards, <code>false</code> otherwise. 088 * @throws IOException if an I/O error occurs. 089 */ 090 public static void copy(Reader r, Writer w, boolean close) throws IOException { 091 char[] buf = new char[8192]; 092 int len; 093 try { 094 while((len = r.read(buf)) != -1) 095 w.write(buf, 0, len); 096 } 097 finally { 098 if(close) { 099 try { r.close(); } catch(IOException e) {} 100 try { w.close(); } catch(IOException e) {} 101 } 102 } 103 } 104 105 /** 106 * Copies all text from the given reader to the given writer and closes 107 * both afterwards. 108 * Convenience method, same as <code>copy(r, w, true)</code>. 109 * @param r the reader. 110 * @param w the writer. 111 * @throws IOException if an I/O error occurs. 112 * @see #copy(Reader, Writer, boolean) 113 */ 114 public static void copy(Reader r, Writer w) throws IOException { 115 copy(r, w, true); 116 } 117}