View Javadoc
1   package org.apache.maven.surefire.extensions.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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              // already closed externally
87          }
88      }
89  }