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 *
48 * @deprecated Please use {@link java.net.URL#openConnection} instead.
49 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
50 *     for further details.
51 */
52@Deprecated
53public class LoggingSessionInputBuffer implements SessionInputBuffer {
54
55    /** Original session input buffer. */
56    private final SessionInputBuffer in;
57
58    /** The wire log to use for writing. */
59    private final Wire wire;
60
61    /**
62     * Create an instance that wraps the specified session input buffer.
63     * @param in The session input buffer.
64     * @param wire The wire log to use.
65     */
66    public LoggingSessionInputBuffer(final SessionInputBuffer in, final Wire wire) {
67        super();
68        this.in = in;
69        this.wire = wire;
70    }
71
72    public boolean isDataAvailable(int timeout) throws IOException {
73        return this.in.isDataAvailable(timeout);
74    }
75
76    public int read(byte[] b, int off, int len) throws IOException {
77        int l = this.in.read(b,  off,  len);
78        if (this.wire.enabled() && l > 0) {
79            this.wire.input(b, off, l);
80        }
81        return l;
82    }
83
84    public int read() throws IOException {
85        int l = this.in.read();
86        if (this.wire.enabled() && l > 0) {
87            this.wire.input(l);
88        }
89        return l;
90    }
91
92    public int read(byte[] b) throws IOException {
93        int l = this.in.read(b);
94        if (this.wire.enabled() && l > 0) {
95            this.wire.input(b, 0, l);
96        }
97        return l;
98    }
99
100    public String readLine() throws IOException {
101        String s = this.in.readLine();
102        if (this.wire.enabled() && s != null) {
103            this.wire.input(s + "[EOL]");
104        }
105        return s;
106    }
107
108    public int readLine(final CharArrayBuffer buffer) throws IOException {
109        int l = this.in.readLine(buffer);
110        if (this.wire.enabled() && l > 0) {
111            int pos = buffer.length() - l;
112            String s = new String(buffer.buffer(), pos, l);
113            this.wire.input(s + "[EOL]");
114        }
115        return l;
116    }
117
118    public HttpTransportMetrics getMetrics() {
119        return this.in.getMetrics();
120    }
121
122}
123