View Javadoc

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