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.Contract;
33  import org.apache.http.annotation.ThreadingBehavior;
34  import org.apache.http.io.EofSensor;
35  import org.apache.http.io.HttpTransportMetrics;
36  import org.apache.http.io.SessionInputBuffer;
37  import org.apache.http.util.CharArrayBuffer;
38  
39  /**
40   * Logs all data read to the wire LOG.
41   *
42   * @since 4.0
43   *
44   * @deprecated (4.3) no longer used.
45   */
46  @Contract(threading = ThreadingBehavior.IMMUTABLE)
47  @Deprecated
48  public class LoggingSessionInputBuffer implements SessionInputBuffer, EofSensor {
49  
50      /** Original session input buffer. */
51      private final SessionInputBuffer in;
52  
53      private final EofSensor eofSensor;
54  
55      /** The wire log to use for writing. */
56      private final Wire wire;
57  
58      private final String charset;
59  
60      /**
61       * Create an instance that wraps the specified session input buffer.
62       * @param in The session input buffer.
63       * @param wire The wire log to use.
64       * @param charset protocol charset, {@code ASCII} if {@code null}
65       */
66      public LoggingSessionInputBuffer(
67              final SessionInputBuffer in, final Wire wire, final String charset) {
68          super();
69          this.in = in;
70          this.eofSensor = in instanceof EofSensor ? (EofSensor) in : null;
71          this.wire = wire;
72          this.charset = charset != null ? charset : Consts.ASCII.name();
73      }
74  
75      public LoggingSessionInputBuffer(final SessionInputBuffer in, final Wire wire) {
76          this(in, wire, null);
77      }
78  
79      @Override
80      public boolean isDataAvailable(final int timeout) throws IOException {
81          return this.in.isDataAvailable(timeout);
82      }
83  
84      @Override
85      public int read(final byte[] b, final int off, final int len) throws IOException {
86          final int l = this.in.read(b,  off,  len);
87          if (this.wire.enabled() && l > 0) {
88              this.wire.input(b, off, l);
89          }
90          return l;
91      }
92  
93      @Override
94      public int read() throws IOException {
95          final int l = this.in.read();
96          if (this.wire.enabled() && l != -1) {
97              this.wire.input(l);
98          }
99          return l;
100     }
101 
102     @Override
103     public int read(final byte[] b) throws IOException {
104         final int l = this.in.read(b);
105         if (this.wire.enabled() && l > 0) {
106             this.wire.input(b, 0, l);
107         }
108         return l;
109     }
110 
111     @Override
112     public String readLine() throws IOException {
113         final String s = this.in.readLine();
114         if (this.wire.enabled() && s != null) {
115             final String tmp = s + "\r\n";
116             this.wire.input(tmp.getBytes(this.charset));
117         }
118         return s;
119     }
120 
121     @Override
122     public int readLine(final CharArrayBuffer buffer) throws IOException {
123         final int l = this.in.readLine(buffer);
124         if (this.wire.enabled() && l >= 0) {
125             final int pos = buffer.length() - l;
126             final String s = new String(buffer.buffer(), pos, l);
127             final String tmp = s + "\r\n";
128             this.wire.input(tmp.getBytes(this.charset));
129         }
130         return l;
131     }
132 
133     @Override
134     public HttpTransportMetrics getMetrics() {
135         return this.in.getMetrics();
136     }
137 
138     @Override
139     public boolean isEof() {
140         if (this.eofSensor != null) {
141             return this.eofSensor.isEof();
142         } else {
143             return false;
144         }
145     }
146 
147 }