KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > SocketOutputStream


1 /*
2  * @(#)SocketOutputStream.java 1.30 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.io.FileDescriptor JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.nio.channels.FileChannel JavaDoc;
14
15 /**
16  * This stream extends FileOutputStream to implement a
17  * SocketOutputStream. Note that this class should <b>NOT</b> be
18  * public.
19  *
20  * @version 1.30, 12/19/03
21  * @author Jonathan Payne
22  * @author Arthur van Hoff
23  */

24 class SocketOutputStream extends FileOutputStream JavaDoc
25 {
26     static {
27         init();
28     }
29
30     private PlainSocketImpl JavaDoc impl = null;
31     private byte temp[] = new byte[1];
32     private Socket JavaDoc socket = null;
33     
34     /**
35      * Creates a new SocketOutputStream. Can only be called
36      * by a Socket. This method needs to hang on to the owner Socket so
37      * that the fd will not be closed.
38      * @param impl the socket output stream inplemented
39      */

40     SocketOutputStream(PlainSocketImpl JavaDoc impl) throws IOException JavaDoc {
41     super(impl.getFileDescriptor());
42     this.impl = impl;
43     socket = impl.getSocket();
44     }
45
46     /**
47      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
48      * object associated with this file output stream. </p>
49      *
50      * The <code>getChannel</code> method of <code>SocketOutputStream</code>
51      * returns <code>null</code> since it is a socket based stream.</p>
52      *
53      * @return the file channel associated with this file output stream
54      *
55      * @since 1.4
56      * @spec JSR-51
57      */

58     public final FileChannel JavaDoc getChannel() {
59         return null;
60     }
61
62     /**
63      * Writes to the socket.
64      * @param fd the FileDescriptor
65      * @param b the data to be written
66      * @param off the start offset in the data
67      * @param len the number of bytes that are written
68      * @exception IOException If an I/O error has occurred.
69      */

70     private native void socketWrite0(FileDescriptor JavaDoc fd, byte[] b, int off,
71                      int len) throws IOException JavaDoc;
72
73     /**
74      * Writes to the socket with appropriate locking of the
75      * FileDescriptor.
76      * @param b the data to be written
77      * @param off the start offset in the data
78      * @param len the number of bytes that are written
79      * @exception IOException If an I/O error has occurred.
80      */

81     private void socketWrite(byte b[], int off, int len) throws IOException JavaDoc {
82
83     if (len <= 0 || off < 0 || off + len > b.length) {
84         if (len == 0) {
85         return;
86         }
87         throw new ArrayIndexOutOfBoundsException JavaDoc();
88     }
89
90     FileDescriptor JavaDoc fd = impl.acquireFD();
91     try {
92         socketWrite0(fd, b, off, len);
93     } catch (SocketException JavaDoc se) {
94         if (se instanceof sun.net.ConnectionResetException) {
95         impl.setConnectionResetPending();
96         se = new SocketException JavaDoc("Connection reset");
97         }
98         if (impl.isClosedOrPending()) {
99                 throw new SocketException JavaDoc("Socket closed");
100             } else {
101         throw se;
102         }
103     } finally {
104         impl.releaseFD();
105     }
106     }
107
108     /**
109      * Writes a byte to the socket.
110      * @param b the data to be written
111      * @exception IOException If an I/O error has occurred.
112      */

113     public void write(int b) throws IOException JavaDoc {
114     temp[0] = (byte)b;
115     socketWrite(temp, 0, 1);
116     }
117
118     /**
119      * Writes the contents of the buffer <i>b</i> to the socket.
120      * @param b the data to be written
121      * @exception SocketException If an I/O error has occurred.
122      */

123     public void write(byte b[]) throws IOException JavaDoc {
124     socketWrite(b, 0, b.length);
125     }
126
127     /**
128      * Writes <i>length</i> bytes from buffer <i>b</i> starting at
129      * offset <i>len</i>.
130      * @param b the data to be written
131      * @param off the start offset in the data
132      * @param len the number of bytes that are written
133      * @exception SocketException If an I/O error has occurred.
134      */

135     public void write(byte b[], int off, int len) throws IOException JavaDoc {
136     socketWrite(b, off, len);
137     }
138
139     /**
140      * Closes the stream.
141      */

142     private boolean closing = false;
143     public void close() throws IOException JavaDoc {
144     // Prevent recursion. See BugId 4484411
145
if (closing)
146         return;
147     closing = true;
148     if (socket != null) {
149         if (!socket.isClosed())
150         socket.close();
151     } else
152         impl.close();
153     closing = false;
154     }
155
156     /**
157      * Overrides finalize, the fd is closed by the Socket.
158      */

159     protected void finalize() {}
160
161     /**
162      * Perform class load-time initializations.
163      */

164     private native static void init();
165
166 }
167
Popular Tags