1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/LoggingSessionInputBuffer.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.SessionInputBuffer;
37import org.apache.http.util.CharArrayBuffer;
38
39/**
40 * Logs all data read to the wire LOG.
41 *
42 * @author Ortwin Glueck
43 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
44 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
45 *
46 * @since 4.0
47 */
48public class LoggingSessionInputBuffer implements SessionInputBuffer {
49
50    /** Original session input buffer. */
51    private final SessionInputBuffer in;
52
53    /** The wire log to use for writing. */
54    private final Wire wire;
55
56    /**
57     * Create an instance that wraps the specified session input buffer.
58     * @param in The session input buffer.
59     * @param wire The wire log to use.
60     */
61    public LoggingSessionInputBuffer(final SessionInputBuffer in, final Wire wire) {
62        super();
63        this.in = in;
64        this.wire = wire;
65    }
66
67    public boolean isDataAvailable(int timeout) throws IOException {
68        return this.in.isDataAvailable(timeout);
69    }
70
71    public int read(byte[] b, int off, int len) throws IOException {
72        int l = this.in.read(b,  off,  len);
73        if (this.wire.enabled() && l > 0) {
74            this.wire.input(b, off, l);
75        }
76        return l;
77    }
78
79    public int read() throws IOException {
80        int l = this.in.read();
81        if (this.wire.enabled() && l > 0) {
82            this.wire.input(l);
83        }
84        return l;
85    }
86
87    public int read(byte[] b) throws IOException {
88        int l = this.in.read(b);
89        if (this.wire.enabled() && l > 0) {
90            this.wire.input(b, 0, l);
91        }
92        return l;
93    }
94
95    public String readLine() throws IOException {
96        String s = this.in.readLine();
97        if (this.wire.enabled() && s != null) {
98            this.wire.input(s + "[EOL]");
99        }
100        return s;
101    }
102
103    public int readLine(final CharArrayBuffer buffer) throws IOException {
104        int l = this.in.readLine(buffer);
105        if (this.wire.enabled() && l > 0) {
106            int pos = buffer.length() - l;
107            String s = new String(buffer.buffer(), pos, l);
108            this.wire.input(s + "[EOL]");
109        }
110        return l;
111    }
112
113    public HttpTransportMetrics getMetrics() {
114        return this.in.getMetrics();
115    }
116
117}
118