bb.net
Class NetUtil

java.lang.Object
  extended by bb.net.NetUtil

public final class NetUtil
extends Object

Provides static utility methods that deal with networking.

This class is multithread safe: it is immutable (both its immediate state, as well as the deep state of its fields).

Author:
Brent Boyer

Nested Class Summary
static class NetUtil.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Constructor Summary
private NetUtil()
          This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.
 
Method Summary
static URL getFtpUrl(String filenameRemote, String host, String username, String password)
          Returns a new URL instance that specifies filenameRemote on the remote host.
static String getSocketState(Socket socket)
          Returns a String which lists all of socket's publicly accessible state.
static boolean isValidPort(int port)
          Determines whether or not port is a valid TCP or UDP port number.
static void writeFileToUrl(File file, URL url, PrintWriter logger)
          Writes file to url.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetUtil

private NetUtil()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.

Method Detail

isValidPort

public static boolean isValidPort(int port)
Determines whether or not port is a valid TCP or UDP port number. The requirement is that port be representable by an unsigned 2 byte integer (i.e. 0 <= port <= NumberUtil.unsigned2ByteMaxValue). See TCP and UDP port.


getSocketState

public static String getSocketState(Socket socket)
                             throws IllegalArgumentException,
                                    SocketException
Returns a String which lists all of socket's publicly accessible state.

Throws:
IllegalArgumentException - if any socket == null
SocketException - if there is an error in the underlying protocol, such as a TCP error

getFtpUrl

public static URL getFtpUrl(String filenameRemote,
                            String host,
                            String username,
                            String password)
                     throws IllegalArgumentException,
                            RuntimeException
Returns a new URL instance that specifies filenameRemote on the remote host. The ftp protocol in binary mode is used, with username and password as login credentials. In particular, the returned URL has the string value
"ftp://" + username + ":" + password + "@" + host + "/" + filenameRemote + ";type=i"
Thus, the host value should not end with a '/' char, nor should filenameRemote begin with a '/' char.

Warning: the ftp protocol is completely insecure, with the username and password along with all data being sent in the clear.

Throws:
IllegalArgumentException - if any arg is null
RuntimeException - if any arg causes the ftp URL to be malformed
See Also:
ftp URLs, ftp passwords sent in clear, Network Security

writeFileToUrl

public static void writeFileToUrl(File file,
                                  URL url,
                                  PrintWriter logger)
                           throws IllegalArgumentException,
                                  IOException
Writes file to url. Note that some protocols (e.g. FTP) will overwrite any previously existing remote file.

Parameters:
logger - is passed to StreamUtil.transfer(in, out, logger)
Throws:
IllegalArgumentException - if file is not valid; url == null
IOException - if an I/O problem occurs