11package net .b07z .sepia .server .core .tools ;
22
3- import java .io .BufferedInputStream ;
43import java .io .BufferedReader ;
54import java .io .File ;
65import java .io .FileInputStream ;
76import java .io .FileOutputStream ;
87import java .io .IOException ;
98import java .io .InputStream ;
109import java .io .InputStreamReader ;
11- import java .io .OutputStream ;
10+ import java .io .OutputStreamWriter ;
11+ import java .nio .charset .Charset ;
1212import java .nio .charset .StandardCharsets ;
1313import java .nio .file .Files ;
1414import java .nio .file .Path ;
1515import java .nio .file .Paths ;
1616import java .util .ArrayList ;
1717import java .util .List ;
1818import java .util .Properties ;
19+ import java .util .function .Consumer ;
1920
2021/**
2122 * Handles file read/write/edit etc.
@@ -36,21 +37,62 @@ public static interface LineOperation {
3637 */
3738 public String run (String lineInput );
3839 }
40+
41+ /**
42+ * Consume streams with custom consumer. Usage example with println:<br>
43+ * <br>
44+ * BufferedInputStreamConsumer streamGobbler = new StreamGobbler(myStream, System.out::println);<br>
45+ * Executors.newSingleThreadExecutor().submit(streamGobbler);<br>
46+ * <br>
47+ * As seen on <a href="https://www.baeldung.com/run-shell-command-in-java">baeldung.com</a>
48+ */
49+ public static class BufferedInputStreamConsumer implements Runnable {
50+ private InputStream inputStream ;
51+ private Consumer <String > consumer ;
52+ private Charset charset ;
53+
54+ public BufferedInputStreamConsumer (InputStream inputStream , Charset charset , Consumer <String > consumer ) {
55+ this .inputStream = inputStream ;
56+ this .consumer = consumer ;
57+ this .charset = charset ;
58+ }
59+ @ Override
60+ public void run () {
61+ new BufferedReader (new InputStreamReader (inputStream , charset )).lines ()
62+ .forEach (consumer );
63+ }
64+ }
3965
4066 /**
41- * Collect all data of an InputStream to a string.<br>
42- * NOTE: Please define encoding of stream!
67+ * Collect all data of an InputStream to a string via BufferedReader and InputStreamReader .<br>
68+ * NOTE: Please check correct encoding of stream!
4369 */
44- public static String getStringFromStream (InputStream stream ) {
45- try (BufferedReader in = new BufferedReader (new InputStreamReader (stream ))) {
70+ public static String getStringFromStream (InputStream stream , Charset charset ) throws IOException {
71+ try (BufferedReader in = new BufferedReader (new InputStreamReader (stream , charset ))) {
4672 String inputLine ;
4773 StringBuilder response = new StringBuilder ();
4874 while ((inputLine = in .readLine ()) != null ) {
49- response .append (inputLine );
75+ response .append (inputLine + System . lineSeparator () );
5076 }
5177 return response .toString ();
52- } catch (IOException e ) {
53- throw new RuntimeException (e );
78+ }catch (IOException e ){
79+ throw e ;
80+ }
81+ }
82+ /**
83+ * Collect all data of an InputStream to a list, line-by-line via BufferedReader and InputStreamReader.<br>
84+ * NOTE: Please check correct encoding of stream!
85+ */
86+ public static List <String > getLinesFromStream (InputStream stream , Charset charset ) throws IOException {
87+ try (BufferedReader in = new BufferedReader (new InputStreamReader (stream , charset ))) {
88+ String inputLine ;
89+ List <String > response = new ArrayList <>();
90+ while ((inputLine = in .readLine ()) != null ) {
91+ response .add (inputLine );
92+ }
93+ return response ;
94+ }catch (IOException e ){
95+ throw e ;
5496 }
5597 }
5698
@@ -141,7 +183,7 @@ public static boolean replaceLineInFile(String pathWithName, String lineMatchReg
141183 Path path = Paths .get (pathWithName );
142184 List <String > fileContent = new ArrayList <>(Files .readAllLines (path , StandardCharsets .UTF_8 ));
143185 boolean foundLine = false ;
144- for (int i = 0 ; i < fileContent .size (); i ++) {
186+ for (int i = 0 ; i < fileContent .size (); i ++) {
145187 String line = fileContent .get (i );
146188 if (line .matches (lineMatchRegExp )) {
147189 foundLine = true ;
@@ -164,27 +206,29 @@ public static boolean replaceLineInFile(String pathWithName, String lineMatchReg
164206 }
165207
166208 /**
167- * Save settings to properties file.
168- * @param config_file - path and file
209+ * Save settings to properties file (UTF-8) .
210+ * @param configFile - path and file
169211 * @param config - Properties with settings to store
170212 */
171- public static void saveSettings (String config_file , Properties config ) throws Exception {
172- OutputStream out =null ;
173- File f = new File (config_file );
174- out = new FileOutputStream ( f );
213+ public static void saveSettings (String configFile , Properties config ) throws Exception {
214+ File f = new File (configFile );
215+ OutputStreamWriter out = new OutputStreamWriter (
216+ new FileOutputStream (f ), StandardCharsets .UTF_8
217+ );
175218 config .store (out , null );
176219 out .flush ();
177220 out .close ();
178221 }
179222
180223 /**
181- * Load settings from properties file and return Properties.
224+ * Load settings from properties file (UTF-8) and return Properties.
182225 * @param config_file - path and file
183226 */
184- public static Properties loadSettings (String config_file ) throws Exception {
185- BufferedInputStream stream =null ;
227+ public static Properties loadSettings (String configFile ) throws Exception {
186228 Properties config = new Properties ();
187- stream = new BufferedInputStream (new FileInputStream (config_file ));
229+ InputStreamReader stream = new InputStreamReader (
230+ new FileInputStream (configFile ), StandardCharsets .UTF_8
231+ );
188232 config .load (stream );
189233 stream .close ();
190234 return config ;
0 commit comments