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