author | jboes |
Fri, 20 Sep 2019 11:07:52 +0100 | |
changeset 58242 | 94bb65cb37d3 |
parent 54689 | b28b7f631301 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
52499
diff
changeset
|
2 |
* Copyright (c) 1995, 2019, 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 |
*/ |
|
49249
92cca24c8807
8199329: Remove code that attempts to read bytes after connection reset reported
alanb
parents:
48224
diff
changeset
|
41 |
class SocketOutputStream extends FileOutputStream { |
2 | 42 |
static { |
43 |
init(); |
|
44 |
} |
|
45 |
||
46 |
private AbstractPlainSocketImpl impl = null; |
|
47 |
private byte temp[] = new byte[1]; |
|
48 |
||
49 |
/** |
|
50 |
* Creates a new SocketOutputStream. Can only be called |
|
51 |
* by a Socket. This method needs to hang on to the owner Socket so |
|
52 |
* that the fd will not be closed. |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
50425
diff
changeset
|
53 |
* @param impl the socket output stream implemented |
2 | 54 |
*/ |
55 |
SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException { |
|
56 |
super(impl.getFileDescriptor()); |
|
57 |
this.impl = impl; |
|
58 |
} |
|
59 |
||
60 |
/** |
|
61 |
* Returns the unique {@link java.nio.channels.FileChannel FileChannel} |
|
62 |
* object associated with this file output stream. </p> |
|
63 |
* |
|
19069 | 64 |
* The {@code getChannel} method of {@code SocketOutputStream} |
65 |
* returns {@code null} since it is a socket based stream.</p> |
|
2 | 66 |
* |
67 |
* @return the file channel associated with this file output stream |
|
68 |
* |
|
69 |
* @since 1.4 |
|
70 |
* @spec JSR-51 |
|
71 |
*/ |
|
72 |
public final FileChannel getChannel() { |
|
73 |
return null; |
|
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* Writes to the socket. |
|
78 |
* @param fd the FileDescriptor |
|
79 |
* @param b the data to be written |
|
80 |
* @param off the start offset in the data |
|
81 |
* @param len the number of bytes that are written |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54689
diff
changeset
|
82 |
* @throws IOException If an I/O error has occurred. |
2 | 83 |
*/ |
84 |
private native void socketWrite0(FileDescriptor fd, byte[] b, int off, |
|
85 |
int len) throws IOException; |
|
86 |
||
87 |
/** |
|
88 |
* Writes to the socket with appropriate locking of the |
|
89 |
* FileDescriptor. |
|
90 |
* @param b the data to be written |
|
91 |
* @param off the start offset in the data |
|
92 |
* @param len the number of bytes that are written |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54689
diff
changeset
|
93 |
* @throws IOException If an I/O error has occurred. |
2 | 94 |
*/ |
95 |
private void socketWrite(byte b[], int off, int len) throws IOException { |
|
96 |
||
43202 | 97 |
|
98 |
if (len <= 0 || off < 0 || len > b.length - off) { |
|
2 | 99 |
if (len == 0) { |
100 |
return; |
|
101 |
} |
|
43202 | 102 |
throw new ArrayIndexOutOfBoundsException("len == " + len |
103 |
+ " off == " + off + " buffer length == " + b.length); |
|
2 | 104 |
} |
105 |
||
106 |
FileDescriptor fd = impl.acquireFD(); |
|
107 |
try { |
|
108 |
socketWrite0(fd, b, off, len); |
|
109 |
} catch (SocketException se) { |
|
110 |
if (impl.isClosedOrPending()) { |
|
111 |
throw new SocketException("Socket closed"); |
|
112 |
} else { |
|
113 |
throw se; |
|
114 |
} |
|
115 |
} finally { |
|
116 |
impl.releaseFD(); |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Writes a byte to the socket. |
|
122 |
* @param b the data to be written |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54689
diff
changeset
|
123 |
* @throws IOException If an I/O error has occurred. |
2 | 124 |
*/ |
125 |
public void write(int b) throws IOException { |
|
126 |
temp[0] = (byte)b; |
|
127 |
socketWrite(temp, 0, 1); |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Writes the contents of the buffer <i>b</i> to the socket. |
|
132 |
* @param b the data to be written |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54689
diff
changeset
|
133 |
* @throws SocketException If an I/O error has occurred. |
2 | 134 |
*/ |
135 |
public void write(byte b[]) throws IOException { |
|
136 |
socketWrite(b, 0, b.length); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Writes <i>length</i> bytes from buffer <i>b</i> starting at |
|
141 |
* offset <i>len</i>. |
|
142 |
* @param b the data to be written |
|
143 |
* @param off the start offset in the data |
|
144 |
* @param len the number of bytes that are written |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54689
diff
changeset
|
145 |
* @throws SocketException If an I/O error has occurred. |
2 | 146 |
*/ |
147 |
public void write(byte b[], int off, int len) throws IOException { |
|
148 |
socketWrite(b, off, len); |
|
149 |
} |
|
150 |
||
151 |
public void close() throws IOException { |
|
54689
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
52499
diff
changeset
|
152 |
// No longer used. Socket.getOutputStream returns an |
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
52499
diff
changeset
|
153 |
// OutputStream which calls Socket.close directly |
b28b7f631301
8216978: Drop support for pre JDK 1.4 SocketImpl implementations
michaelm
parents:
52499
diff
changeset
|
154 |
assert false; |
2 | 155 |
} |
156 |
||
157 |
/** |
|
158 |
* Overrides finalize, the fd is closed by the Socket. |
|
159 |
*/ |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47216
diff
changeset
|
160 |
@SuppressWarnings({"deprecation", "removal"}) |
2 | 161 |
protected void finalize() {} |
162 |
||
163 |
/** |
|
164 |
* Perform class load-time initializations. |
|
165 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25859
diff
changeset
|
166 |
private static native void init(); |
2 | 167 |
|
168 |
} |