Chapter 33

Package java.net


CONTENTS


The java.net package contains the most common classes used for networking. This includes classes to create and store sockets, data packets, and URLs as well as a set of interfaces. The java.net package's classes and interfaces are described in detail throughout the remainder of this chapter. Table 33.1 lists all classes, interfaces, and exceptions that have public members. Figure 33.1 shows a graphic representation of the hierarchy of the package.

Figurre 33.1: Contents of package java.net.

Table 33.1. Contents of package java.net.

Class IndexInterface Index Exception Index
ContentHandler ContentHandlerFactory MalformedURLException
DatagramPacket SocketImplFactory ProtocolException
DatagramSocket URLStreamHandlerFactory SocketException
InetAddress  UnknownHostException
ServerSocket  UnknownServiceException
Socket   
SocketImpl   
URL   
URLConnection   
URLEncoder   
URLStreamHandler   

ContentHandler

Object

The class hierarchy for the ContentHandler class derives from class java.lang.Object. (See Listing 33.1.) The ContentHandler class is used as a base class for classes that will handle specific MIME content types. The ContentHandlerFactory interface is used to select the appropriate ContentHandler for a given content type. ContentHandler's overall derivation can be seen in Figure 33.1.


Listing 33.1. Public members of java.net.ContentHandler.
abstract public class ContentHandler {
  abstract public Object getContent(URLConnection urlc) throws IOException
}

getContent

ContentHandler

abstract public Object getContent(URLConnection urlc)

The getContent() method accepts a URLConnection argument positioned at the beginning of an input stream and constructs an object from the input stream.

urlc is a URLConnection object representing the input stream to be read in by the ContentHandler.

An Object object that was constructed from the specified URLConnection.

An IOException if the input stream could not be read.

DatagramPacket

Object

The class hierarchy for the DatagramPacket class derives from class java.lang.Object. (See Listing 33.2.) The DatagramPacket class is used to store packet data such as data, length, Internet address, and port. DatagramPacket's overall derivation can be seen in Figure 33.1.


Listing 33.2. Public members of java.net.DatagramPacket.
public final class DatagramPacket {
  public DatagramPacket(byte ibuf[], int ilength)
  public DatagramPacket(byte ibuf[], int ilength, InetAddress iaddr, int iport)
  public InetAddress getAddress()
  public int getPort()
  public byte[] getData()
  public int getLength()
}

DatagramPacket

DatagramPacket

public DatagramPacket(byte ibuf[], int ilength)

This DatagramPacket constructor constructs a DatagramPacket object to be used for receiving datagrams.

Ibuf is an array of bytes that will be used to store the DatagramPacket.

ilength is an integer value specifying the size of the DatagramPacket.

DatagramPacket

DatagramPacket

public DatagramPacket(byte ibuf[], int ilength, InetAddress iaddr, int iport)

This DatagramPacket constructor constructs a DatagramPacket object to be sent.

ibuf is an array of bytes that will be used to store the DatagramPacket.

ilength is an integer value specifying the size of the DatagramPacket.

iaddr is the destination IP address.

iport is the destination port.

getAddress

DatagramPacket

public InetAddress getAddress()

The getAddress() method returns the IP address value of the DatagramPacket.

An InetAddress object containing the IP address of the DatagramPacket.

getPort

DatagramPacket

public int getPort()

The getPort() method returns the port value of the DatagramPacket.

An integer value containing the port address of the DatagramPacket.

getData

DatagramPacket

public byte[] getData()

The getData() method returns the array of DatagramPacket values.

An array of bytes containing the contents of the DatagramPacket.

getLength

DatagramPacket

public int getLength()

The getLength() method returns the length of the DatagramPacket.

An integer value containing the length of the DatagramPacket.

DatagramSocket

Object

The class hierarchy for the DatagramSocket class derives from class java.lang.Object. (See Listing 33.3.) The DatagramSocket class is used to designate a dedicated socket for implementing unreliable datagrams. These packages are unreliable because there is no verification scheme for ensuring that the packets were not corrupted during transmission. DatagramSocket's overall derivation can be seen in Figure 33.1.


Listing 33.3. Public members of java.net.DatagramSocket.
public class DatagramSocket {
  public DatagramSocket() throws SocketException
  public DatagramSocket(int port) throws SocketException
  public void send(DatagramPacket p) throws IOException
  public synchronized void receive(DatagramPacket p) throws IOException
  public int getLocalPort()
  public synchronized void close()
}

DatagramSocket

DatagramSocket

public DatagramSocket() throws SocketException

The DatagramSocket constructor is used to implement an unreliable Datagram connection.

None

This constructor throws a SocketException if the socket could not be created.

DatagramSocket

DatagramSocket

public DatagramSocket(int port) throws SocketException

This DatagramSocket constructor implements an unreliable Datagram connection using the specified port value.

port is an integer value specifying the port to be used for the socket.

This constructor throws a SocketException if the socket could not be created.

send

DatagramSocket

public void send(DatagramPacket p) throws IOException

The send() method sends a DatagramPacket to the destination address specified in the DatagramPacket's address value. For more information on the DatagramPacket class, see the documentation earlier in this chapter.

p is a DatagramPacket object containing data to be sent through the socket.

This method throws an IOException if an I/O exception has occurred.

receive

DatagramSocket

public synchronized void receive(DatagramPacket p) throws IOException

The receive() method receives a DatagramPacket. This method will block until the DatagramPacket has been received. For more information on the DatagramPacket class, see the documentation earlier in this chapter.

p is the DatagramPacket to be received.

This method throws an IOException if an I/O exception has occurred.

getLocalPort

DatagramSocket

public int getLocalPort()

The getLocalPort() method returns the port on the local machine to which this socket is bound.

An integer value containing the port value to which this socket is bound.

close

DatagramSocket

public synchronized void close()

The close() method closes the DatagramSocket.

none

InetAddress

Object

The class hierarchy for the InetAddress class derives from class java.lang.Object. (See Listing 33.4.) The InetAddress class is used to represent Internet addresses. InetAddress's overall derivation can be seen in Figure 33.1.


Listing 33.4. Public members of java.net.InetAddress.
public final class InetAddress {
  public String getHostName()
  public byte[] getAddress()
  public String getHostAddress()
  public int hashCode()
  public boolean equals(Object obj)
  public String toString()
  public static synchronized InetAddress getByName(String host) throws
  
 UnknownHostException
  public static synchronized InetAddress getAllByName(String host)[] throws
  
 UnknownHostException
  public static InetAddress getLocalHost() throws UnknownHostException
}

getHostName

InetAddress

public String getHostName()

The getHostName() method returns the name of the host for this InetAddress. If the host is null, the returned string will contain any of the local machine's available network addresses.

A String object containing the name of the host for this InetAddress.

getAddress

InetAddress

public byte[] getAddress()

The getAddress() method returns an array of bytes containing the raw IP address in network byte order.

A byte array containing the raw IP address of this InetAddress in network byte order.

getHostAddress

InetAddress

public String getHostAddress()

The getHostAddress() method returns the IP address string %d.%d.%d.%d.

A String value containing the raw IP address using the standard IP address format (%d.%d.%d.%d).

hashCode

InetAddress

public int hashCode()

The hashCode() method returns a hash code for this InetAddress.

An integer value representing this InetAddress's hash code.

equals

InetAddress

public boolean equals(Object obj)

The equals() method is used to compare this InetAddress object to the specified object.

obj is the object to be compared with the InetAddress.

A Boolean value that will be true if the objects are equal, false if not.

toString

InetAddress

public String toString()

The toString() method is used to return a string representation of the InetAddress.

A String value containing information about the InetAddress.

getByName

InetAddress

public static synchronized InetAddress getByName(String host) throws
 UnknownHostException

The getByName() method returns an InetAddress object based on the specified hostname.

host is a string object specifying the name of the host.

An InetAddress object containing the internet address information for the specified host.

An UnknownHostException if the specified host is invalid or unknown.

getAllByName

InetAddress

public static synchronized InetAddress getAllByName(String host)[] throws
 UnknownHostException

The getAllByName() method returns an array of InetAddress objects representing all of the addresses for the specified host.

host is a String object specifying the name of the host.

An array of all corresponding InetAddresses for the specified host.

An UnknownHostException if the specified host is invalid or unknown.

getLocalHost

InetAddress

public static InetAddress getLocalHost() throws UnknownHostException

The getLocalHost() returns an InetAddress object representing the address of the local host.

An InetAddress object containing the Internet address information for the local host.

ServerSocket

Object

The class hierarchy for the ServerSocket class derives from class java.lang.Object. (See Listing 33.5.) The ServerSocket class is used to encapsulate a server socket. The setSocketFactory() method is used to change the ServerSocket's implementation based on specific firewalls. ServerSocket's overall derivation can be seen in Figure 33.1.


Listing 33.5. Public members of java.net.ServerSocket.
public final class ServerSocket {
  public ServerSocket(int port) throws IOException
  public ServerSocket(int port, int backlog) throws IOException
  public InetAddress getInetAddress()
  public int getLocalPort()
  public Socket accept() throws IOException
  public void close() throws IOException
  public String toString()
  public static synchronized void setSocketFactory(SocketImplFactory fac) throws
  
 IOException
}

ServerSocket

ServerSocket

public ServerSocket(int port) throws IOException

The ServerSocket() constructor creates a server socket on the specified port.

port is an integer value specifying the port to create the socket on.

An IOException if an I/O exception has occurred.

ServerSocket

ServerSocket

public ServerSocket(int port, int backlog) throws IOException

This ServerSocket() constructor creates a server socket on the specified port and listens to it for a specified time.

port is an integer value specifying the port to create the socket on.

backlog is used to specify the amount of time to listen to a connection.

An IOException if an I/O exception has occurred.

getInetAddress

ServerSocket

public InetAddress getInetAddress()

The getInetAddress() method returns an InetAddress object specifying the address to which this socket is connected. For more information on the InetAddress class, see the documentation earlier in this chapter.

An InetAddress object containing the address information to which the socket is connected.

getLocalPort

ServerSocket

public int getLocalPort()

The getLocalPort() method returns the local port on which the socket is currently listening.

An integer value representing the port on the local machine to which the ServerSocket is listening.

accept

ServerSocket

public Socket accept() throws IOException

The accept() method is used to accept a connection. This method will block all others until a connection is made.

A Socket object after the connection has been accepted.

An IOException if an I/O error occurred while waiting for the connection.

close

ServerSocket

public void close() throws IOException

The close() method closes the socket's connection.

An IOException if an I/O error occurred while closing the server socket.

toString

ServerSocket

public String toString()

The toString() method returns a string representation of the ServerSocket.

A String object containing the implementation address and implementation port of the ServerSocket.

setSocketFactory

ServerSocket

public static synchronized void setSocketFactory(SocketImplFactory fac) throws
 IOException, SocketException

The setSocketFactory() method sets the server SocketImplFactory for use by this ServerSocket. This factory can only be set once. For more information on the SocketImplFactory interface, see the documentation later in this chapter.

fac is a SocketImplFactory interface to be used by this ServerSocket.

An IOException if there was an I/O error when setting the SocketImplFactory.

A SocketException if the SocketImplFactory has already been set.

Socket

Object

The class hierarchy for the Socket class derives from class java.lang.Object. (See Listing 33.6.) The Socket class is used to implement socket functionality. The setSocketImplFactory() method is used to change the Socket's implementation based on specific firewalls. Socket's overall derivation can be seen in Figure 33.1.


Listing 33.6. Public members of java.net.Socket.
public final class Socket {
  public Socket(String host, int port) throws UnknownHostException, IOException
  public Socket(String host, int port, boolean stream) throws IOException
  public Socket(InetAddress address, int port) throws IOException
  public Socket(InetAddress address, int port, boolean stream) throws
  
  IOException
  public InetAddress getInetAddress()
  public int getPort()
  public int getLocalPort()
  public InputStream getInputStream() throws IOException
  public OutputStream getOutputStream() throws IOException
  public synchronized void close() throws IOException
  public String toString()
  public static synchronized void setSocketImplFactory(SocketImplFactory fac)
  
 throws IOException
}

Socket

Socket

public Socket(String host, int port) throws UnknownHostException, IOException

This Socket() constructor creates a stream socket to the specified port on the specified host.

host is a String object containing the host name to create the socket on.

port is an integer value representing the port to create the socket on.

An UnknownHostException if the host name is unrecognized or invalid.

An IOException if an I/O error occurred while creating the socket.

Socket

Socket

public Socket(String host, int port, boolean stream) throws IOException

This Socket() constructor creates a stream socket to the specified port on the specified host. The Boolean stream value can be used to specify a stream socket or a datagram socket.

host is a String object containing the host name to create the socket on.

port is an integer value representing the port to create the socket on.

stream is a Boolean value that is true if a stream socket is to be created, false if a datagram socket is to be created.

An IOException if an I/O error occurred while creating the socket.

Socket

Socket

public Socket(InetAddress address, int port) throws IOException

This Socket() constructor creates a stream socket to the specified port at the specified InetAddress. For more information on the InetAddress class, see the documentation earlier in this chapter.

address is an InetAddress specifying the address at which to create the socket.

port is an integer value representing the port on which to create the socket.

An IOException if an I/O error occurred while creating the socket.

Socket

Socket

public Socket(InetAddress address, int port, boolean stream) throws IOException

This Socket() constructor creates a stream socket to the specified port at the specified address. The Boolean stream value can be used to specify a stream socket or a datagram socket. For more information on the InetAddress class, see the documentation earlier in this chapter.

address is an InetAddress specifying the address at which to create the socket.

port is an integer value representing the port on which to create the socket.

stream is a Boolean value that is true if a stream socket is to be created, false if a datagram socket is to be created.

An IOException if an I/O error occurred while creating the socket.

getInetAddress

Socket

public InetAddress getInetAddress()

The getInetAddress() method is used to return the address to which the socket is connected. For more information on the InetAddress class, see the documentation earlier in this chapter.

An InetAddress object containing information about the address to which the socket is connected.

getPort

Socket

public int getPort()

The getPort() method returns the remote port to which the socket is connected.

An integer value representing the remote port number to which the socket is connected.

getLocalPort

Socket

public int getLocalPort()

The getLocalPort() method returns the local port to which the socket is connected.

An integer value representing the local port number to which the socket is connected.

getInputStream

Socket

public InputStream getInputStream() throws IOException

The getInputStream() method returns an InputStream for this socket. For more information on the InputStream class, see the documentation in Chapter 31, "Package java.io."

An InputStream object to be used as the socket's input stream.

An IOException if an I/O error occurred while retrieving the input stream.

getOutputStream

Socket

public OutputStream getOutputStream() throws IOException

The getOutputStream() method returns an OutputStream for this socket. For more information on the OutputStream class, see the documentation in Chapter 31.

An OutputStream object to be used as the socket's output stream.

An IOException if an I/O error occurred while retrieving the output stream.

close

Socket

public synchronized void close() throws IOException

The close() method closes the socket's connection.

An IOException if an I/O error occurred while closing the socket.

toString

Socket

public String toString()

The toString() method returns a String representation of the socket.

A String object containing the Socket information.

setSocketImplFactory

Socket

public static synchronized void setSocketImplFactory(SocketImplFactory fac)
 throws IOException

The setSocketImplFactory() method sets the SocketImplFactory interface for this socket. The factory can only be specified once. For more information on the SocketImplFactory interface, see the documentation later in this chapter.

fac is a SocketImplFactory interface to be used by this Socket.

An IOException if an I/O error occurred while setting the SocketImplFactory.

SocketImpl

Object

The class hierarchy for the SocketImpl class derives from class java.lang.Object. (See Listing 33.7.) The SocketImpl class is an abstract base class provided as a template for socket implementations. SocketImpl's overall derivation can be seen in Figure 33.1.


Listing 33.7. Public members of java.net.SocketImpl.
public abstract class SocketImpl {
 public String toString()
}

toString

SocketImpl

public String toString()

The toString() method returns a String representation of the SocketImpl class.

A String object containing the port and address of this socket.

URL

Object

The class hierarchy for the URL class derives from class java.lang.Object. (See Listing 33.8.) The URL class is used to represent a Uniform Resource Locator. A URL is a reference to an object on the Web such as an FTP site, an e-mail address, or an HTML page on a Web server. URL's overall derivation can be seen in Figure 33.1.


Listing 33.8. Public members of java.net.URL.
public final class URL {
  public URL(String protocol, String host, int port, String file) throws
  
 MalformedURLException
  public URL(String protocol, String host, String file) throws
  
 MalformedURLException
  public URL(String spec) throws MalformedURLException
  public URL(URL context, String spec) throws MalformedURLException
  public int getPort()
  public String getProtocol()
  public String getHost()
  public String getFile()
  public String getRef()
  public boolean equals(Object obj)
  public int hashCode()
  public boolean sameFile(URL other)
  public String toString()
  public String toExternalForm()
  public URLConnection openConnection() throws java.io.IOException
  public final InputStream openStream() throws java.io.IOException
  public final Object getContent() throws java.io.IOException
  public static synchronized void
  
 setURLStreamHandlerFactory(URLStreamHandlerFactory fac)
}

URL

URL

public URL(String protocol, String host, int port, String file) throws
 MalformedURLException

This URL() constructor creates a URL using the specified protocol, host, port, and host filename.

protocol is a String object specifying the protocol to be used.

host is a String object specifying the host name.

port is an integer value specifying the port.

file is a String object specifying the filename on the host.

A MalformedURLException if the protocol was unknown or invalid.

URL

URL

public URL(String protocol, String host, String file) throws
 MalformedURLException

This URL() constructor creates a URL using the specified protocol, host, and host filename. The port number will be the default port used for the specified protocol.

protocol is a String object specifying the protocol to be used.

host is a String object specifying the host name.

file is a String object specifying the filename on the host.

A MalformedURLException if the protocol was unknown or invalid.

URL

URL

public URL(String spec) throws MalformedURLException

This URL() constructor creates a URL using the specified unparsed URL.

spec is a String object containing an unparsed URL string.

A MalformedURLException if the specified unparsed URL was invalid.

URL

URL

public URL(URL context, String spec) throws MalformedURLException

This URL() constructor creates a URL using the specified context and unparsed URL. If the unparsed URL is an absolute URL, it is used as is; otherwise, it is used in combination with the specified context. The context may be null.

context is a URL object specifying the context to be used in combination with the unparsed URL string (spec parameter).

spec is a String object containing an unparsed URL string.

A MalformedURLException if the specified unparsed URL was invalid.

getPort

URL

public int getPort()

The getPort() method returns the port number for this URL.

An integer value representing the port number for this URL. This will be -1 if the port has not been set.

getProtocol

URL

public String getProtocol()

The getProtocol()method returns a string representing the protocol used by this URL.

A String object containing the protocol name.

getHost

URL

public String getHost()

The getHost() method returns a string containing the host name.

A String object containing the host name.

getFile

URL

public String getFile()

The getFile() method returns a string containing the host filename.

A String object containing the name of the file on the host.

getRef

URL

public String getRef()

The getRef() method returns the ref (if any) that was specified in the unparsed string used to create this URL.

A String object containing the URL's ref.

equals

URL

public boolean equals(Object obj)

The equals() method can be used to compare this URL to another object.

obj is an Object object that will be compared with this URL.

A Boolean value that will be true if the objects are equals, false if not.

hashCode

URL

public int hashCode()

The hashCode() method will return a hash code value for the URL.

An integer value representing the hash code value of this URL.

sameFile

URL

public boolean sameFile(URL other)

The sameFile() method can be used to determine if the specified file is the same file used to create this URL.

other is a URL object specifying the location of another file.

A Boolean value that will be true if the files are equals, false if not.

toString

URL

public String toString()

The toString() method returns a string representation of the URL.

A String object containing a textual representation of the URL including the protocol, host, port, and filename.

toExternalForm

URL

public String toExternalForm()

The toExternalForm() method is used to reverse the parsing of the URL.

A String object containing the textual representation of the fully qualified URL (after it has been unparsed).

openConnection

URL

public URLConnection openConnection() throws java.io.IOException

The openConnection() method will open a URLConnection to the object specified by the URL. For more information on the URLConnection class, see the documentation later in this chapter.

A URLConnection object that represents a connection to the URL.

An IOException if an I/O error occurred while creating the URLConnection.

openStream

URL

public final InputStream openStream() throws java.io.IOException

The openStream() method opens an InputStream. For more information on the InputStream class, see the documentation in Chapter 31.

An InputStream representing an input stream for the URL.

An IOException if an I/O error occurred while creating the input stream.

getContent

URL

public final Object getContent() throws java.io.IOException

The getContent() method retrieves the contents from the opened connection.

An Object object representing the contents that are retrieved from the connection.

An IOException if an I/O error occurred while retrieving the content.

setURLStreamHandlerFactory

URL

public static synchronized void
 setURLStreamHandlerFactory(URLStreamHandlerFactory fac)

The setURLStreamHandlerFactory() method sets the URLStreamHandlerFactory interface for this URL. The factory can only be specified once. For more information on the URLStreamHandlerFactory interface, see the documentation later in this chapter.

Fac is a URLStreamHandlerFactory interface to be used by this URL.

An Error if this factory has already been specified.

URLConnection

Object

The class hierarchy for the URLConnection class derives from class java.lang.Object. (See Listing 33.9.) The URLConnection class is an abstract base class used to represent a URL connection. It must be subclassed in order to provide true functionality. URLConnection's overall derivation can be seen in Figure 33.1.


Listing 33.9. Public members of java.net.URLConnection.
abstract public class URLConnection {
  abstract public void connect() throws IOException
  public URL getURL()
  public int getContentLength()
  public String getContentType()
  public String getContentEncoding()
  public long getExpiration()
  public long getDate()
  public long getLastModified()
  public String getHeaderField(String name)
  public int getHeaderFieldInt(String name, int Default)
  public long getHeaderFieldDate(String name, long Default)
  public String getHeaderFieldKey(int n)
  public String getHeaderField(int n)
  public Object getContent() throws IOException
  public InputStream getInputStream() throws IOException
  public OutputStream getOutputStream() throws IOException
  public String toString()
  public void setDoInput(boolean doinput)
  public void setDoOutput(boolean dooutput)
  public boolean getDoOutput()
  public void setAllowUserInteraction(boolean allowuserinteraction)
  public boolean getAllowUserInteraction()
  public static void
  
 setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)
  public static boolean getDefaultAllowUserInteraction()
  public void setUseCaches(boolean usecaches)
  public boolean getUseCaches()
  public void setIfModifiedSince(long ifmodifiedsince)
  public long getIfModifiedSince()
  public boolean getDefaultUseCaches()
  public void setDefaultUseCaches(boolean defaultusecaches)
  public void setRequestProperty(String key, String value)
  public String getRequestProperty(String key)
  public static void setDefaultRequestProperty(String key, String value)
  public static String getDefaultRequestProperty(String key)
  public static synchronized void
  
 setContentHandlerFactory(ContentHandlerFactory fac)
}

connect

URLConnection

abstract public void connect() throws IOException

The connect() method is used to connect the URLConnection after it has been created. Operations that depend on being connected will call this method to automatically connect. Calling this method after the connection has already been made does nothing.

An IOException if an I/O error occurred while the connection was attempted.

getURL

URLConnection

public URL getURL()

The getURL() method returns the URL for this URLConnection.

A URL object. For more information on the URL class, see the documentation earlier in this chapter.

getContentLength

URLConnection

public int getContentLength()

The getContentLength() method returns the length of the content.

An integer value containing the length of the content. This value will be -1 if the length is not known.

getContentType

URLConnection

public String getContentType()

The getContentType() method returns the type of the content.

A String object containing the type of the content. This value will be null if the type is not known.

getContentEncoding

URLConnection

public String getContentEncoding()

The getContentEncoding() method returns the encoding of the content.

A String object containing the encoding of the content. This value will be null if the encoding is not known.

getExpiration

URLConnection

public long getExpiration()

The getExpiration() method will return the expiration of the object.

A long value containing the expiration of the object. This value will be 0 if the expiration is not known.

getDate

URLConnection

public long getDate()

The getDate() method will return the date of the object.

A long value containing the date of the object. This value will be 0 if the date is not known.

getLastModified

URLConnection

public long getLastModified()

The getLastModified() will return the last modified date of the object.

A long value containing the last modified date of the object. This value will be 0 if the last modified date is not known.

getHeaderField

URLConnection

public String getHeaderField(String name)

The getHeaderField() method returns the contents of the header field based on the specified field name.

name is a String object specifying the name of the header field to be returned.

A String object containing the contents of the specified header field. This value will be null if the contents are not known.

getHeaderFieldInt

URLConnection

public int getHeaderFieldInt(String name, int Default)

The getHeaderFieldInt() method returns the preparsed contents of the specified header field.

name is a String object specifying the name of the header field to be returned.

Default is an integer value containing the value to be returned if the field is missing.

An integer value containing the preparsed header field value.

getHeaderFieldDate

URLConnection

public long getHeaderFieldDate(String name, long Default)

The getHeaderFieldDate() method returns the contents of the specified header field parsed as a date.

name is a String object specifying the name of the header field to be returned.

Default is an integer value containing the value to be returned if the field is missing.

A long value containing the header field value parsed as a date.

getHeaderFieldKey

URLConnection

public String getHeaderFieldKey(int n)

The getHeaderFieldKey() method returns the key for the specified header field.

n is the position of the header field to be returned.

A String object containing the key for the specified header field. This value will be null if there are fewer than n header fields.

getHeaderField

URLConnection

public String getHeaderField(int n)

The getHeaderField() method returns the specified header field value.

n is the position of the header field to be returned.

A String object containing the contents of the specified header field. This value will be null if there are fewer than n header fields.

getContent

URLConnection

public Object getContent() throws IOException

The getContent() method returns the object referred to by this URLConnection.

An Object object that was referred to by this URL.

An IOException if an I/O error occurred while retrieving the content.

getInputStream

URLConnection

public InputStream getInputStream() throws IOException

The getInputStream() method returns an InputStream object to be used as an input stream to read from the object. For more information on the InputStream class, see the documentation in Chapter 31.

An InputStream object to be used to read from the object.

An IOException if an I/O error occurred while creating the input stream.

getOutputStream

URLConnection

public OutputStream getOutputStream() throws IOException

The getOutputStream() method returns an OutputStream object to be used as an output stream to write to the object. For more information on the OutputStream class, see the documentation in Chapter 31Z.

An OutputStream object to be used to write to the object.

An IOException if an I/O error occurred while creating the output stream.

toString

URLConnection

public String toString()

The toString() method returns a string representation of the URLConnection.

A String object containing a textual representation of the URLConnection object.

setDoInput

URLConnection

public void setDoInput(boolean doinput)

The setDoInput() method sets the functionality of the URLConnection. If the parameter is true, the URLConnection will be used for input. If it is false, it will be used for output. (The default functionality is input.)

doinput is a Boolean value that will be true if the URLConnection is to be used for input, false if for output.

setDoOutput

URLConnection

public void setDoOutput(boolean dooutput)

The setDoOutput() method sets the functionality of the URLConnection. If the parameter is true, the URLConnection will be used for output. If it is false, it will be used for input. (The default functionality is input.)

dooutput is a Boolean value that will be true if the URLConnection is to be used for output, false if for input.

getDoOutput

URLConnection

public boolean getDoOutput()

The getDoOutput() method returns the input/output functionality of the URLConnection.

A Boolean value that will be true if the URLConnection is used for output, false if it is used for input.

setAllowUserInteraction

URLConnection

public void setAllowUserInteraction(boolean allowuserinteraction)

The setAllowUserInteraction() method allows the protocol to interact with the user. For example, a dialog box may need to be displayed to the user.

allowuserinteraction is a Boolean value that should be true if user interaction is allowed, false if not.

getAllowUserInteraction

URLConnection

public boolean getAllowUserInteraction()

The getAllowUserInteraction() method can be called to determine if user interaction is allowed (see setAllowUserInteraction()).

A Boolean value that will be true if user interaction is allowed, false if not.

setDefaultAllowUserInteraction

URLConnection

public static void
 setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)

The setDefaultAllowUserInteraction() method allows the default user interaction value to be set for all URLConnections because it is a static method.

Defaultallowuserinteraction is a Boolean value that should be true if user interaction is allowed, false if not.

GetDefaultAllowUserInteraction

URLConnection

public static boolean getDefaultAllowUserInteraction()

The getDefaultAllowUserInteraction() static method returns the default user interaction value.

A Boolean value that will be true if user interaction is allowed, false if not.

setUseCaches

URLConnection

public void setUseCaches(boolean usecaches)

The setUseCaches() method is used to control the use of caching by the protocol. Some protocols allow files to be cached.

usecaches is a Boolean value that will be true if caching is to be used by the protocol, false if not.

getUseCaches

URLConnection

public boolean getUseCaches()

The getUseCaches() method can be called to determine if caching is to be used by the protocol.

A Boolean value that will be true if caching is to be used by the protocol, false if not.

setIfModifiedSince

URLConnection

public void setIfModifiedSince(long ifmodifiedsince)

The setIfModifiedSince()method is provided to set the internal ifmodifiedsince variable of the URLConnection class. Because some protocols allow caching of files, if the file to be retrieved is newer than ifmodifiedsince, it will need to be retrieved from the URL (rather than the cache).

ifmodifiedsince is a long value used to represent the ifmodifiedsince date.

getIfModifiedSince

URLConnection

public long getIfModifiedSince()

The getIfModifiedSince() method returns the internal ifmodifiedsince date value. See the setIfModifiedSince() method documentation.

A long value representing the ifmodifiedsince date value.

getDefaultUseCaches

URLConnection

public boolean getDefaultUseCaches()

The getDefaultUseCaches() method can be called to determine if caches are used by default. Because this value is static, it will apply to all current and future URLConnections.

A Boolean value that will be true if caches are used by default, false if not.

setDefaultUseCaches

URLConnection

public void setDefaultUseCaches(boolean defaultusecaches)

The setDefaultUseCaches() method can be used to force all URLConnections to use caching by default because it is a static value.

defaultusecaches is a Boolean value that should be true if caches are to be used by default, false if not.

setRequestProperty

URLConnection

public void setRequestProperty(String key, String value)

The setRequestProperty() method is used to set URLConnection properties.

key is a String object containing the key by which the property is known.

value is a String object containing the property value.

getRequestProperty

URLConnection

public String getRequestProperty(String key)

The getRequestProperty() method returns the value for the specified property key.

key is a String object containing the key by which the property is known.

A String object containing the specified property's value.

setDefaultRequestProperty

URLConnection

public static void setDefaultRequestProperty(String key, String value)

The setDefaultRequestProperty() method sets the default value of a specified property. All current and future URLConnections will be initialized with these properties.

key is a String object containing the key by which the property is known.

value is a String object containing the specified property's value.

getDefaultRequestProperty

URLConnection

public static String getDefaultRequestProperty(String key)

The getDefaultRequestProperty() method gets the default value of a specified property.

key is a String object containing the key by which the property is known.

A String object containing the specified property's value.

SetContentHandlerFactory

URLConnection

public static synchronized void
 setContentHandlerFactory(ContentHandlerFactory fac)

The setContentHandlerFactory()method is used to set the ContentHandlerFactory interface for this URLConnection. The factory may only be set once. For more information on the ContentHandlerFactory interface, see the documentation later in this chapter.

Fac is a ContentHandlerFactory interface.

An error if the ContentHandlerFactory has already been defined.

URLEncoder

Object

The class hierarchy for the URLEncoder class derives from class java.lang.Object. (See Listing 33.10.) The URLEncoder class is used to encode text into x-www-form-urlencoded format. URLEncoder's overall derivation can be seen in Figure 33.1.


Listing 33.10. Public members of java.net.URLEncoder.
public class URLEncoder {
   public static String encode(String s)
}

encode

URLEncoder

public static String encode(String s)

The encode() method is used to translate a string into x-www-form-urlencoded format.

s is a String object that will be translated by the encode() method.

A String object in x-www-form-urlencoded format.

URLStreamHandler

Object

The class hierarchy for the URLStreamHandler class derives from class java.lang.Object. The URLStreamHandler class is an abstract base class used to open URL connection streams. It must be subclassed in order to provide true functionality. URLStreamHandler's overall derivation can be seen in Figure 33.1.

Listing 33.11 shows the declarations for all of the methods included in the class java.awt.net.URLStreamHandler.


Listing 33.11. Public members of java.awt.net.URLStreamHandler.
public class URLStreamHandler extends Object {
  public URLStreamHandler()
  protected abstract URLConnection openConnection(URL u) throws IOException
  protected void parseURL(URL u, String spec, int start, int limit)
  protected String toExternalForm(URL u)
  protected void setURL(URL u, String protocol, String host, int port,
  
 String file, String ref)
}

URLStreamHandler

public URLStreamHandler()

This URLStreamHandler() method constructs a URLStreamHandler. After this object has been constructed, the openConnection() or setURL() methods are commonly called to perform operations with the URL connection stream.

None

none

OpenConnection

URLStreamHandler

protected abstract URLConnection openConnection(URL u) throws IOException

The openConnection() method is to be overridden by a subclass of URLStreamHandler. This method should open an input stream to the referenced URL.

u references the URL stream to be opened.

A URLConnection class that represents an active connection. For more information on the URLConnection class, see the documentation earlier in this chapter.

parseURL

URLStreamHandler

protected void parseURL(URL u, String spec, int start, int limit)

The parseURL() method is to be overridden by a subclass of URLStreamHandler. This method should parse the spec string into the URL u using the specified start and limit characters of the string.

u references the URL referenced by the spec string.

spec is a String containing a URL to be parsed.

start references the character position in spec to start parsing at.

limit is the character position in spec to stop parsing at.

toExternalForm

URLStreamHandler

protected String toExternalForm(URL u)
The toExternalForm() method is to be overridden by a subclass of URLStreamHandler. This method should "unparse" the specified URL and return a String.

u references the URL stream to be unparsed.

A String object containing the unparsed contents of the URL object.

setURL

URLStreamHandler

protected void setURL(URL u, String protocol, String host, int port,
 String file, String ref)

The setURL() method is to be overridden by a subclass of URLStreamHandler. This method should call the set() method of the specified URL u to set the fields of the URL. For more information on the set() method of the URL class, see the documentation earlier in this chapter.

u references the URL whose fields will be set by this method.

protocol is a String containing the protocol to use for the URL.

host references the host machine pointed to by the URL.

port is the protocol port that will be connected to by the URL.

file is a String containing the file that is located on the host.

Ref is the reference value used by the URL.

MalformedURLException

IOException

The class hierarchy for the MalformedURLException class derives from class java.io.IOException. (See Listing 33.12.) The MalformedURLException class is a class used to signal a malformed URL. MalformedURLException's overall derivation can be seen in Figure 33.1.


Listing 33.12. Public members of java.net.MalformedURLException.
public class MalformedURLException extends IOException {
  public MalformedURLException()
  public MalformedURLException(String msg)
}

MalformedURLException

MalformedURLException

public MalformedURLException()

This MalformedURLException() method constructs a MalformedURLException with no detail message to describe the exception.

None

none

MalformedURLException

MalformedURLException

public MalformedURLException(String msg)

This MalformedURLException() method constructs a MalformedURLException using the specified detail message to describe the exception.

msg is a String object containing the detail message.

ProtocolException

IOException

The class hierarchy for the ProtocolException class derives from class java.io.IOException. (See Listing 33.13.) This exception signals when a connect receives an EPROTO message. This exception is used by the Socket class. ProtocolException's overall derivation can be seen in Figure 33.1.


Listing 33.13. Public members of java.net.ProtocolException.
public class ProtocolException extends IOException {
  public ProtocolException(String host)
  public ProtocolException()
}

ProtocolException

ProtocolException

public ProtocolException()

This ProtocolException() method constructs a ProtocolException with no detail message to describe the exception.

None

none

ProtocolException

ProtocolException

public ProtocolException(String host)

This ProtocolException() method constructs a ProtocolException using the specified detail message to describe the exception.

Host is a String object containing the detail message.

SocketException

IOException

The class hierarchy for the SocketException class derives from class java.io.IOException. (See Listing 33.14.) This exception signals when an error has occurred while trying to use a socket. This exception is used by the Socket class. SocketException's overall derivation can be seen in Figure 33.1.


Listing 33.14. Public members of java.net.SocketException.
public class SocketException extends IOException {
  public SocketException(String host)
  public SocketException()
}

SocketException

SocketException

public SocketException()

This SocketException() method constructs a SocketException with no detail message to describe the exception.

None

none

SocketException

SocketException

public SocketException(String host)

This SocketException() method constructs a SocketException using the specified detail message to describe the exception.

msg is a String object containing the detail message.

UnknownHostException

IOException

The class hierarchy for the UnknownHostException class derives from class java.io.IOException. (See Listing 33.15.) This exception signals that the host address specified by the client cannot be resolved. UnknownHostException's overall derivation can be seen in Figure 33.1.


Listing 33.15. Public members of java.net.UnknownHostException.
public class UnknownHostException extends IOException {
  public UnknownHostException(String host)
  public UnknownHostException()
}

UnknownHostException

UnknownHostException

public UnknownHostException()

This UnknownHostException() method constructs an UnknownHostException with no detail message to describe the exception.

none

none

UnknownHostException

UnknownHostException

public UnknownHostException(String host)

This UnknownHostException() method constructs a UnknownHostException using the specified detail message to describe the exception.

host is a String object containing the detail message.

UnknownServiceException

IOException

The class hierarchy for the UnknownServiceException class derives from class java.io.IOException. (See Listing 33.16.) This exception signals when an unknown service exception has occurred. UnknownServiceException's overall derivation can be seen in Figure 33.1.


Listing 33.16. Public members of java.net.UnknownServiceException.
public class UnknownServiceException extends IOException {
  public UnknownServiceException()
  public UnknownServiceException(String msg)
}

UnknownServiceException

UnknownServiceException

public UnknownServiceException()

This UnknownServiceException() method constructs an UnknownServiceException with no detail message to describe the exception.

None

none

UnknownServiceException

UnknownServiceException

public UnknownServiceException(String host)

This UnknownServiceException() method constructs an UnknownServiceException using the specified detail message to describe the exception.

Host is a String object containing the detail message.

ContentHandlerFactory

The class hierarchy for the ContentHandlerFactory interface derives from class java.lang.Object. (See Listing 33.17.) This interface is used to create ContentHandlers for usage by various streams. ContentHandlerFactory's overall derivation can be seen in Figure 33.1.


Listing 33.17. Public members of java.net.ContentHandlerFactory.
public interface ContentHandlerFactory{
  public abstract ContentHandler createContentHandler(String mimetype)
}

createContentHandler

ContentHandlerFactory

public abstract ContentHandler createContentHandler(String mimetype)

The createContentHandler() method creates a new content handler to read the content from a URLStreamHandler using the specified mime type. For more information on the URLStreamHandler class, see the documentation later in this chapter.

mimetype is a String object containing the mime type of the content.

A ContentHandler object that will read data from a URLConnection and construct an object. For more information on the ContentHandler class, see the documentation earlier in this chapter.

SocketImplFactory

The class hierarchy for the SocketImplFactory interface derives from class java.lang.Object. (See Listing 33.18.) This interface is used by the socket class to specific socket implementations. SocketImplFactory's overall derivation can be seen in Figure 33.1.


Listing 33.18. Public members of java.net.SocketImplFactory.
public interface SocketImplFactory {
  SocketImpl createSocketImpl()
}

createSocketImpl

SocketImplFactory

SocketImpl createSocketImpl()

The createSocketImpl() method creates a SocketImpl instance that is an implementation of a socket. For more information on the SocketImpl class, see the documentation earlier in this chapter.

A SocketImpl object that provides a socket implementation.

URLStreamHandlerFactory

The class hierarchy for the URLStreamHandlerFactory interface derives from class java.lang.Object. (See Listing 33.19.) This interface is used by the URL class to create stream handlers for various stream types. URLStreamHandlerFactory's overall derivation can be seen in Figure 33.1.


Listing 33.19. Public members of java.net.URLStreamHandlerFactory.
public interface URLStreamHandlerFactory {
  URLStreamHandler createURLStreamHandler(String protocol)
}

createURLStreamHandler

URLStreamHandlerFactory

URLStreamHandler createURLStreamHandler(String protocol)

The createURLStreamHandler() method creates a URLStreamHandler instance for use by the URL class based on the specified protocol. For more information on the URLStreamHandler class, see the documentation earlier in this chapter.

protocol is a String object that specifies the protocol to be used by the URLStreamHandler class.

A URLStreamHandler class that is created with the protocol specified in the input para-meter.