1 /******************************************************************************* 2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 *******************************************************************************/ 16 package it.jnrpe.utils; 17 18 import java.io.Closeable; 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.FileNotFoundException; 22 import java.io.FileOutputStream; 23 import java.io.InputStream; 24 import java.io.OutputStream; 25 import java.io.Reader; 26 import java.io.Writer; 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * Utility class for handling streams. 32 * 33 * @author Massimiliano Ziccardi 34 * 35 */ 36 public final class StreamManager { 37 /** 38 * Contains all the streams to be managed by the {@link StreamManager} 39 * object. 40 */ 41 private final List<Closeable> managedStreamsList = 42 new ArrayList<Closeable>(); 43 44 /** 45 * Default constructor. 46 */ 47 public StreamManager() { 48 } 49 50 /** 51 * Handles the received InputStream and returns it. 52 * 53 * @param in 54 * The stream to be automatically closed when {@link #closeAll()} 55 * is called. 56 * @return The passed in stream. 57 */ 58 public InputStream handle(final InputStream in) { 59 managedStreamsList.add(in); 60 return in; 61 } 62 63 /** 64 * Handles the received OutputStream and returns it. 65 * 66 * @param out 67 * The stream to be automatically closed when {@link #closeAll()} 68 * is called. 69 * @return The passed in stream. 70 */ 71 public OutputStream handle(final OutputStream out) { 72 managedStreamsList.add(out); 73 return out; 74 } 75 76 /** 77 * Handles the received Reader and returns it. 78 * 79 * @param r 80 * The reader to be automatically closed when {@link #closeAll()} 81 * is called. 82 * @return The passed in reader. 83 */ 84 public Reader handle(final Reader r) { 85 managedStreamsList.add(r); 86 return r; 87 } 88 89 /** 90 * Handles the received Writer and returns it. 91 * 92 * @param w 93 * The writer to be automatically closed when {@link #closeAll()} 94 * is called. 95 * @return The passed in writer. 96 */ 97 public Writer handle(final Writer w) { 98 managedStreamsList.add(w); 99 return w; 100 } 101 102 /** 103 * Returns an InputStream on the given file. 104 * 105 * @param f 106 * The file attached to the returned stream to be automatically 107 * closed when {@link #closeAll()} is called. 108 * @return The stream to the passed in file 109 * @throws FileNotFoundException 110 * If the file does not exists 111 */ 112 public InputStream getInputStream(final File f) 113 throws FileNotFoundException { 114 return handle(new FileInputStream(f)); 115 } 116 117 /** 118 * Returns an OutputStream on the given file. 119 * 120 * @param f 121 * The file attached to the returned stream to be automatically 122 * closed when {@link #closeAll()} is called. 123 * @return The stream to the passed in file 124 * @throws FileNotFoundException 125 * If the file does not exists 126 */ 127 public OutputStream getOutputStream(final File f) 128 throws FileNotFoundException { 129 return handle(new FileOutputStream(f)); 130 } 131 132 /** 133 * Closes all handles streams and readers. Non exception is thrown. This. 134 * method should be called in the finally block. 135 */ 136 public void closeAll() { 137 for (Closeable obj : managedStreamsList) { 138 try { 139 obj.close(); 140 } catch (Exception e) { 141 // if (m_Logger.isDebugEnabled()) 142 // m_Logger.debug("EXCEPTION CLOSING STREAM/READER : " + 143 // e.getMessage()); 144 } 145 } 146 } 147 148 }