Project

General

Profile

Download (2.79 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.common;
11

    
12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.io.InputStreamReader;
15
import java.io.StringBufferInputStream;
16

    
17
import org.apache.log4j.Logger;
18

    
19
/**
20
 * @author a.kohlbecker
21
 * @date 16.12.2010
22
 *
23
 */
24
public class StreamUtils {
25
	
26
	public static final Logger logger = Logger.getLogger(StreamUtils.class);
27
	
28
	/**
29
	 * Replaces each substring of this stream that matches the literal search sequence with the specified literal replace sequence. 
30
	 * The replacement proceeds from the beginning of the stream to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
31
	 * 
32
	 * @param stream 
33
	 * @param search The sequence of char values to be replaced
34
	 * @param replace The replacement sequence of char values
35
	 * @return
36
	 * @throws IOException
37
	 * 
38
	 */
39
	public static InputStream streamReplace(InputStream stream, String search,	String replace) throws IOException {
40
		InputStreamReader reader = new InputStreamReader(stream);
41
		StringBuilder strBuilder = new StringBuilder();
42
		
43
		char[] cbuf = new char[1024];
44
		int charsRead = -1;
45
		while ((charsRead = reader.read(cbuf)) > -1){
46
			strBuilder.append(cbuf, 0, charsRead);
47
		}
48
		String replacedContent = strBuilder.toString().replace(search, replace);	
49
		StringBufferInputStream replacedStream = new StringBufferInputStream(replacedContent); //TODO replace with StringReader
50
		logger.debug(replacedContent);
51
		return replacedStream;
52
	}
53
	
54
	public static InputStream streamReplaceAll(InputStream stream, String regex, String replace) throws IOException {
55
		InputStreamReader reader = new InputStreamReader(stream);
56
		StringBuilder strBuilder = new StringBuilder();
57
		
58
		char[] cbuf = new char[1024];
59
		int charsRead = -1;
60
		while ((charsRead = reader.read(cbuf)) > -1){
61
			strBuilder.append(cbuf, 0, charsRead);
62
		}
63
		String replacedContent = strBuilder.toString().replaceAll(regex, replace);	
64
		StringBufferInputStream replacedStream = new StringBufferInputStream(replacedContent); //TODO replace with StringReader
65
		logger.debug(replacedContent);
66
		return replacedStream;
67
	}
68
	
69
	public static String readToString(InputStream stream) throws IOException {
70
		InputStreamReader reader = new InputStreamReader(stream);
71
		StringBuilder strBuilder = new StringBuilder();
72
		
73
		char[] cbuf = new char[1024];
74
		int charsRead = -1;
75
		while ((charsRead = reader.read(cbuf)) > -1){
76
			strBuilder.append(cbuf, 0, charsRead);
77
		}
78
		return strBuilder.toString();
79
	}
80

    
81
}
(14-14/20)