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 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
32
33
34
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 }