author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44534 | jdk/src/java.base/share/classes/java/net/SocketOutputStream.java@a076dffbc2c1 |
child 48224 | be0df5ab3093 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44534 | 2 |
* Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
|
53 |
* by a Socket. This method needs to hang on to the owner Socket so |
|
54 |
* that the fd will not be closed. |
|
55 |
* @param impl the socket output stream inplemented |
|
56 |
*/ |
|
57 |
SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException { |
|
58 |
super(impl.getFileDescriptor()); |
|
59 |
this.impl = impl; |
|
60 |
socket = impl.getSocket(); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Returns the unique {@link java.nio.channels.FileChannel FileChannel} |
|
65 |
* object associated with this file output stream. </p> |
|
66 |
* |
|
19069 | 67 |
* The {@code getChannel} method of {@code SocketOutputStream} |
68 |
* returns {@code null} since it is a socket based stream.</p> |
|
2 | 69 |
* |
70 |
* @return the file channel associated with this file output stream |
|
71 |
* |
|
72 |
* @since 1.4 |
|
73 |
* @spec JSR-51 |
|
74 |
*/ |
|
75 |
public final FileChannel getChannel() { |
|
76 |
return null; |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* Writes to the socket. |
|
81 |
* @param fd the FileDescriptor |
|
82 |
* @param b the data to be written |
|
83 |
* @param off the start offset in the data |
|
84 |
* @param len the number of bytes that are written |
|
85 |
* @exception IOException If an I/O error has occurred. |
|
86 |
*/ |
|
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 |
||
43202 | 100 |
|
101 |
if (len <= 0 || off < 0 || len > b.length - off) { |
|
2 | 102 |
if (len == 0) { |
103 |
return; |
|
104 |
} |
|
43202 | 105 |
throw new ArrayIndexOutOfBoundsException("len == " + len |
106 |
+ " off == " + off + " buffer length == " + b.length); |
|
2 | 107 |
} |
108 |
||
109 |
FileDescriptor fd = impl.acquireFD(); |
|
110 |
try { |
|
111 |
socketWrite0(fd, b, off, len); |
|
112 |
} catch (SocketException se) { |
|
113 |
if (se instanceof sun.net.ConnectionResetException) { |
|
114 |
impl.setConnectionResetPending(); |
|
115 |
se = new SocketException("Connection reset"); |
|
116 |
} |
|
117 |
if (impl.isClosedOrPending()) { |
|
118 |
throw new SocketException("Socket closed"); |
|
119 |
} else { |
|
120 |
throw se; |
|
121 |
} |
|
122 |
} finally { |
|
123 |
impl.releaseFD(); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Writes a byte to the socket. |
|
129 |
* @param b the data to be written |
|
130 |
* @exception IOException If an I/O error has occurred. |
|
131 |
*/ |
|
132 |
public void write(int b) throws IOException { |
|
133 |
temp[0] = (byte)b; |
|
134 |
socketWrite(temp, 0, 1); |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Writes the contents of the buffer <i>b</i> to the socket. |
|
139 |
* @param b the data to be written |
|
140 |
* @exception SocketException If an I/O error has occurred. |
|
141 |
*/ |
|
142 |
public void write(byte b[]) throws IOException { |
|
143 |
socketWrite(b, 0, b.length); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Writes <i>length</i> bytes from buffer <i>b</i> starting at |
|
148 |
* offset <i>len</i>. |
|
149 |
* @param b the data to be written |
|
150 |
* @param off the start offset in the data |
|
151 |
* @param len the number of bytes that are written |
|
152 |
* @exception SocketException If an I/O error has occurred. |
|
153 |
*/ |
|
154 |
public void write(byte b[], int off, int len) throws IOException { |
|
155 |
socketWrite(b, off, len); |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Closes the stream. |
|
160 |
*/ |
|
161 |
private boolean closing = false; |
|
162 |
public void close() throws IOException { |
|
163 |
// Prevent recursion. See BugId 4484411 |
|
164 |
if (closing) |
|
165 |
return; |
|
166 |
closing = true; |
|
167 |
if (socket != null) { |
|
168 |
if (!socket.isClosed()) |
|
169 |
socket.close(); |
|
170 |
} else |
|
171 |
impl.close(); |
|
172 |
closing = false; |
|
173 |
} |
|
174 |
||
175 |
/** |
|
176 |
* Overrides finalize, the fd is closed by the Socket. |
|
177 |
*/ |
|
44534 | 178 |
@SuppressWarnings("deprecation") |
2 | 179 |
protected void finalize() {} |
180 |
||
181 |
/** |
|
182 |
* Perform class load-time initializations. |
|
183 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
184 |
private static native void init(); |
2 | 185 |
|
186 |
} |