1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package java.net;
28 
29 import java.io.FileDescriptor;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.nio.channels.FileChannel;
33 
34 import dalvik.system.BlockGuard;
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     private AbstractPlainSocketImpl impl = null;
47     private byte temp[] = new byte[1];
48     private Socket socket = null;
49 
50     /**
51      * Creates a new SocketOutputStream. Can only be called
52      * by a Socket. This method needs to hang on to the owner Socket so
53      * that the fd will not be closed.
54      * @param impl the socket output stream inplemented
55      */
SocketOutputStream(AbstractPlainSocketImpl impl)56     SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException {
57         super(impl.getFileDescriptor());
58         this.impl = impl;
59         socket = impl.getSocket();
60     }
61 
62     /**
63      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
64      * object associated with this file output stream. </p>
65      *
66      * The {@code getChannel} method of {@code SocketOutputStream}
67      * returns {@code null} since it is a socket based stream.</p>
68      *
69      * @return  the file channel associated with this file output stream
70      *
71      * @since 1.4
72      * @spec JSR-51
73      */
getChannel()74     public final FileChannel getChannel() {
75         return null;
76     }
77 
78     /**
79      * Writes to the socket.
80      * @param fd the FileDescriptor
81      * @param b the data to be written
82      * @param off the start offset in the data
83      * @param len the number of bytes that are written
84      * @exception IOException If an I/O error has occurred.
85      */
socketWrite0(FileDescriptor fd, byte[] b, int off, int len)86     private native void socketWrite0(FileDescriptor fd, byte[] b, int off,
87                                      int len) throws IOException;
88 
89     /**
90      * Writes to the socket with appropriate locking of the
91      * FileDescriptor.
92      * @param b the data to be written
93      * @param off the start offset in the data
94      * @param len the number of bytes that are written
95      * @exception IOException If an I/O error has occurred.
96      */
socketWrite(byte b[], int off, int len)97     private void socketWrite(byte b[], int off, int len) throws IOException {
98         if (len <= 0 || off < 0 || len > b.length - off) {
99             if (len == 0) {
100                 return;
101             }
102             throw new ArrayIndexOutOfBoundsException("len == " + len
103                     + " off == " + off + " buffer length == " + b.length);
104         }
105 
106         FileDescriptor fd = impl.acquireFD();
107         try {
108             BlockGuard.getThreadPolicy().onNetwork();
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      */
write(int b)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      */
write(byte b[])140     public void write(byte b[]) throws IOException {
141         socketWrite(b, 0, b.length);
142     }
143 
144     /**
145      * Writes <i>length</i> bytes from buffer <i>b</i> starting at
146      * offset <i>len</i>.
147      * @param b the data to be written
148      * @param off the start offset in the data
149      * @param len the number of bytes that are written
150      * @exception SocketException If an I/O error has occurred.
151      */
write(byte b[], int off, int len)152     public void write(byte b[], int off, int len) throws IOException {
153         socketWrite(b, off, len);
154     }
155 
156     /**
157      * Closes the stream.
158      */
159     private boolean closing = false;
close()160     public void close() throws IOException {
161         // Prevent recursion. See BugId 4484411
162         if (closing)
163             return;
164         closing = true;
165         if (socket != null) {
166             if (!socket.isClosed())
167                 socket.close();
168         } else
169             impl.close();
170         closing = false;
171     }
172 
173     /**
174      * Overrides finalize, the fd is closed by the Socket.
175      */
finalize()176     protected void finalize() {}
177 }
178