Skip to content

Commit 9a3b973

Browse files
committed
Merge branch 'release-0.0.10'
2 parents abd2880 + 6831b15 commit 9a3b973

File tree

22 files changed

+1119
-6
lines changed

22 files changed

+1119
-6
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/java-io-xml/target/
2-
/java-io-base/target/
1+
target/

java-io-base/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>be.nbb.rd</groupId>
77
<artifactId>java-io-parent</artifactId>
8-
<version>0.0.9</version>
8+
<version>0.0.10</version>
99
</parent>
1010

1111
<artifactId>java-io-base</artifactId>

java-io-base/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
exports nbbrd.io;
2525
exports nbbrd.io.function;
26+
exports nbbrd.io.sys;
2627
exports nbbrd.io.text;
2728
exports nbbrd.io.zip;
2829

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019 National Bank of Belgium
3+
*
4+
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
5+
* by the European Commission - subsequent versions of the EUPL (the "Licence");
6+
* You may not use this work except in compliance with the Licence.
7+
* You may obtain a copy of the Licence at:
8+
*
9+
* http://ec.europa.eu/idabc/eupl
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the Licence is distributed on an "AS IS" basis,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the Licence for the specific language governing permissions and
15+
* limitations under the Licence.
16+
*/
17+
package nbbrd.io.sys;
18+
19+
import org.checkerframework.checker.nullness.qual.NonNull;
20+
import org.checkerframework.checker.nullness.qual.Nullable;
21+
22+
import java.util.Locale;
23+
24+
/**
25+
* @author Philippe Charles
26+
*/
27+
@lombok.experimental.UtilityClass
28+
public class OS {
29+
30+
public static final Name NAME = Name.parse(System.getProperty("os.name"));
31+
32+
public enum Name {
33+
WINDOWS, LINUX, SOLARIS, MACOS, UNKNOWN;
34+
35+
public static @NonNull Name parse(@Nullable String osName) {
36+
if (osName != null) {
37+
String str = osName.toLowerCase(Locale.ROOT);
38+
if (str.contains("win")) return WINDOWS;
39+
if (str.contains("linux")) return LINUX;
40+
if (str.contains("solaris") || str.contains("sunos")) return SOLARIS;
41+
if (str.contains("mac")) return MACOS;
42+
}
43+
return UNKNOWN;
44+
}
45+
}
46+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2018 National Bank of Belgium
3+
*
4+
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
5+
* by the European Commission - subsequent versions of the EUPL (the "Licence");
6+
* You may not use this work except in compliance with the Licence.
7+
* You may obtain a copy of the Licence at:
8+
*
9+
* http://ec.europa.eu/idabc/eupl
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the Licence is distributed on an "AS IS" basis,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the Licence for the specific language governing permissions and
15+
* limitations under the Licence.
16+
*/
17+
package nbbrd.io.sys;
18+
19+
import org.checkerframework.checker.nullness.qual.NonNull;
20+
21+
import java.io.*;
22+
import java.nio.charset.Charset;
23+
import java.util.stream.Collectors;
24+
25+
/**
26+
* @author Philippe Charles
27+
*/
28+
@lombok.experimental.UtilityClass
29+
public class ProcessReader {
30+
31+
public static @NonNull BufferedReader newReader(@NonNull String... args) throws IOException {
32+
return newReader(new ProcessBuilder(args).redirectError(ProcessBuilder.Redirect.INHERIT).start());
33+
}
34+
35+
public static @NonNull BufferedReader newReader(@NonNull Process process) throws IOException {
36+
return new BufferedReader(new InputStreamReader(new ProcessInputStream(process), Charset.defaultCharset()));
37+
}
38+
39+
public static @NonNull String readToString(@NonNull String... args) throws IOException {
40+
return readToString(new ProcessBuilder(args).redirectError(ProcessBuilder.Redirect.INHERIT).start());
41+
}
42+
43+
public static @NonNull String readToString(@NonNull Process process) throws IOException {
44+
try (BufferedReader reader = newReader(process)) {
45+
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
46+
}
47+
}
48+
49+
private static final class ProcessInputStream extends InputStream {
50+
51+
@lombok.experimental.Delegate(excludes = Closeable.class)
52+
private final InputStream delegate;
53+
54+
private final Process process;
55+
56+
public ProcessInputStream(Process process) {
57+
this.delegate = process.getInputStream();
58+
this.process = process;
59+
}
60+
61+
@Override
62+
public void close() throws IOException {
63+
try {
64+
readUntilEnd();
65+
waitForEndOfProcess();
66+
} finally {
67+
delegate.close();
68+
}
69+
}
70+
71+
// we need the process to end, else we'll get an illegal Thread State Exception
72+
private void readUntilEnd() throws IOException {
73+
while (delegate.read() != -1) {
74+
}
75+
}
76+
77+
private void waitForEndOfProcess() throws IOException {
78+
try {
79+
process.waitFor();
80+
} catch (InterruptedException ex) {
81+
throw new IOException(ex);
82+
}
83+
if (process.exitValue() != 0) {
84+
throw new IOException("Invalid exit value: " + process.exitValue());
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)