|
1 /* |
|
2 * ==================================================================== |
|
3 * |
|
4 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
5 * contributor license agreements. See the NOTICE file distributed with |
|
6 * this work for additional information regarding copyright ownership. |
|
7 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
8 * (the "License"); you may not use this file except in compliance with |
|
9 * the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, software |
|
14 * distributed under the License is distributed on an "AS IS" BASIS, |
|
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
16 * See the License for the specific language governing permissions and |
|
17 * limitations under the License. |
|
18 * ==================================================================== |
|
19 * |
|
20 * This software consists of voluntary contributions made by many |
|
21 * individuals on behalf of the Apache Software Foundation. For more |
|
22 * information on the Apache Software Foundation, please see |
|
23 * <http://www.apache.org/>. |
|
24 * |
|
25 */ |
|
26 |
|
27 package ch.boye.httpclientandroidlib.impl.conn; |
|
28 |
|
29 import java.io.IOException; |
|
30 |
|
31 import ch.boye.httpclientandroidlib.annotation.Immutable; |
|
32 |
|
33 import ch.boye.httpclientandroidlib.io.HttpTransportMetrics; |
|
34 import ch.boye.httpclientandroidlib.io.SessionOutputBuffer; |
|
35 import ch.boye.httpclientandroidlib.protocol.HTTP; |
|
36 import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
|
37 |
|
38 /** |
|
39 * Logs all data written to the wire LOG. |
|
40 * |
|
41 * |
|
42 * @since 4.0 |
|
43 */ |
|
44 @Immutable |
|
45 public class LoggingSessionOutputBuffer implements SessionOutputBuffer { |
|
46 |
|
47 /** Original data transmitter. */ |
|
48 private final SessionOutputBuffer out; |
|
49 |
|
50 /** The wire log to use. */ |
|
51 private final Wire wire; |
|
52 |
|
53 private final String charset; |
|
54 |
|
55 /** |
|
56 * Create an instance that wraps the specified session output buffer. |
|
57 * @param out The session output buffer. |
|
58 * @param wire The Wire log to use. |
|
59 * @param charset protocol charset, <code>ASCII</code> if <code>null</code> |
|
60 */ |
|
61 public LoggingSessionOutputBuffer( |
|
62 final SessionOutputBuffer out, final Wire wire, final String charset) { |
|
63 super(); |
|
64 this.out = out; |
|
65 this.wire = wire; |
|
66 this.charset = charset != null ? charset : HTTP.ASCII; |
|
67 } |
|
68 |
|
69 public LoggingSessionOutputBuffer(final SessionOutputBuffer out, final Wire wire) { |
|
70 this(out, wire, null); |
|
71 } |
|
72 |
|
73 public void write(byte[] b, int off, int len) throws IOException { |
|
74 this.out.write(b, off, len); |
|
75 if (this.wire.enabled()) { |
|
76 this.wire.output(b, off, len); |
|
77 } |
|
78 } |
|
79 |
|
80 public void write(int b) throws IOException { |
|
81 this.out.write(b); |
|
82 if (this.wire.enabled()) { |
|
83 this.wire.output(b); |
|
84 } |
|
85 } |
|
86 |
|
87 public void write(byte[] b) throws IOException { |
|
88 this.out.write(b); |
|
89 if (this.wire.enabled()) { |
|
90 this.wire.output(b); |
|
91 } |
|
92 } |
|
93 |
|
94 public void flush() throws IOException { |
|
95 this.out.flush(); |
|
96 } |
|
97 |
|
98 public void writeLine(final CharArrayBuffer buffer) throws IOException { |
|
99 this.out.writeLine(buffer); |
|
100 if (this.wire.enabled()) { |
|
101 String s = new String(buffer.buffer(), 0, buffer.length()); |
|
102 String tmp = s + "\r\n"; |
|
103 this.wire.output(tmp.getBytes(this.charset)); |
|
104 } |
|
105 } |
|
106 |
|
107 public void writeLine(final String s) throws IOException { |
|
108 this.out.writeLine(s); |
|
109 if (this.wire.enabled()) { |
|
110 String tmp = s + "\r\n"; |
|
111 this.wire.output(tmp.getBytes(this.charset)); |
|
112 } |
|
113 } |
|
114 |
|
115 public HttpTransportMetrics getMetrics() { |
|
116 return this.out.getMetrics(); |
|
117 } |
|
118 |
|
119 } |