1
package org.hisp.dhis.system.util;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.io.BufferedReader;
31
import java.io.BufferedWriter;
33
import java.io.FileInputStream;
34
import java.io.FileOutputStream;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.InputStreamReader;
38
import java.io.OutputStream;
39
import java.io.OutputStreamWriter;
40
import java.io.Reader;
41
import java.io.Writer;
43
import java.util.Map.Entry;
44
import java.util.zip.ZipEntry;
45
import java.util.zip.ZipInputStream;
46
import java.util.zip.ZipOutputStream;
49
* @author Lars Helge Overland
52
public class StreamUtils
54
private static final String LINE_BREAK = "\n";
55
private static final String ENCODING_UTF = "UTF8";
58
* Loads a resorce from the classpath defined by the name parameter.
60
* @param name the name of the resource.
61
* @return an InputStream.
63
public static InputStream loadResource( String name )
65
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
67
return classLoader.getResourceAsStream( name );
71
* Writes the content of the StringBuffer to the file.
73
* @param file the file to write to.
74
* @param content the content to write.
77
public static void writeContent( File file, StringBuffer content )
80
BufferedWriter writer = new BufferedWriter(
81
new OutputStreamWriter( new FileOutputStream( file ), ENCODING_UTF ) );
85
writer.write( content.toString() );
93
catch ( Exception ex )
101
catch ( Exception ex )
108
* Reads the content of the file to a StringBuffer. Each line is compared to
109
* the keys of the argument map. If a line is matched, the line is replaced
110
* with the keys corresponding value. Passing null as replace map argument skips
111
* value replacement. The reading will stop at the first match for a single
114
* @param file the file to read from.
115
* @param replaceMap a map containing keys to be matched and values with replacements.
116
* @return a StringBuffer with the content of the file replaced according to the Map.
117
* @throws IOException
119
public static StringBuffer readContent( File file, Map<String[], String> replaceMap )
122
StringBuffer content = new StringBuffer();
124
BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( file ), ENCODING_UTF ) );
128
String currentEndString = null;
132
while ( ( line = reader.readLine() ) != null )
134
if ( currentEndString != null )
136
if ( line.contains( currentEndString ) )
138
currentEndString = null;
144
if ( replaceMap != null )
146
for ( Entry<String[], String> entry : replaceMap.entrySet() )
148
if ( line.contains( entry.getKey()[0] ) )
150
currentEndString = ( entry.getKey()[1] != null && !line.contains( entry.getKey()[1] ) ) ? entry.getKey()[1] : null;
152
line = entry.getValue();
159
content.append( line + LINE_BREAK );
168
catch ( Exception ex )
177
* Closes the given Reader.
179
* @param reader the Reader to close.
181
public static void closeReader( Reader reader )
183
if ( reader != null )
189
catch ( Exception ex )
191
ex.printStackTrace();
197
* Closes the given Writer.
199
* @param writer the Writer to close.
201
public static void closeWriter( Writer writer )
203
if ( writer != null )
209
catch ( IOException ex )
211
ex.printStackTrace();
218
catch ( Exception ex )
220
ex.printStackTrace();
226
* Closes the given InputStream.
228
* @param in the InputStream to close.
230
public static void closeInputStream( InputStream in )
238
catch ( Exception ex )
240
ex.printStackTrace();
246
* Closes and flushes the given OutputStream.
248
* @param out the OutputStream to close.
250
public static void closeOutputStream( OutputStream out )
258
catch ( Exception ex )
260
ex.printStackTrace();
267
catch ( Exception ex )
269
ex.printStackTrace();
275
* Reads the next ZIP file entry from the ZipInputStream and positions the
276
* stream at the beginning of the entry data.
278
* @param in the ZipInputStream to read from.
279
* @return a ZipEntry.
281
public static ZipEntry getNextZipEntry( ZipInputStream in )
285
return in.getNextEntry();
287
catch ( Exception ex )
289
throw new RuntimeException( "Failed to get next entry in ZIP-file", ex );
294
* Finishes writing the contents of the ZIP output stream without closing the underlying stream.
296
* @param out the ZipOutputStream to write to.
298
public static void finishZipEntry( ZipOutputStream out )
304
catch ( Exception ex )
306
throw new RuntimeException( "Failed to finish ZipOutputStream", ex );