001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.impl.conn; 029 030import java.io.IOException; 031import java.io.InputStream; 032 033/** 034 * Internal class. 035 * 036 * @since 4.3 037 */ 038class LoggingInputStream extends InputStream { 039 040 private final InputStream in; 041 private final Wire wire; 042 043 public LoggingInputStream(final InputStream in, final Wire wire) { 044 super(); 045 this.in = in; 046 this.wire = wire; 047 } 048 049 @Override 050 public int read() throws IOException { 051 try { 052 final int b = in.read(); 053 if (b == -1) { 054 wire.input("end of stream"); 055 } else { 056 wire.input(b); 057 } 058 return b; 059 } catch (final IOException ex) { 060 wire.input("[read] I/O error: " + ex.getMessage()); 061 throw ex; 062 } 063 } 064 065 @Override 066 public int read(final byte[] b) throws IOException { 067 try { 068 final int bytesRead = in.read(b); 069 if (bytesRead == -1) { 070 wire.input("end of stream"); 071 } else if (bytesRead > 0) { 072 wire.input(b, 0, bytesRead); 073 } 074 return bytesRead; 075 } catch (final IOException ex) { 076 wire.input("[read] I/O error: " + ex.getMessage()); 077 throw ex; 078 } 079 } 080 081 @Override 082 public int read(final byte[] b, final int off, final int len) throws IOException { 083 try { 084 final int bytesRead = in.read(b, off, len); 085 if (bytesRead == -1) { 086 wire.input("end of stream"); 087 } else if (bytesRead > 0) { 088 wire.input(b, off, bytesRead); 089 } 090 return bytesRead; 091 } catch (final IOException ex) { 092 wire.input("[read] I/O error: " + ex.getMessage()); 093 throw ex; 094 } 095 } 096 097 @Override 098 public long skip(final long n) throws IOException { 099 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}