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      @Override
79      public boolean isDataAvailable(final int timeout) throws IOException {
80          return this.in.isDataAvailable(timeout);
81      }
82  
83      @Override
84      public int read(final byte[] b, final int off, final int len) throws IOException {
85          final int l = this.in.read(b,  off,  len);
86          if (this.wire.enabled() && l > 0) {
87              this.wire.input(b, off, l);
88          }
89          return l;
90      }
91  
92      @Override
93      public int read() throws IOException {
94          final int l = this.in.read();
95          if (this.wire.enabled() && l != -1) {
96              this.wire.input(l);
97          }
98          return l;
99      }
100 
101     @Override
102     public int read(final byte[] b) throws IOException {
103         final int l = this.in.read(b);
104         if (this.wire.enabled() && l > 0) {
105             this.wire.input(b, 0, l);
106         }
107         return l;
108     }
109 
110     @Override
111     public String readLine() throws IOException {
112         final String s = this.in.readLine();
113         if (this.wire.enabled() && s != null) {
114             final String tmp = s + "\r\n";
115             this.wire.input(tmp.getBytes(this.charset));
116         }
117         return s;
118     }
119 
120     @Override
121     public int readLine(final CharArrayBuffer buffer) throws IOException {
122         final int l = this.in.readLine(buffer);
123         if (this.wire.enabled() && l >= 0) {
124             final int pos = buffer.length() - l;
125             final String s = new String(buffer.buffer(), pos, l);
126             final String tmp = s + "\r\n";
127             this.wire.input(tmp.getBytes(this.charset));
128         }
129         return l;
130     }
131 
132     @Override
133     public HttpTransportMetrics getMetrics() {
134         return this.in.getMetrics();
135     }
136 
137     @Override
138     public boolean isEof() {
139         if (this.eofSensor != null) {
140             return this.eofSensor.isEof();
141         } else {
142             return false;
143         }
144     }
145 
146 }