src/share/classes/java/net/SocketOutputStream.java

Print this page
rev 5501 : imported patch io-trace


  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 /**
  34  * This stream extends FileOutputStream to implement a
  35  * SocketOutputStream. Note that this class should <b>NOT</b> be
  36  * public.
  37  *
  38  * @author      Jonathan Payne
  39  * @author      Arthur van Hoff
  40  */
  41 class SocketOutputStream extends FileOutputStream
  42 {
  43     static {
  44         init();
  45     }
  46 
  47     private AbstractPlainSocketImpl impl = null;
  48     private byte temp[] = new byte[1];
  49     private Socket socket = null;
  50 
  51     /**
  52      * Creates a new SocketOutputStream. Can only be called


  87     private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
  88                                      int len) throws IOException;
  89 
  90     /**
  91      * Writes to the socket with appropriate locking of the
  92      * FileDescriptor.
  93      * @param b the data to be written
  94      * @param off the start offset in the data
  95      * @param len the number of bytes that are written
  96      * @exception IOException If an I/O error has occurred.
  97      */
  98     private void socketWrite(byte b[], int off, int len) throws IOException {
  99 
 100         if (len <= 0 || off < 0 || off + len > b.length) {
 101             if (len == 0) {
 102                 return;
 103             }
 104             throw new ArrayIndexOutOfBoundsException();
 105         }
 106 


 107         FileDescriptor fd = impl.acquireFD();
 108         try {
 109             socketWrite0(fd, b, off, len);

 110         } catch (SocketException se) {
 111             if (se instanceof sun.net.ConnectionResetException) {
 112                 impl.setConnectionResetPending();
 113                 se = new SocketException("Connection reset");
 114             }
 115             if (impl.isClosedOrPending()) {
 116                 throw new SocketException("Socket closed");
 117             } else {
 118                 throw se;
 119             }
 120         } finally {
 121             impl.releaseFD();

 122         }
 123     }
 124 
 125     /**
 126      * Writes a byte to the socket.
 127      * @param b the data to be written
 128      * @exception IOException If an I/O error has occurred.
 129      */
 130     public void write(int b) throws IOException {
 131         temp[0] = (byte)b;
 132         socketWrite(temp, 0, 1);
 133     }
 134 
 135     /**
 136      * Writes the contents of the buffer <i>b</i> to the socket.
 137      * @param b the data to be written
 138      * @exception SocketException If an I/O error has occurred.
 139      */
 140     public void write(byte b[]) throws IOException {
 141         socketWrite(b, 0, b.length);




  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 
  35 /**
  36  * This stream extends FileOutputStream to implement a
  37  * SocketOutputStream. Note that this class should <b>NOT</b> be
  38  * public.
  39  *
  40  * @author      Jonathan Payne
  41  * @author      Arthur van Hoff
  42  */
  43 class SocketOutputStream extends FileOutputStream
  44 {
  45     static {
  46         init();
  47     }
  48 
  49     private AbstractPlainSocketImpl impl = null;
  50     private byte temp[] = new byte[1];
  51     private Socket socket = null;
  52 
  53     /**
  54      * Creates a new SocketOutputStream. Can only be called


  89     private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
  90                                      int len) throws IOException;
  91 
  92     /**
  93      * Writes to the socket with appropriate locking of the
  94      * FileDescriptor.
  95      * @param b the data to be written
  96      * @param off the start offset in the data
  97      * @param len the number of bytes that are written
  98      * @exception IOException If an I/O error has occurred.
  99      */
 100     private void socketWrite(byte b[], int off, int len) throws IOException {
 101 
 102         if (len <= 0 || off < 0 || off + len > b.length) {
 103             if (len == 0) {
 104                 return;
 105             }
 106             throw new ArrayIndexOutOfBoundsException();
 107         }
 108 
 109         Object traceContext = IoTrace.socketWriteBegin(impl.address, impl.port);
 110         int bytesWritten = 0;
 111         FileDescriptor fd = impl.acquireFD();
 112         try {
 113             socketWrite0(fd, b, off, len);
 114             bytesWritten = len;
 115         } catch (SocketException se) {
 116             if (se instanceof sun.net.ConnectionResetException) {
 117                 impl.setConnectionResetPending();
 118                 se = new SocketException("Connection reset");
 119             }
 120             if (impl.isClosedOrPending()) {
 121                 throw new SocketException("Socket closed");
 122             } else {
 123                 throw se;
 124             }
 125         } finally {
 126             impl.releaseFD();
 127             IoTrace.socketWriteEnd(traceContext, bytesWritten);
 128         }
 129     }
 130 
 131     /**
 132      * Writes a byte to the socket.
 133      * @param b the data to be written
 134      * @exception IOException If an I/O error has occurred.
 135      */
 136     public void write(int b) throws IOException {
 137         temp[0] = (byte)b;
 138         socketWrite(temp, 0, 1);
 139     }
 140 
 141     /**
 142      * Writes the contents of the buffer <i>b</i> to the socket.
 143      * @param b the data to be written
 144      * @exception SocketException If an I/O error has occurred.
 145      */
 146     public void write(byte b[]) throws IOException {
 147         socketWrite(b, 0, b.length);