2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.common
;
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
;
20 import org
.apache
.logging
.log4j
.LogManager
;
21 import org
.apache
.logging
.log4j
.Logger
;
24 * @author a.kohlbecker
28 public class StreamUtils
{
30 public static final Logger logger
= LogManager
.getLogger(StreamUtils
.class);
31 private static final int BUFFER_SIZE
= 4096;
34 * Replaces each substring of this stream that matches the literal search sequence with the specified literal replace sequence.
35 * 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".
38 * @param search The sequence of char values to be replaced
39 * @param replace The replacement sequence of char values
44 public static InputStream
streamReplace(InputStream stream
, String search
, String replace
) throws IOException
{
45 InputStreamReader reader
= new InputStreamReader(stream
);
46 StringBuilder strBuilder
= new StringBuilder();
48 char[] cbuf
= new char[1024];
50 while ((charsRead
= reader
.read(cbuf
)) > -1){
51 strBuilder
.append(cbuf
, 0, charsRead
);
53 String replacedContent
= strBuilder
.toString().replace(search
, replace
);
54 StringBufferInputStream replacedStream
= new StringBufferInputStream(replacedContent
); //TODO replace with StringReader
55 logger
.debug(replacedContent
);
56 return replacedStream
;
59 public static InputStream
streamReplaceAll(InputStream stream
, String regex
, String replace
) throws IOException
{
60 InputStreamReader reader
= new InputStreamReader(stream
);
61 StringBuilder strBuilder
= new StringBuilder();
63 char[] cbuf
= new char[1024];
65 while ((charsRead
= reader
.read(cbuf
)) > -1){
66 strBuilder
.append(cbuf
, 0, charsRead
);
68 String replacedContent
= strBuilder
.toString().replaceAll(regex
, replace
);
69 StringBufferInputStream replacedStream
= new StringBufferInputStream(replacedContent
); //TODO replace with StringReader
70 logger
.debug(replacedContent
);
71 return replacedStream
;
74 public static String
readToString(InputStream stream
) throws IOException
{
75 InputStreamReader reader
= new InputStreamReader(stream
);
76 StringBuilder strBuilder
= new StringBuilder();
78 char[] cbuf
= new char[1024];
80 while ((charsRead
= reader
.read(cbuf
)) > -1){
81 strBuilder
.append(cbuf
, 0, charsRead
);
83 return strBuilder
.toString();
86 public static void downloadFile(URL url
, String saveDir
)
89 HttpURLConnection httpConn
= (HttpURLConnection
) url
.openConnection();
90 int responseCode
= httpConn
.getResponseCode();
92 // always check HTTP response code first
93 if (responseCode
== HttpURLConnection
.HTTP_OK
) {
95 String disposition
= httpConn
.getHeaderField("Content-Disposition");
97 if (disposition
!= null) {
98 // extracts file name from header field
99 int index
= disposition
.indexOf("filename=");
101 fileName
= disposition
.substring(index
+ 10,
102 disposition
.length() - 1);
105 // extracts file name from URL
106 fileName
= url
.getFile().toString().substring(url
.getFile().lastIndexOf("/") + 1,
107 url
.getFile().length());
110 // opens input stream from the HTTP connection
111 InputStream inputStream
= httpConn
.getInputStream();
112 String saveFilePath
= saveDir
+ File
.separator
+ fileName
;
114 // opens an output stream to save into file
115 FileOutputStream outputStream
= new FileOutputStream(saveFilePath
);
118 byte[] buffer
= new byte[BUFFER_SIZE
];
119 while ((bytesRead
= inputStream
.read(buffer
)) != -1) {
120 outputStream
.write(buffer
, 0, bytesRead
);
123 outputStream
.close();
128 logger
.error("No file to download. Server replied HTTP code: " + responseCode
);
130 httpConn
.disconnect();