0N/A/*
2362N/A * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.FileDescriptor;
0N/A
0N/A/**
0N/A * The abstract class <code>SocketImpl</code> is a common superclass
0N/A * of all classes that actually implement sockets. It is used to
0N/A * create both client and server sockets.
0N/A * <p>
0N/A * A "plain" socket implements these methods exactly as
0N/A * described, without attempting to go through a firewall or proxy.
0N/A *
0N/A * @author unascribed
0N/A * @since JDK1.0
0N/A */
0N/Apublic abstract class SocketImpl implements SocketOptions {
0N/A /**
0N/A * The actual Socket object.
0N/A */
0N/A Socket socket = null;
0N/A ServerSocket serverSocket = null;
0N/A
0N/A /**
0N/A * The file descriptor object for this socket.
0N/A */
0N/A protected FileDescriptor fd;
0N/A
0N/A /**
0N/A * The IP address of the remote end of this socket.
0N/A */
0N/A protected InetAddress address;
0N/A
0N/A /**
0N/A * The port number on the remote host to which this socket is connected.
0N/A */
0N/A protected int port;
0N/A
0N/A /**
0N/A * The local port number to which this socket is connected.
0N/A */
0N/A protected int localport;
0N/A
0N/A /**
0N/A * Creates either a stream or a datagram socket.
0N/A *
0N/A * @param stream if <code>true</code>, create a stream socket;
0N/A * otherwise, create a datagram socket.
0N/A * @exception IOException if an I/O error occurs while creating the
0N/A * socket.
0N/A */
0N/A protected abstract void create(boolean stream) throws IOException;
0N/A
0N/A /**
0N/A * Connects this socket to the specified port on the named host.
0N/A *
0N/A * @param host the name of the remote host.
0N/A * @param port the port number.
0N/A * @exception IOException if an I/O error occurs when connecting to the
0N/A * remote host.
0N/A */
0N/A protected abstract void connect(String host, int port) throws IOException;
0N/A
0N/A /**
0N/A * Connects this socket to the specified port number on the specified host.
0N/A *
0N/A * @param address the IP address of the remote host.
0N/A * @param port the port number.
0N/A * @exception IOException if an I/O error occurs when attempting a
0N/A * connection.
0N/A */
0N/A protected abstract void connect(InetAddress address, int port) throws IOException;
0N/A
0N/A /**
0N/A * Connects this socket to the specified port number on the specified host.
0N/A * A timeout of zero is interpreted as an infinite timeout. The connection
0N/A * will then block until established or an error occurs.
0N/A *
0N/A * @param address the Socket address of the remote host.
0N/A * @param timeout the timeout value, in milliseconds, or zero for no timeout.
0N/A * @exception IOException if an I/O error occurs when attempting a
0N/A * connection.
0N/A * @since 1.4
0N/A */
0N/A protected abstract void connect(SocketAddress address, int timeout) throws IOException;
0N/A
0N/A /**
0N/A * Binds this socket to the specified local IP address and port number.
0N/A *
0N/A * @param host an IP address that belongs to a local interface.
0N/A * @param port the port number.
0N/A * @exception IOException if an I/O error occurs when binding this socket.
0N/A */
0N/A protected abstract void bind(InetAddress host, int port) throws IOException;
0N/A
0N/A /**
0N/A * Sets the maximum queue length for incoming connection indications
0N/A * (a request to connect) to the <code>count</code> argument. If a
0N/A * connection indication arrives when the queue is full, the
0N/A * connection is refused.
0N/A *
0N/A * @param backlog the maximum length of the queue.
0N/A * @exception IOException if an I/O error occurs when creating the queue.
0N/A */
0N/A protected abstract void listen(int backlog) throws IOException;
0N/A
0N/A /**
0N/A * Accepts a connection.
0N/A *
0N/A * @param s the accepted connection.
0N/A * @exception IOException if an I/O error occurs when accepting the
0N/A * connection.
0N/A */
0N/A protected abstract void accept(SocketImpl s) throws IOException;
0N/A
0N/A /**
0N/A * Returns an input stream for this socket.
0N/A *
0N/A * @return a stream for reading from this socket.
0N/A * @exception IOException if an I/O error occurs when creating the
0N/A * input stream.
0N/A */
0N/A protected abstract InputStream getInputStream() throws IOException;
0N/A
0N/A /**
0N/A * Returns an output stream for this socket.
0N/A *
0N/A * @return an output stream for writing to this socket.
0N/A * @exception IOException if an I/O error occurs when creating the
0N/A * output stream.
0N/A */
0N/A protected abstract OutputStream getOutputStream() throws IOException;
0N/A
0N/A /**
0N/A * Returns the number of bytes that can be read from this socket
0N/A * without blocking.
0N/A *
0N/A * @return the number of bytes that can be read from this socket
0N/A * without blocking.
0N/A * @exception IOException if an I/O error occurs when determining the
0N/A * number of bytes available.
0N/A */
0N/A protected abstract int available() throws IOException;
0N/A
0N/A /**
0N/A * Closes this socket.
0N/A *
0N/A * @exception IOException if an I/O error occurs when closing this socket.
0N/A */
0N/A protected abstract void close() throws IOException;
0N/A
0N/A /**
0N/A * Places the input stream for this socket at "end of stream".
0N/A * Any data sent to this socket is acknowledged and then
0N/A * silently discarded.
0N/A *
0N/A * If you read from a socket input stream after invoking
0N/A * shutdownInput() on the socket, the stream will return EOF.
0N/A *
0N/A * @exception IOException if an I/O error occurs when shutting down this
0N/A * socket.
0N/A * @see java.net.Socket#shutdownOutput()
0N/A * @see java.net.Socket#close()
0N/A * @see java.net.Socket#setSoLinger(boolean, int)
0N/A * @since 1.3
0N/A */
0N/A protected void shutdownInput() throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A
0N/A /**
0N/A * Disables the output stream for this socket.
0N/A * For a TCP socket, any previously written data will be sent
0N/A * followed by TCP's normal connection termination sequence.
0N/A *
0N/A * If you write to a socket output stream after invoking
0N/A * shutdownOutput() on the socket, the stream will throw
0N/A * an IOException.
0N/A *
0N/A * @exception IOException if an I/O error occurs when shutting down this
0N/A * socket.
0N/A * @see java.net.Socket#shutdownInput()
0N/A * @see java.net.Socket#close()
0N/A * @see java.net.Socket#setSoLinger(boolean, int)
0N/A * @since 1.3
0N/A */
0N/A protected void shutdownOutput() throws IOException {
0N/A throw new IOException("Method not implemented!");
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this socket's <code>fd</code> field.
0N/A *
0N/A * @return the value of this socket's <code>fd</code> field.
0N/A * @see java.net.SocketImpl#fd
0N/A */
0N/A protected FileDescriptor getFileDescriptor() {
0N/A return fd;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this socket's <code>address</code> field.
0N/A *
0N/A * @return the value of this socket's <code>address</code> field.
0N/A * @see java.net.SocketImpl#address
0N/A */
0N/A protected InetAddress getInetAddress() {
0N/A return address;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this socket's <code>port</code> field.
0N/A *
0N/A * @return the value of this socket's <code>port</code> field.
0N/A * @see java.net.SocketImpl#port
0N/A */
0N/A protected int getPort() {
0N/A return port;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not this SocketImpl supports sending
0N/A * urgent data. By default, false is returned
0N/A * unless the method is overridden in a sub-class
0N/A *
0N/A * @return true if urgent data supported
0N/A * @see java.net.SocketImpl#address
0N/A * @since 1.4
0N/A */
0N/A protected boolean supportsUrgentData () {
0N/A return false; // must be overridden in sub-class
0N/A }
0N/A
0N/A /**
0N/A * Send one byte of urgent data on the socket.
0N/A * The byte to be sent is the low eight bits of the parameter
0N/A * @param data The byte of data to send
0N/A * @exception IOException if there is an error
0N/A * sending the data.
0N/A * @since 1.4
0N/A */
0N/A protected abstract void sendUrgentData (int data) throws IOException;
0N/A
0N/A /**
0N/A * Returns the value of this socket's <code>localport</code> field.
0N/A *
0N/A * @return the value of this socket's <code>localport</code> field.
0N/A * @see java.net.SocketImpl#localport
0N/A */
0N/A protected int getLocalPort() {
0N/A return localport;
0N/A }
0N/A
0N/A void setSocket(Socket soc) {
0N/A this.socket = soc;
0N/A }
0N/A
0N/A Socket getSocket() {
0N/A return socket;
0N/A }
0N/A
0N/A void setServerSocket(ServerSocket soc) {
0N/A this.serverSocket = soc;
0N/A }
0N/A
0N/A ServerSocket getServerSocket() {
0N/A return serverSocket;
0N/A }
0N/A
0N/A /**
0N/A * Returns the address and port of this socket as a <code>String</code>.
0N/A *
0N/A * @return a string representation of this socket.
0N/A */
0N/A public String toString() {
0N/A return "Socket[addr=" + getInetAddress() +
0N/A ",port=" + getPort() + ",localport=" + getLocalPort() + "]";
0N/A }
0N/A
0N/A void reset() throws IOException {
0N/A address = null;
0N/A port = 0;
0N/A localport = 0;
0N/A }
0N/A
0N/A /**
0N/A * Sets performance preferences for this socket.
0N/A *
0N/A * <p> Sockets use the TCP/IP protocol by default. Some implementations
0N/A * may offer alternative protocols which have different performance
0N/A * characteristics than TCP/IP. This method allows the application to
0N/A * express its own preferences as to how these tradeoffs should be made
0N/A * when the implementation chooses from the available protocols.
0N/A *
0N/A * <p> Performance preferences are described by three integers
0N/A * whose values indicate the relative importance of short connection time,
0N/A * low latency, and high bandwidth. The absolute values of the integers
0N/A * are irrelevant; in order to choose a protocol the values are simply
0N/A * compared, with larger values indicating stronger preferences. Negative
0N/A * values represent a lower priority than positive values. If the
0N/A * application prefers short connection time over both low latency and high
0N/A * bandwidth, for example, then it could invoke this method with the values
0N/A * <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low
0N/A * latency, and low latency above short connection time, then it could
0N/A * invoke this method with the values <tt>(0, 1, 2)</tt>.
0N/A *
0N/A * By default, this method does nothing, unless it is overridden in a
0N/A * a sub-class.
0N/A *
0N/A * @param connectionTime
0N/A * An <tt>int</tt> expressing the relative importance of a short
0N/A * connection time
0N/A *
0N/A * @param latency
0N/A * An <tt>int</tt> expressing the relative importance of low
0N/A * latency
0N/A *
0N/A * @param bandwidth
0N/A * An <tt>int</tt> expressing the relative importance of high
0N/A * bandwidth
0N/A *
0N/A * @since 1.5
0N/A */
0N/A protected void setPerformancePreferences(int connectionTime,
0N/A int latency,
0N/A int bandwidth)
0N/A {
0N/A /* Not implemented yet */
0N/A }
0N/A}