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.File;
|
12
|
import java.io.FileOutputStream;
|
13
|
import java.io.IOException;
|
14
|
import java.io.InputStream;
|
15
|
import java.io.InputStreamReader;
|
16
|
import java.io.StringBufferInputStream;
|
17
|
import java.net.HttpURLConnection;
|
18
|
import java.net.URL;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
|
22
|
/**
|
23
|
* @author a.kohlbecker
|
24
|
* @since 16.12.2010
|
25
|
*
|
26
|
*/
|
27
|
public class StreamUtils {
|
28
|
|
29
|
public static final Logger logger = Logger.getLogger(StreamUtils.class);
|
30
|
private static final int BUFFER_SIZE = 4096;
|
31
|
|
32
|
/**
|
33
|
* Replaces each substring of this stream that matches the literal search sequence with the specified literal replace sequence.
|
34
|
* 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".
|
35
|
*
|
36
|
* @param stream
|
37
|
* @param search The sequence of char values to be replaced
|
38
|
* @param replace The replacement sequence of char values
|
39
|
* @return
|
40
|
* @throws IOException
|
41
|
*
|
42
|
*/
|
43
|
public static InputStream streamReplace(InputStream stream, String search, String replace) throws IOException {
|
44
|
InputStreamReader reader = new InputStreamReader(stream);
|
45
|
StringBuilder strBuilder = new StringBuilder();
|
46
|
|
47
|
char[] cbuf = new char[1024];
|
48
|
int charsRead = -1;
|
49
|
while ((charsRead = reader.read(cbuf)) > -1){
|
50
|
strBuilder.append(cbuf, 0, charsRead);
|
51
|
}
|
52
|
String replacedContent = strBuilder.toString().replace(search, replace);
|
53
|
StringBufferInputStream replacedStream = new StringBufferInputStream(replacedContent); //TODO replace with StringReader
|
54
|
logger.debug(replacedContent);
|
55
|
return replacedStream;
|
56
|
}
|
57
|
|
58
|
public static InputStream streamReplaceAll(InputStream stream, String regex, String replace) throws IOException {
|
59
|
InputStreamReader reader = new InputStreamReader(stream);
|
60
|
StringBuilder strBuilder = new StringBuilder();
|
61
|
|
62
|
char[] cbuf = new char[1024];
|
63
|
int charsRead = -1;
|
64
|
while ((charsRead = reader.read(cbuf)) > -1){
|
65
|
strBuilder.append(cbuf, 0, charsRead);
|
66
|
}
|
67
|
String replacedContent = strBuilder.toString().replaceAll(regex, replace);
|
68
|
StringBufferInputStream replacedStream = new StringBufferInputStream(replacedContent); //TODO replace with StringReader
|
69
|
logger.debug(replacedContent);
|
70
|
return replacedStream;
|
71
|
}
|
72
|
|
73
|
public static String readToString(InputStream stream) throws IOException {
|
74
|
InputStreamReader reader = new InputStreamReader(stream);
|
75
|
StringBuilder strBuilder = new StringBuilder();
|
76
|
|
77
|
char[] cbuf = new char[1024];
|
78
|
int charsRead = -1;
|
79
|
while ((charsRead = reader.read(cbuf)) > -1){
|
80
|
strBuilder.append(cbuf, 0, charsRead);
|
81
|
}
|
82
|
return strBuilder.toString();
|
83
|
}
|
84
|
|
85
|
public static void downloadFile(URL url, String saveDir)
|
86
|
throws IOException {
|
87
|
|
88
|
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
|
89
|
int responseCode = httpConn.getResponseCode();
|
90
|
|
91
|
// always check HTTP response code first
|
92
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
93
|
String fileName = "";
|
94
|
String disposition = httpConn.getHeaderField("Content-Disposition");
|
95
|
|
96
|
if (disposition != null) {
|
97
|
// extracts file name from header field
|
98
|
int index = disposition.indexOf("filename=");
|
99
|
if (index > 0) {
|
100
|
fileName = disposition.substring(index + 10,
|
101
|
disposition.length() - 1);
|
102
|
}
|
103
|
} else {
|
104
|
// extracts file name from URL
|
105
|
fileName = url.getFile().toString().substring(url.getFile().lastIndexOf("/") + 1,
|
106
|
url.getFile().length());
|
107
|
}
|
108
|
|
109
|
// opens input stream from the HTTP connection
|
110
|
InputStream inputStream = httpConn.getInputStream();
|
111
|
String saveFilePath = saveDir + File.separator + fileName;
|
112
|
|
113
|
// opens an output stream to save into file
|
114
|
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
|
115
|
|
116
|
int bytesRead = -1;
|
117
|
byte[] buffer = new byte[BUFFER_SIZE];
|
118
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
119
|
outputStream.write(buffer, 0, bytesRead);
|
120
|
}
|
121
|
|
122
|
outputStream.close();
|
123
|
inputStream.close();
|
124
|
|
125
|
|
126
|
} else {
|
127
|
logger.error("No file to download. Server replied HTTP code: " + responseCode);
|
128
|
}
|
129
|
httpConn.disconnect();
|
130
|
}
|
131
|
|
132
|
|
133
|
}
|