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  
28  package org.apache.http.impl.conn;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  /**
34   * Internal class.
35   *
36   * @since 4.3
37   */
38  class LoggingInputStream extends InputStream {
39  
40      private final InputStream in;
41      private final Wire wire;
42  
43      public LoggingInputStream(final InputStream in, final Wire wire) {
44          super();
45          this.in = in;
46          this.wire = wire;
47      }
48  
49      @Override
50      public int read() throws IOException {
51          try {
52              final int b = in.read();
53              if (b == -1) {
54                  wire.input("end of stream");
55              } else {
56                  wire.input(b);
57              }
58              return b;
59          } catch (final IOException ex) {
60              wire.input("[read] I/O error: " + ex.getMessage());
61              throw ex;
62          }
63      }
64  
65      @Override
66      public int read(final byte[] b) throws IOException {
67          try {
68              final int bytesRead = in.read(b);
69              if (bytesRead == -1) {
70                  wire.input("end of stream");
71              } else if (bytesRead > 0) {
72                  wire.input(b, 0, bytesRead);
73              }
74              return bytesRead;
75          } catch (final IOException ex) {
76              wire.input("[read] I/O error: " + ex.getMessage());
77              throw ex;
78          }
79      }
80  
81      @Override
82      public int read(final byte[] b, final int off, final int len) throws IOException {
83          try {
84              final int bytesRead = in.read(b, off, len);
85              if (bytesRead == -1) {
86                  wire.input("end of stream");
87              } else if (bytesRead > 0) {
88                  wire.input(b, off, bytesRead);
89              }
90              return bytesRead;
91          } catch (final IOException ex) {
92              wire.input("[read] I/O error: " + ex.getMessage());
93              throw ex;
94          }
95      }
96  
97      @Override
98      public long skip(final long n) throws IOException {
99          try {
100             return super.skip(n);
101         } catch (final IOException ex) {
102             wire.input("[skip] I/O error: " + ex.getMessage());
103             throw ex;
104         }
105     }
106 
107     @Override
108     public int available() throws IOException {
109         try {
110             return in.available();
111         } catch (final IOException ex) {
112             wire.input("[available] I/O error : " + ex.getMessage());
113             throw ex;
114         }
115     }
116 
117     @Override
118     public void mark(final int readlimit) {
119         super.mark(readlimit);
120     }
121 
122     @Override
123     public void reset() throws IOException {
124         super.reset();
125     }
126 
127     @Override
128     public boolean markSupported() {
129         return false;
130     }
131 
132     @Override
133     public void close() throws IOException {
134         try {
135             in.close();
136         } catch (final IOException ex) {
137             wire.input("[close] I/O error: " + ex.getMessage());
138             throw ex;
139         }
140     }
141 
142 }