[SUREFIRE-1091] Prevent IOExceptions in case a thread tries to write to an already...
[maven-surefire.git] / maven-surefire-common / src / main / java / org / apache / maven / plugin / surefire / report / Utf8RecodingDeferredFileOutputStream.java
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 java.io.IOException;
23 import java.io.OutputStream;
24 import java.nio.ByteBuffer;
25 import java.nio.CharBuffer;
26 import java.nio.charset.Charset;
27
28 import org.apache.commons.io.output.DeferredFileOutputStream;
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     private static final Charset UTF8 = Charset.forName( "UTF-8" );
43
44     public Utf8RecodingDeferredFileOutputStream( String channel )
45     {
46         this.deferredFileOutputStream = new DeferredFileOutputStream( 1000000, channel, "deferred", null );
47     }
48
49     public synchronized void write( byte[] buf, int off, int len )
50         throws IOException
51     {
52         if ( closed )
53         {
54             return;
55         }
56
57         if ( !Charset.defaultCharset().equals( UTF8 ) )
58         {
59             CharBuffer decodedFromDefaultCharset = Charset.defaultCharset().decode( ByteBuffer.wrap( buf, off, len ) );
60             ByteBuffer utf8Encoded = UTF8.encode( decodedFromDefaultCharset );
61
62             if ( utf8Encoded.hasArray() )
63             {
64                 byte[] convertedBytes = utf8Encoded.array();
65
66                 deferredFileOutputStream.write( convertedBytes, utf8Encoded.position(), utf8Encoded.remaining() );
67             }
68             else
69             {
70                 byte[] convertedBytes = new byte[utf8Encoded.remaining()];
71                 utf8Encoded.get( convertedBytes, 0, utf8Encoded.remaining() );
72
73                 deferredFileOutputStream.write( convertedBytes, 0, convertedBytes.length );
74             }
75         }
76         else
77         {
78             deferredFileOutputStream.write( buf, off, len );
79         }
80     }
81
82     public long getByteCount()
83     {
84         return deferredFileOutputStream.getByteCount();
85     }
86
87     public synchronized void close()
88         throws IOException
89     {
90         closed = true;
91         deferredFileOutputStream.close();
92     }
93
94     public synchronized void writeTo( OutputStream out )
95         throws IOException
96     {
97         if ( closed )
98         {
99             deferredFileOutputStream.writeTo( out );
100         }
101     }
102
103     public synchronized void free()
104     {
105         if ( null != deferredFileOutputStream && null != deferredFileOutputStream.getFile() )
106         {
107             try
108             {
109                 closed = true;
110                 deferredFileOutputStream.close();
111                 if ( !deferredFileOutputStream.getFile().delete() )
112                 {
113                     deferredFileOutputStream.getFile().deleteOnExit();
114                 }
115             }
116             catch ( IOException ioe )
117             {
118                 deferredFileOutputStream.getFile().deleteOnExit();
119
120             }
121         }
122     }
123 }