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