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