Package org.h2.util
Class NetUtils
- java.lang.Object
-
- org.h2.util.NetUtils
-
public class NetUtils extends java.lang.Object
This utility class contains socket helper functions.
-
-
Field Summary
Fields Modifier and Type Field Description private static int
CACHE_MILLIS
private static java.net.InetAddress
cachedBindAddress
private static java.lang.String
cachedLocalAddress
private static long
cachedLocalAddressTime
-
Constructor Summary
Constructors Modifier Constructor Description private
NetUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.net.ServerSocket
closeSilently(java.net.ServerSocket socket)
Close a server socket and ignore any exceptions.static java.net.Socket
createLoopbackSocket(int port, boolean ssl)
Create a loopback socket (a socket that is connected to localhost) on this port.static java.net.ServerSocket
createServerSocket(int port, boolean ssl)
Create a server socket.private static java.net.ServerSocket
createServerSocketTry(int port, boolean ssl)
static java.net.Socket
createSocket(java.lang.String server, int defaultPort, boolean ssl)
Create a client socket that is connected to the given address and port.static java.net.Socket
createSocket(java.lang.String server, int defaultPort, boolean ssl, int networkTimeout)
Create a client socket that is connected to the given address and port.static java.net.Socket
createSocket(java.net.InetAddress address, int port, boolean ssl)
Create a client socket that is connected to the given address and port.static java.net.Socket
createSocket(java.net.InetAddress address, int port, boolean ssl, int networkTimeout)
Create a client socket that is connected to the given address and port.private static java.net.InetAddress
getBindAddress()
Get the bind address if the system property h2.bindAddress is set, or null if not.static java.lang.String
getHostName(java.lang.String localAddress)
Get the host name of a local address, if available.static java.lang.String
getLocalAddress()
Get the local host address as a string.static java.lang.StringBuilder
ipToShortForm(java.lang.StringBuilder builder, byte[] address, boolean addBrackets)
Appends short representation of the specified IP address to the string builder.static boolean
isLocalAddress(java.net.Socket socket)
Check if a socket is connected to a local address.
-
-
-
Field Detail
-
CACHE_MILLIS
private static final int CACHE_MILLIS
- See Also:
- Constant Field Values
-
cachedBindAddress
private static java.net.InetAddress cachedBindAddress
-
cachedLocalAddress
private static java.lang.String cachedLocalAddress
-
cachedLocalAddressTime
private static long cachedLocalAddressTime
-
-
Method Detail
-
createLoopbackSocket
public static java.net.Socket createLoopbackSocket(int port, boolean ssl) throws java.io.IOException
Create a loopback socket (a socket that is connected to localhost) on this port.- Parameters:
port
- the portssl
- if SSL should be used- Returns:
- the socket
- Throws:
java.io.IOException
- on failure
-
createSocket
public static java.net.Socket createSocket(java.lang.String server, int defaultPort, boolean ssl) throws java.io.IOException
Create a client socket that is connected to the given address and port.- Parameters:
server
- to connect to (including an optional port)defaultPort
- the default port (if not specified in the server address)ssl
- if SSL should be used- Returns:
- the socket
- Throws:
java.io.IOException
- on failure
-
createSocket
public static java.net.Socket createSocket(java.lang.String server, int defaultPort, boolean ssl, int networkTimeout) throws java.io.IOException
Create a client socket that is connected to the given address and port.- Parameters:
server
- to connect to (including an optional port)defaultPort
- the default port (if not specified in the server address)ssl
- if SSL should be usednetworkTimeout
- socket so timeout- Returns:
- the socket
- Throws:
java.io.IOException
- on failure
-
createSocket
public static java.net.Socket createSocket(java.net.InetAddress address, int port, boolean ssl) throws java.io.IOException
Create a client socket that is connected to the given address and port.- Parameters:
address
- the address to connect toport
- the portssl
- if SSL should be used- Returns:
- the socket
- Throws:
java.io.IOException
- on failure
-
createSocket
public static java.net.Socket createSocket(java.net.InetAddress address, int port, boolean ssl, int networkTimeout) throws java.io.IOException
Create a client socket that is connected to the given address and port.- Parameters:
address
- the address to connect toport
- the portssl
- if SSL should be usednetworkTimeout
- socket so timeout- Returns:
- the socket
- Throws:
java.io.IOException
- on failure
-
createServerSocket
public static java.net.ServerSocket createServerSocket(int port, boolean ssl)
Create a server socket. The system property h2.bindAddress is used if set. If SSL is used and h2.enableAnonymousTLS is true, an attempt is made to modify the security property jdk.tls.legacyAlgorithms (in newer JVMs) to allow anonymous TLS.This system change is effectively permanent for the lifetime of the JVM.
- Parameters:
port
- the port to listen onssl
- if SSL should be used- Returns:
- the server socket
- See Also:
CipherFactory.removeAnonFromLegacyAlgorithms()
-
getBindAddress
private static java.net.InetAddress getBindAddress() throws java.net.UnknownHostException
Get the bind address if the system property h2.bindAddress is set, or null if not.- Returns:
- the bind address
- Throws:
java.net.UnknownHostException
-
createServerSocketTry
private static java.net.ServerSocket createServerSocketTry(int port, boolean ssl)
-
isLocalAddress
public static boolean isLocalAddress(java.net.Socket socket) throws java.net.UnknownHostException
Check if a socket is connected to a local address.- Parameters:
socket
- the socket- Returns:
- true if it is
- Throws:
java.net.UnknownHostException
- on failure
-
closeSilently
public static java.net.ServerSocket closeSilently(java.net.ServerSocket socket)
Close a server socket and ignore any exceptions.- Parameters:
socket
- the socket- Returns:
- null
-
getLocalAddress
public static java.lang.String getLocalAddress()
Get the local host address as a string. For performance, the result is cached for one second.- Returns:
- the local host address
-
getHostName
public static java.lang.String getHostName(java.lang.String localAddress)
Get the host name of a local address, if available.- Parameters:
localAddress
- the local address- Returns:
- the host name, or another text if not available
-
ipToShortForm
public static java.lang.StringBuilder ipToShortForm(java.lang.StringBuilder builder, byte[] address, boolean addBrackets)
Appends short representation of the specified IP address to the string builder.- Parameters:
builder
- string builder to append to, ornull
address
- IP addressaddBrackets
- if (true
, add brackets around IPv6 addresses- Returns:
- the specified or the new string builder with short representation of specified address
-
-