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 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 static final Charset UTF8 = Charset.forName( "UTF-8" );
41  
42      public Utf8RecodingDeferredFileOutputStream( String channel )
43      {
44          this.deferredFileOutputStream = new DeferredFileOutputStream( 1000000, channel, "deferred", null );
45      }
46  
47      public void write( byte[] buf, int off, int len )
48          throws IOException
49      {
50          if ( !Charset.defaultCharset().equals( UTF8 ) )
51          {
52              CharBuffer decodedFromDefaultCharset = Charset.defaultCharset().decode( ByteBuffer.wrap( buf, off, len ) );
53              ByteBuffer utf8Encoded = UTF8.encode( decodedFromDefaultCharset );
54  
55              if ( utf8Encoded.hasArray() )
56              {
57                  byte[] convertedBytes = utf8Encoded.array();
58  
59                  deferredFileOutputStream.write( convertedBytes, utf8Encoded.position(), utf8Encoded.remaining() );
60              }
61              else
62              {
63                  byte[] convertedBytes = new byte[utf8Encoded.remaining()];
64                  utf8Encoded.get( convertedBytes, 0, utf8Encoded.remaining() );
65  
66                  deferredFileOutputStream.write( convertedBytes, 0, convertedBytes.length );
67              }
68          }
69          else
70          {
71              deferredFileOutputStream.write( buf, off, len );
72          }
73      }
74  
75      public long getByteCount()
76      {
77          return deferredFileOutputStream.getByteCount();
78      }
79  
80      public void close()
81          throws IOException
82      {
83          deferredFileOutputStream.close();
84      }
85  
86      public void writeTo( OutputStream out )
87          throws IOException
88      {
89          deferredFileOutputStream.writeTo( out );
90      }
91  }