1 package org.apache.maven.surefire.extensions.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.annotation.Nonnull;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.channels.Channel;
27 import java.nio.channels.ClosedChannelException;
28 import java.nio.channels.ReadableByteChannel;
29 import java.nio.channels.WritableByteChannel;
30
31 import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
32 import static org.apache.maven.surefire.api.util.internal.Channels.newChannel;
33
34
35
36
37 public final class CommandlineStreams implements Closeable
38 {
39 private final ReadableByteChannel stdOutChannel;
40 private final ReadableByteChannel stdErrChannel;
41 private final WritableByteChannel stdInChannel;
42 private volatile boolean closed;
43
44 public CommandlineStreams( @Nonnull Process process )
45 {
46 InputStream stdOutStream = process.getInputStream();
47 stdOutChannel = newBufferedChannel( stdOutStream );
48
49 InputStream stdErrStream = process.getErrorStream();
50 stdErrChannel = newBufferedChannel( stdErrStream );
51
52 stdInChannel = newChannel( process.getOutputStream() );
53 }
54
55 public ReadableByteChannel getStdOutChannel()
56 {
57 return stdOutChannel;
58 }
59
60 public ReadableByteChannel getStdErrChannel()
61 {
62 return stdErrChannel;
63 }
64
65 public WritableByteChannel getStdInChannel()
66 {
67 return stdInChannel;
68 }
69
70 @Override
71 public void close() throws IOException
72 {
73 if ( closed )
74 {
75 return;
76 }
77
78 try ( Channel c1 = stdOutChannel;
79 Channel c2 = stdErrChannel;
80 Channel c3 = stdInChannel )
81 {
82 closed = true;
83 }
84 catch ( ClosedChannelException e )
85 {
86
87 }
88 }
89 }