1 /*
   2  * Copyright (c) 1995, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.FileDescriptor;
  29 import java.io.FileOutputStream;
  30 import java.io.IOException;
  31 import java.nio.channels.FileChannel;
  32 
  33 import sun.misc.IoTrace;
  34 import sun.misc.IoTraceContext;
  35 
  36 /**
  37  * This stream extends FileOutputStream to implement a
  38  * SocketOutputStream. Note that this class should <b>NOT</b> be
  39  * public.
  40  *
  41  * @author      Jonathan Payne
  42  * @author      Arthur van Hoff
  43  */
  44 class SocketOutputStream extends FileOutputStream
  45 {
  46     static {
  47         init();
  48     }
  49 
  50     private AbstractPlainSocketImpl impl = null;
  51     private byte temp[] = new byte[1];
  52     private Socket socket = null;
  53 
  54     /**
  55      * Creates a new SocketOutputStream. Can only be called
  56      * by a Socket. This method needs to hang on to the owner Socket so
  57      * that the fd will not be closed.
  58      * @param impl the socket output stream inplemented
  59      */
  60     SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException {
  61         super(impl.getFileDescriptor());
  62         this.impl = impl;
  63         socket = impl.getSocket();
  64     }
  65 
  66     /**
  67      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
  68      * object associated with this file output stream. </p>
  69      *
  70      * The <code>getChannel</code> method of <code>SocketOutputStream</code>
  71      * returns <code>null</code> since it is a socket based stream.</p>
  72      *
  73      * @return  the file channel associated with this file output stream
  74      *
  75      * @since 1.4
  76      * @spec JSR-51
  77      */
  78     public final FileChannel getChannel() {
  79         return null;
  80     }
  81 
  82     /**
  83      * Writes to the socket.
  84      * @param fd the FileDescriptor
  85      * @param b the data to be written
  86      * @param off the start offset in the data
  87      * @param len the number of bytes that are written
  88      * @exception IOException If an I/O error has occurred.
  89      */
  90     private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
  91                                      int len) throws IOException;
  92 
  93     /**
  94      * Writes to the socket with appropriate locking of the
  95      * FileDescriptor.
  96      * @param b the data to be written
  97      * @param off the start offset in the data
  98      * @param len the number of bytes that are written
  99      * @exception IOException If an I/O error has occurred.
 100      */
 101     private void socketWrite(byte b[], int off, int len) throws IOException {
 102 
 103         if (len <= 0 || off < 0 || off + len > b.length) {
 104             if (len == 0) {
 105                 return;
 106             }
 107             throw new ArrayIndexOutOfBoundsException();
 108         }
 109 
 110         IoTraceContext traceContext = IoTrace.socketWriteBegin(impl.address, impl.port);
 111         int bytesWritten = 0;
 112         FileDescriptor fd = impl.acquireFD();
 113         try {
 114             socketWrite0(fd, b, off, len);
 115             bytesWritten = len;
 116         } catch (SocketException se) {
 117             if (se instanceof sun.net.ConnectionResetException) {
 118                 impl.setConnectionResetPending();
 119                 se = new SocketException("Connection reset");
 120             }
 121             if (impl.isClosedOrPending()) {
 122                 throw new SocketException("Socket closed");
 123             } else {
 124                 throw se;
 125             }
 126         } finally {
 127             impl.releaseFD();
 128             IoTrace.socketWriteEnd(traceContext, bytesWritten);
 129         }
 130     }
 131 
 132     /**
 133      * Writes a byte to the socket.
 134      * @param b the data to be written
 135      * @exception IOException If an I/O error has occurred.
 136      */
 137     public void write(int b) throws IOException {
 138         temp[0] = (byte)b;
 139         socketWrite(temp, 0, 1);
 140     }
 141 
 142     /**
 143      * Writes the contents of the buffer <i>b</i> to the socket.
 144      * @param b the data to be written
 145      * @exception SocketException If an I/O error has occurred.
 146      */
 147     public void write(byte b[]) throws IOException {
 148         socketWrite(b, 0, b.length);
 149     }
 150 
 151     /**
 152      * Writes <i>length</i> bytes from buffer <i>b</i> starting at
 153      * offset <i>len</i>.
 154      * @param b the data to be written
 155      * @param off the start offset in the data
 156      * @param len the number of bytes that are written
 157      * @exception SocketException If an I/O error has occurred.
 158      */
 159     public void write(byte b[], int off, int len) throws IOException {
 160         socketWrite(b, off, len);
 161     }
 162 
 163     /**
 164      * Closes the stream.
 165      */
 166     private boolean closing = false;
 167     public void close() throws IOException {
 168         // Prevent recursion. See BugId 4484411
 169         if (closing)
 170             return;
 171         closing = true;
 172         if (socket != null) {
 173             if (!socket.isClosed())
 174                 socket.close();
 175         } else
 176             impl.close();
 177         closing = false;
 178     }
 179 
 180     /**
 181      * Overrides finalize, the fd is closed by the Socket.
 182      */
 183     protected void finalize() {}
 184 
 185     /**
 186      * Perform class load-time initializations.
 187      */
 188     private native static void init();
 189 
 190 }