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