|
| 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