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