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.OutputStream;
032
033/**
034 * Internal class.
035 *
036 * @since 4.3
037 */
038class LoggingOutputStream extends OutputStream {
039
040    private final OutputStream out;
041    private final Wire wire;
042
043    public LoggingOutputStream(final OutputStream out, final Wire wire) {
044        super();
045        this.out = out;
046        this.wire = wire;
047    }
048
049    @Override
050    public void write(final int b) throws IOException {
051        try {
052            wire.output(b);
053        } catch (final IOException ex) {
054            wire.output("[write] I/O error: " + ex.getMessage());
055            throw ex;
056        }
057    }
058
059    @Override
060    public void write(final byte[] b) throws IOException {
061        try {
062            wire.output(b);
063            out.write(b);
064        } catch (final IOException ex) {
065            wire.output("[write] I/O error: " + ex.getMessage());
066            throw ex;
067        }
068    }
069
070    @Override
071    public void write(final byte[] b, final int off, final int len) throws IOException {
072        try {
073            wire.output(b, off, len);
074            out.write(b, off, len);
075        } catch (final IOException ex) {
076            wire.output("[write] I/O error: " + ex.getMessage());
077            throw ex;
078        }
079    }
080
081    @Override
082    public void flush() throws IOException {
083        try {
084            out.flush();
085        } catch (final IOException ex) {
086            wire.output("[flush] I/O error: " + ex.getMessage());
087            throw ex;
088        }
089    }
090
091    @Override
092    public void close() throws IOException {
093        try {
094            out.close();
095        } catch (final IOException ex) {
096            wire.output("[close] I/O error: " + ex.getMessage());
097            throw ex;
098        }
099    }
100
101}