001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.net.io;
019
020 import java.io.FilterOutputStream;
021 import java.io.IOException;
022 import java.io.OutputStream;
023 import java.net.Socket;
024
025 /***
026 * This class wraps an output stream, storing a reference to its originating
027 * socket. When the stream is closed, it will also close the socket
028 * immediately afterward. This class is useful for situations where you
029 * are dealing with a stream originating from a socket, but do not have
030 * a reference to the socket, and want to make sure it closes when the
031 * stream closes.
032 * <p>
033 * <p>
034 * @see SocketInputStream
035 ***/
036
037 public class SocketOutputStream extends FilterOutputStream
038 {
039 private final Socket __socket;
040
041 /***
042 * Creates a SocketOutputStream instance wrapping an output stream and
043 * storing a reference to a socket that should be closed on closing
044 * the stream.
045 * <p>
046 * @param socket The socket to close on closing the stream.
047 * @param stream The input stream to wrap.
048 ***/
049 public SocketOutputStream(Socket socket, OutputStream stream)
050 {
051 super(stream);
052 __socket = socket;
053 }
054
055
056 /***
057 * Writes a number of bytes from a byte array to the stream starting from
058 * a given offset. This method bypasses the equivalent method in
059 * FilterOutputStream because the FilterOutputStream implementation is
060 * very inefficient.
061 * <p>
062 * @param buffer The byte array to write.
063 * @param offset The offset into the array at which to start copying data.
064 * @param length The number of bytes to write.
065 * @exception IOException If an error occurs while writing to the underlying
066 * stream.
067 ***/
068 @Override
069 public void write(byte buffer[], int offset, int length) throws IOException
070 {
071 out.write(buffer, offset, length);
072 }
073
074
075 /***
076 * Closes the stream and immediately afterward closes the referenced
077 * socket.
078 * <p>
079 * @exception IOException If there is an error in closing the stream
080 * or socket.
081 ***/
082 @Override
083 public void close() throws IOException
084 {
085 super.close();
086 __socket.close();
087 }
088 }