View Javadoc
1   package org.apache.maven.plugin.surefire.report;
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.shared.io.output.DeferredFileOutputStream;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  
27  import static java.nio.charset.StandardCharsets.UTF_8;
28  import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
29  
30  /**
31   * A deferred file output stream decorator that recodes the bytes written into the stream from the VM default encoding
32   * to UTF-8.
33   *
34   * @author Andreas Gudian
35   */
36  class Utf8RecodingDeferredFileOutputStream
37  {
38      private DeferredFileOutputStream deferredFileOutputStream;
39  
40      private boolean closed = false;
41  
42      @SuppressWarnings( "checkstyle:magicnumber" )
43      Utf8RecodingDeferredFileOutputStream( String channel )
44      {
45          deferredFileOutputStream = new DeferredFileOutputStream( 1000000, channel, "deferred", null );
46      }
47  
48      public synchronized void write( String output, boolean newLine )
49          throws IOException
50      {
51          if ( closed )
52          {
53              return;
54          }
55  
56          if ( output == null )
57          {
58              output = "null";
59          }
60  
61          deferredFileOutputStream.write( output.getBytes( UTF_8 ) );
62          if ( newLine )
63          {
64              deferredFileOutputStream.write( NL.getBytes( UTF_8 ) );
65          }
66      }
67  
68      public long getByteCount()
69      {
70          return deferredFileOutputStream.getByteCount();
71      }
72  
73      public synchronized void close()
74          throws IOException
75      {
76          closed = true;
77          deferredFileOutputStream.close();
78      }
79  
80      public synchronized void writeTo( OutputStream out )
81          throws IOException
82      {
83          if ( closed )
84          {
85              deferredFileOutputStream.writeTo( out );
86          }
87      }
88  
89      public synchronized void free()
90      {
91          if ( null != deferredFileOutputStream && null != deferredFileOutputStream.getFile() )
92          {
93              try
94              {
95                  closed = true;
96                  deferredFileOutputStream.close();
97                  if ( !deferredFileOutputStream.getFile().delete() )
98                  {
99                      deferredFileOutputStream.getFile().deleteOnExit();
100                 }
101             }
102             catch ( IOException ioe )
103             {
104                 deferredFileOutputStream.getFile().deleteOnExit();
105 
106             }
107         }
108     }
109 }