001: /*
002: * @(#)SocketOutputStream.java 1.25 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.net;
029:
030: import java.io.FileDescriptor;
031: import java.io.FileOutputStream;
032: import java.io.IOException;
033:
034: /**
035: * This stream extends FileOutputStream to implement a
036: * SocketOutputStream. Note that this class should <b>NOT</b> be
037: * public.
038: *
039: * @version 1.17, 02/02/00
040: * @author Jonathan Payne
041: * @author Arthur van Hoff
042: */
043: class SocketOutputStream extends FileOutputStream {
044: static {
045: init();
046: }
047:
048: private PlainSocketImpl impl = null;
049: private byte temp[] = new byte[1];
050: private Socket socket = null;
051:
052: /**
053: * Creates a new SocketOutputStream. Can only be called
054: * by a Socket. This method needs to hang on to the owner Socket so
055: * that the fd will not be closed.
056: * @param impl the socket output stream inplemented
057: */
058: SocketOutputStream(PlainSocketImpl impl) throws IOException {
059: super (impl.getFileDescriptor());
060: this .impl = impl;
061: socket = impl.getSocket();
062: }
063:
064: /**
065: * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
066: * object associated with this file output stream. </p>
067: *
068: * The <code>getChannel</code> method of <code>SocketOutputStream</code>
069: * returns <code>null</code> since it is a socket based stream.</p>
070: *
071: * @return the file channel associated with this file output stream
072: *
073: * @since 1.4
074: * @spec JSR-51
075: */
076: /* NOTE: No NIO in CDC
077: public final FileChannel getChannel() {
078: return null;
079: }
080: */
081: /**
082: * Writes to the socket.
083: * @param fd the FileDescriptor
084: * @param b the data to be written
085: * @param off the start offset in the data
086: * @param len the number of bytes that are written
087: * @exception IOException If an I/O error has occurred.
088: */
089: private native void socketWrite0(FileDescriptor fd, byte[] b,
090: int off, int len) throws IOException;
091:
092: /**
093: * Writes to the socket with appropriate locking of the
094: * FileDescriptor.
095: * @param b the data to be written
096: * @param off the start offset in the data
097: * @param len the number of bytes that are written
098: * @exception IOException If an I/O error has occurred.
099: */
100: private void socketWrite(byte b[], int off, int len)
101: throws IOException {
102:
103: if (len <= 0 || off < 0 | off + len > b.length) {
104: if (len == 0) {
105: return;
106: }
107: throw new ArrayIndexOutOfBoundsException();
108: }
109:
110: FileDescriptor fd = impl.acquireFD();
111: try {
112: socketWrite0(fd, b, off, len);
113: } catch (SocketException se) {
114: if (se instanceof sun.net.ConnectionResetException) {
115: impl.setConnectionResetPending();
116: se = new SocketException("Connection reset");
117: }
118: if (impl.isClosedOrPending()) {
119: throw new SocketException("Socket closed");
120: } else {
121: throw se;
122: }
123: } finally {
124: impl.releaseFD();
125: }
126: }
127:
128: /**
129: * Writes a byte to the socket.
130: * @param b the data to be written
131: * @exception IOException If an I/O error has occurred.
132: */
133: public void write(int b) throws IOException {
134: temp[0] = (byte) b;
135: socketWrite(temp, 0, 1);
136: }
137:
138: /**
139: * Writes the contents of the buffer <i>b</i> to the socket.
140: * @param b the data to be written
141: * @exception SocketException If an I/O error has occurred.
142: */
143: public void write(byte b[]) throws IOException {
144: socketWrite(b, 0, b.length);
145: }
146:
147: /**
148: * Writes <i>length</i> bytes from buffer <i>b</i> starting at
149: * offset <i>len</i>.
150: * @param b the data to be written
151: * @param off the start offset in the data
152: * @param len the number of bytes that are written
153: * @exception SocketException If an I/O error has occurred.
154: */
155: public void write(byte b[], int off, int len) throws IOException {
156: socketWrite(b, off, len);
157: }
158:
159: /**
160: * Closes the stream.
161: */
162: private boolean closing = false;
163:
164: public void close() throws IOException {
165: // Prevent recursion. See BugId 4484411
166: if (closing)
167: return;
168: closing = true;
169: if (socket != null) {
170: if (!socket.isClosed())
171: socket.close();
172: } else
173: impl.close();
174: closing = false;
175: }
176:
177: /**
178: * Overrides finalize, the fd is closed by the Socket.
179: */
180: protected void finalize() {
181: }
182:
183: /**
184: * Perform class load-time initializations.
185: */
186: private native static void init();
187:
188: }
|