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
33
34
35
36 public final class CommandlineStreams implements Closeable
37 {
38 private final ReadableByteChannel stdOutChannel;
39 private final ReadableByteChannel stdErrChannel;
40 private final WritableByteChannel stdInChannel;
41 private volatile boolean closed;
42
43 public CommandlineStreams( @Nonnull Process process )
44 {
45 InputStream stdOutStream = process.getInputStream();
46 stdOutChannel = newBufferedChannel( stdOutStream );
47
48 InputStream stdErrStream = process.getErrorStream();
49 stdErrChannel = newBufferedChannel( stdErrStream );
50
51 stdInChannel = newBufferedChannel( process.getOutputStream() );
52 }
53
54 public ReadableByteChannel getStdOutChannel()
55 {
56 return stdOutChannel;
57 }
58
59 public ReadableByteChannel getStdErrChannel()
60 {
61 return stdErrChannel;
62 }
63
64 public WritableByteChannel getStdInChannel()
65 {
66 return stdInChannel;
67 }
68
69 @Override
70 public void close() throws IOException
71 {
72 if ( closed )
73 {
74 return;
75 }
76
77 try ( Channel c1 = stdOutChannel;
78 Channel c2 = stdErrChannel;
79 Channel c3 = stdInChannel )
80 {
81 closed = true;
82 }
83 catch ( ClosedChannelException e )
84 {
85
86 }
87 }
88 }