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 | 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 | |
} |