View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with 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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.conn;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.Consts;
32  import org.apache.http.annotation.Immutable;
33  import org.apache.http.io.EofSensor;
34  import org.apache.http.io.HttpTransportMetrics;
35  import org.apache.http.io.SessionInputBuffer;
36  import org.apache.http.util.CharArrayBuffer;
37  
38  /**
39   * Logs all data read to the wire LOG.
40   *
41   * @since 4.0
42   *
43   * @deprecated (4.3) no longer used.
44   */
45  @Immutable
46  @Deprecated
47  public class LoggingSessionInputBuffer implements SessionInputBuffer, EofSensor {
48  
49      /** Original session input buffer. */
50      private final SessionInputBuffer in;
51  
52      private final EofSensor eofSensor;
53  
54      /** The wire log to use for writing. */
55      private final Wire wire;
56  
57      private final String charset;
58  
59      /**
60       * Create an instance that wraps the specified session input buffer.
61       * @param in The session input buffer.
62       * @param wire The wire log to use.
63       * @param charset protocol charset, {@code ASCII} if {@code null}
64       */
65      public LoggingSessionInputBuffer(
66              final SessionInputBuffer in, final Wire wire, final String charset) {
67          super();
68          this.in = in;
69          this.eofSensor = in instanceof EofSensor ? (EofSensor) in : null;
70          this.wire = wire;
71          this.charset = charset != null ? charset : Consts.ASCII.name();
72      }
73  
74      public LoggingSessionInputBuffer(final SessionInputBuffer in, final Wire wire) {
75          this(in, wire, null);
76      }
77  
78      public boolean isDataAvailable(final int timeout) throws IOException {
79          return this.in.isDataAvailable(timeout);
80      }
81  
82      public int read(final byte[] b, final int off, final int len) throws IOException {
83          final int l = this.in.read(b,  off,  len);
84          if (this.wire.enabled() && l > 0) {
85              this.wire.input(b, off, l);
86          }
87          return l;
88      }
89  
90      public int read() throws IOException {
91          final int l = this.in.read();
92          if (this.wire.enabled() && l != -1) {
93              this.wire.input(l);
94          }
95          return l;
96      }
97  
98      public int read(final byte[] b) throws IOException {
99          final int l = this.in.read(b);
100         if (this.wire.enabled() && l > 0) {
101             this.wire.input(b, 0, l);
102         }
103         return l;
104     }
105 
106     public String readLine() throws IOException {
107         final String s = this.in.readLine();
108         if (this.wire.enabled() && s != null) {
109             final String tmp = s + "\r\n";
110             this.wire.input(tmp.getBytes(this.charset));
111         }
112         return s;
113     }
114 
115     public int readLine(final CharArrayBuffer buffer) throws IOException {
116         final int l = this.in.readLine(buffer);
117         if (this.wire.enabled() && l >= 0) {
118             final int pos = buffer.length() - l;
119             final String s = new String(buffer.buffer(), pos, l);
120             final String tmp = s + "\r\n";
121             this.wire.input(tmp.getBytes(this.charset));
122         }
123         return l;
124     }
125 
126     public HttpTransportMetrics getMetrics() {
127         return this.in.getMetrics();
128     }
129 
130     public boolean isEof() {
131         if (this.eofSensor != null) {
132             return this.eofSensor.isEof();
133         } else {
134             return false;
135         }
136     }
137 
138 }