Top Description Fields Constructors Methods
org.h2.util

public Class NetUtils

extends Object
Class Inheritance
Imports
java.io.IOException, java.net.BindException, .Inet6Address, .InetAddress, .InetSocketAddress, .ServerSocket, .Socket, .UnknownHostException, org.h2.api.ErrorCode, org.h2.engine.SysProperties, org.h2.message.DbException, org.h2.security.CipherFactory

This utility class contains socket helper functions.

Field Summary

Modifier and TypeField and Description
private static final int
private static InetAddress
private static String
private static long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ServerSocket

Returns:

null
closeSilently
(ServerSocket
the socket
socket
)

Close a server socket and ignore any exceptions.

public static Socket

Returns:

the socket
createLoopbackSocket
(int
the port
port
,
boolean
if SSL should be used
ssl
)

Create a loopback socket (a socket that is connected to localhost) on this port.

public static ServerSocket

Returns:

the server socket
createServerSocket
(int
the port to listen on
port
,
boolean
if SSL should be used
ssl
)

Create a server socket.

private static ServerSocket
createServerSocketTry(int port, boolean ssl)

public static Socket

Returns:

the socket
createSocket
(String
to connect to (including an optional port)
server
,
int
the default port (if not specified in the server address)
defaultPort
,
boolean
if SSL should be used
ssl
)

Create a client socket that is connected to the given address and port.

public static Socket

Returns:

the socket
createSocket
(String
to connect to (including an optional port)
server
,
int
the default port (if not specified in the server address)
defaultPort
,
boolean
if SSL should be used
ssl
,
int
socket so timeout
networkTimeout
)

Create a client socket that is connected to the given address and port.

public static Socket

Returns:

the socket
createSocket
(InetAddress
the address to connect to
address
,
int
the port
port
,
boolean
if SSL should be used
ssl
)

Create a client socket that is connected to the given address and port.

public static Socket

Returns:

the socket
createSocket
(InetAddress
the address to connect to
address
,
int
the port
port
,
boolean
if SSL should be used
ssl
,
int
socket so timeout
networkTimeout
)

Create a client socket that is connected to the given address and port.

private static InetAddress

Returns:

the bind address
getBindAddress
()

Get the bind address if the system property h2.bindAddress is set, or null if not.

public static String

Returns:

the host name, or another text if not available
getHostName
(String
the local address
localAddress
)

Get the host name of a local address, if available.

public static synchronized String

Returns:

the local host address
getLocalAddress
()

Get the local host address as a string.

public static StringBuilder

Returns:

the specified or the new string builder with short representation of specified address
ipToShortForm
(StringBuilder
string builder to append to, or null
builder
,
byte[]
IP address
address
,
boolean
if (true, add brackets around IPv6 addresses
addBrackets
)

Appends short representation of the specified IP address to the string builder.

public static boolean

Returns:

true if it is
isLocalAddress
(Socket
the socket
socket
)

Check if a socket is connected to a local address.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CACHE_MILLISback to summary
private static final int CACHE_MILLIS
cachedBindAddressback to summary
private static InetAddress cachedBindAddress
cachedLocalAddressback to summary
private static String cachedLocalAddress
cachedLocalAddressTimeback to summary
private static long cachedLocalAddressTime

Constructor Detail

NetUtilsback to summary
private NetUtils()

Method Detail

closeSilentlyback to summary
public static ServerSocket closeSilently(ServerSocket socket)

Close a server socket and ignore any exceptions.

Parameters
socket:ServerSocket

the socket

Returns:ServerSocket

null

createLoopbackSocketback to summary
public static Socket createLoopbackSocket(int port, boolean ssl) throws IOException

Create a loopback socket (a socket that is connected to localhost) on this port.

Parameters
port:int

the port

ssl:boolean

if SSL should be used

Returns:Socket

the socket

Exceptions
IOException:
on failure
createServerSocketback to summary
public static ServerSocket createServerSocket(int port, boolean ssl)

Create a server socket. The system property h2.bindAddress is used if set.

Parameters
port:int

the port to listen on

ssl:boolean

if SSL should be used

Returns:ServerSocket

the server socket

createServerSocketTryback to summary
private static ServerSocket createServerSocketTry(int port, boolean ssl)
createSocketback to summary
public static Socket createSocket(String server, int defaultPort, boolean ssl) throws IOException

Create a client socket that is connected to the given address and port.

Parameters
server:String

to connect to (including an optional port)

defaultPort:int

the default port (if not specified in the server address)

ssl:boolean

if SSL should be used

Returns:Socket

the socket

Exceptions
IOException:
on failure
createSocketback to summary
public static Socket createSocket(String server, int defaultPort, boolean ssl, int networkTimeout) throws IOException

Create a client socket that is connected to the given address and port.

Parameters
server:String

to connect to (including an optional port)

defaultPort:int

the default port (if not specified in the server address)

ssl:boolean

if SSL should be used

networkTimeout:int

socket so timeout

Returns:Socket

the socket

Exceptions
IOException:
on failure
createSocketback to summary
public static Socket createSocket(InetAddress address, int port, boolean ssl) throws IOException

Create a client socket that is connected to the given address and port.

Parameters
address:InetAddress

the address to connect to

port:int

the port

ssl:boolean

if SSL should be used

Returns:Socket

the socket

Exceptions
IOException:
on failure
createSocketback to summary
public static Socket createSocket(InetAddress address, int port, boolean ssl, int networkTimeout) throws IOException

Create a client socket that is connected to the given address and port.

Parameters
address:InetAddress

the address to connect to

port:int

the port

ssl:boolean

if SSL should be used

networkTimeout:int

socket so timeout

Returns:Socket

the socket

Exceptions
IOException:
on failure
getBindAddressback to summary
private static InetAddress getBindAddress() throws UnknownHostException

Get the bind address if the system property h2.bindAddress is set, or null if not.

Returns:InetAddress

the bind address

getHostNameback to summary
public static String getHostName(String localAddress)

Get the host name of a local address, if available.

Parameters
localAddress:String

the local address

Returns:String

the host name, or another text if not available

getLocalAddressback to summary
public static synchronized String getLocalAddress()

Get the local host address as a string. For performance, the result is cached for one second.

Returns:String

the local host address

ipToShortFormback to summary
public static StringBuilder ipToShortForm(StringBuilder builder, byte[] address, boolean addBrackets)

Appends short representation of the specified IP address to the string builder.

Parameters
builder:StringBuilder

string builder to append to, or null

address:byte[]

IP address

addBrackets:boolean

if (true, add brackets around IPv6 addresses

Returns:StringBuilder

the specified or the new string builder with short representation of specified address

isLocalAddressback to summary
public static boolean isLocalAddress(Socket socket) throws UnknownHostException

Check if a socket is connected to a local address.

Parameters
socket:Socket

the socket

Returns:boolean

true if it is

Exceptions
UnknownHostException:
on failure