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