1 package org.apache.maven.plugin.surefire.report;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
33
34
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 }