1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.common;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.io.InputStreamReader;
|
14
|
import java.io.StringBufferInputStream;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
/**
|
19
|
* @author a.kohlbecker
|
20
|
* @date 16.12.2010
|
21
|
*
|
22
|
*/
|
23
|
public class StreamUtils {
|
24
|
|
25
|
public static final Logger logger = Logger.getLogger(StreamUtils.class);
|
26
|
|
27
|
/**
|
28
|
* Replaces each substring of this stream that matches the literal search sequence with the specified literal replace sequence.
|
29
|
* 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".
|
30
|
*
|
31
|
* @param stream
|
32
|
* @param search The sequence of char values to be replaced
|
33
|
* @param replace The replacement sequence of char values
|
34
|
* @return
|
35
|
* @throws IOException
|
36
|
*
|
37
|
*/
|
38
|
public static InputStream streamReplace(InputStream stream, String search, String replace) throws IOException {
|
39
|
InputStreamReader reader = new InputStreamReader(stream);
|
40
|
StringBuilder strBuilder = new StringBuilder();
|
41
|
|
42
|
char[] cbuf = new char[1024];
|
43
|
int charsRead = -1;
|
44
|
while ((charsRead = reader.read(cbuf)) > -1){
|
45
|
strBuilder.append(cbuf, 0, charsRead);
|
46
|
}
|
47
|
String replacedContent = strBuilder.toString().replace(search, replace);
|
48
|
StringBufferInputStream replacedStream = new StringBufferInputStream(replacedContent); //TODO replace with StringReader
|
49
|
logger.debug(replacedContent);
|
50
|
return replacedStream;
|
51
|
}
|
52
|
|
53
|
public static InputStream streamReplaceAll(InputStream stream, String regex, String replace) throws IOException {
|
54
|
InputStreamReader reader = new InputStreamReader(stream);
|
55
|
StringBuilder strBuilder = new StringBuilder();
|
56
|
|
57
|
char[] cbuf = new char[1024];
|
58
|
int charsRead = -1;
|
59
|
while ((charsRead = reader.read(cbuf)) > -1){
|
60
|
strBuilder.append(cbuf, 0, charsRead);
|
61
|
}
|
62
|
String replacedContent = strBuilder.toString().replaceAll(regex, replace);
|
63
|
StringBufferInputStream replacedStream = new StringBufferInputStream(replacedContent); //TODO replace with StringReader
|
64
|
logger.debug(replacedContent);
|
65
|
return replacedStream;
|
66
|
}
|
67
|
|
68
|
public static String readToString(InputStream stream) throws IOException {
|
69
|
InputStreamReader reader = new InputStreamReader(stream);
|
70
|
StringBuilder strBuilder = new StringBuilder();
|
71
|
|
72
|
char[] cbuf = new char[1024];
|
73
|
int charsRead = -1;
|
74
|
while ((charsRead = reader.read(cbuf)) > -1){
|
75
|
strBuilder.append(cbuf, 0, charsRead);
|
76
|
}
|
77
|
return strBuilder.toString();
|
78
|
}
|
79
|
|
80
|
}
|