View Javadoc
1   package org.apache.maven.surefire.junitcore;
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 org.apache.maven.surefire.report.ConsoleOutputReceiver;
23  
24  import java.util.Arrays;
25  import java.util.Queue;
26  import java.util.concurrent.ConcurrentLinkedQueue;
27  
28  /**
29   * A stream-like object that preserves ordering between stdout/stderr
30   */
31  public final class LogicalStream
32  {
33      private final Queue<Entry> output = new ConcurrentLinkedQueue<Entry>();
34  
35      private static final class Entry
36      {
37          private final boolean stdout;
38  
39          private final byte[] b;
40  
41          private final int off;
42  
43          private final int len;
44  
45          private Entry( boolean stdout, byte[] b, int off, int len )
46          {
47              this.stdout = stdout;
48              this.b = Arrays.copyOfRange( b, off, off + len );
49              this.off = 0;
50              this.len = len;
51          }
52  
53          private void writeDetails( ConsoleOutputReceiver outputReceiver )
54          {
55              outputReceiver.writeTestOutput( b, off, len, stdout );
56          }
57      }
58  
59      public void write( boolean stdout, byte b[], int off, int len )
60      {
61          if ( !isBlankLine( b, len ) )
62          {
63              Entry entry = new Entry( stdout, b, off, len );
64              output.add( entry );
65          }
66      }
67  
68      public void writeDetails( ConsoleOutputReceiver outputReceiver )
69      {
70          for ( Entry entry = output.poll(); entry != null; entry = output.poll() )
71          {
72              entry.writeDetails( outputReceiver );
73          }
74      }
75  
76      private static boolean isBlankLine( byte[] b, int len )
77      {
78          return b == null || len == 0;
79      }
80  }