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.OutputStream;
32  
33  import org.apache.hc.client5.http.impl.Wire;
34  
35  class LoggingOutputStream extends OutputStream {
36  
37      private final OutputStream out;
38      private final Wire wire;
39  
40      public LoggingOutputStream(final OutputStream out, final Wire wire) {
41          super();
42          this.out = out;
43          this.wire = wire;
44      }
45  
46      @Override
47      public void write(final int b) throws IOException {
48          try {
49              out.write(b);
50              wire.output(b);
51          } catch (final IOException ex) {
52              wire.output("[write] I/O error: " + ex.getMessage());
53              throw ex;
54          }
55      }
56  
57      @Override
58      public void write(final byte[] b) throws IOException {
59          try {
60              wire.output(b);
61              out.write(b);
62          } catch (final IOException ex) {
63              wire.output("[write] I/O error: " + ex.getMessage());
64              throw ex;
65          }
66      }
67  
68      @Override
69      public void write(final byte[] b, final int off, final int len) throws IOException {
70          try {
71              wire.output(b, off, len);
72              out.write(b, off, len);
73          } catch (final IOException ex) {
74              wire.output("[write] I/O error: " + ex.getMessage());
75              throw ex;
76          }
77      }
78  
79      @Override
80      public void flush() throws IOException {
81          try {
82              out.flush();
83          } catch (final IOException ex) {
84              wire.output("[flush] I/O error: " + ex.getMessage());
85              throw ex;
86          }
87      }
88  
89      @Override
90      public void close() throws IOException {
91          try {
92              out.close();
93          } catch (final IOException ex) {
94              wire.output("[close] I/O error: " + ex.getMessage());
95              throw ex;
96          }
97      }
98  
99  }