Package org.evolvis.tartools.rfc822
Class IPAddress
- java.lang.Object
-
- org.evolvis.tartools.rfc822.IPAddress
-
public class IPAddress extends Object
Represents an IP address (including Legacy IP) for use in eMail on public Internet (no scoped addresses / IPv6 Zone ID) The main entry points are thev6(String)andv4(String)methods.- Author:
- mirabilos (t.glaser@tarent.de)
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedIPAddress(String input)Private constructor, use the factory methodof(String)instead
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intaccept()Advances the current position to the next characterInetAddressasIPv4Address()Parses the passed address as Legacy IP address (IPv4).InetAddressasIPv6Address()Parses the passed address as IP address (IPv6).protected intbra(int deltapos)Jumps to a specified input character position, relative jumpprotected intcur()Returns the wide character at the current positionprotected intjmp(int pos)Jumps to a specified input character position, absolute jumpprotected static <T extends org.evolvis.tartools.rfc822.Parser>
Tof(Class<T> cls, String input)Constructs a parser.static IPAddressof(String address)Creates and initialises a new parser for IPv6 and IPv4 addresses (excluding IPv6 Zone ID) to use byasIPv6Address()andasIPv4Address().protected intpeek()Returns the wide character after the one at the current positionprotected byte[]pIPv4Address()protected byte[]pIPv6Address()protected intpos()Returns the current input character position, for saving and restoring (withjmp(int)) and for error messagesprotected Strings()Returns the input string, for use with substring comparisons (this is safe because Java™ strings are immutable)protected intskip(BiFunction<Integer,Integer,Boolean> matcher)Advances the current position as long as the matcher returns true and end of input is not yet reachedprotected intskip(Function<Integer,Boolean> matcher)Advances the current position as long as the matcher returns true and end of input is not yet reached; cf.static InetAddressv4(String address)Parses the passed address as Legacy IP address (IPv4).static InetAddressv6(String address)Parses the passed address as IP address (IPv6), excluding Zone ID.
-
-
-
Constructor Detail
-
IPAddress
protected IPAddress(String input)
Private constructor, use the factory methodof(String)instead- Parameters:
input- string to analyse
-
-
Method Detail
-
of
public static IPAddress of(String address)
Creates and initialises a new parser for IPv6 and IPv4 addresses (excluding IPv6 Zone ID) to use byasIPv6Address()andasIPv4Address().- Parameters:
address- to parse (protocol depends on parser method called)- Returns:
- null if address was null or much too large, the new instance otherwise
-
asIPv6Address
public InetAddress asIPv6Address()
Parses the passed address as IP address (IPv6). Note that the returned InetAddress object can be anInet4Addressobject, for example if the passed address represents a v4-mapped address; in most cases it will be anInet6Addressobject though. In either case, if the address is no valid IPv6 address (e.g. because it is an IPv4 address), null will be returned instead, so the return value can be used to distinguish the address families, even if a v4-mapped address occurs. TheInetAddress.getHostName()method will return the original string in all cases anyway.- Returns:
InetAddressrepresenting the address, or null on failure
-
asIPv4Address
public InetAddress asIPv4Address()
Parses the passed address as Legacy IP address (IPv4). TheInetAddress.getHostName()method will return the original string.- Returns:
InetAddressrepresenting the address, or null on failure
-
v6
public static InetAddress v6(String address)
Parses the passed address as IP address (IPv6), excluding Zone ID. Note that the returned InetAddress object can be anInet4Addressobject, for example if the passed address represents a v4-mapped address; in most cases it will be anInet6Addressobject though. In either case, if the address is no valid IPv6 address (e.g. because it is an IPv4 address), null will be returned instead, so the return value can be used to distinguish the address families, even if a v4-mapped address occurs. TheInetAddress.getHostName()method will return the original string in all cases anyway.- Parameters:
address- to parse as IPv6 address (IPv4 addresses return null)- Returns:
InetAddressrepresenting the address, or null on failure
-
v4
public static InetAddress v4(String address)
Parses the passed address as Legacy IP address (IPv4). TheInetAddress.getHostName()method will return the original string.- Parameters:
address- to parse as IPv4 address- Returns:
InetAddressrepresenting the address, or null on failure
-
pIPv4Address
protected byte[] pIPv4Address()
-
pIPv6Address
protected byte[] pIPv6Address()
-
of
protected static <T extends org.evolvis.tartools.rfc822.Parser> T of(Class<T> cls, String input)
Constructs a parser. Intended to be used by subclasses from static factory methods *only*; seePath.of(String)for an example.- Type Parameters:
T- subclass of Parser to construct- Parameters:
cls- subclass of Parser to constructinput- user-providedStringto parse- Returns:
- null if input was null or too large, the new instance otherwise
-
jmp
protected final int jmp(int pos)
Jumps to a specified input character position, absolute jump- Parameters:
pos- to jump to- Returns:
- the codepoint at that position
- Throws:
IndexOutOfBoundsException- if pos is not in or just past the input
-
bra
protected final int bra(int deltapos)
Jumps to a specified input character position, relative jump- Parameters:
deltapos- to add to the current positioin- Returns:
- the codepoint at that position
- Throws:
IndexOutOfBoundsException- if pos is not in or just past the input
-
pos
protected final int pos()
Returns the current input character position, for saving and restoring (withjmp(int)) and for error messages- Returns:
- position
-
s
protected final String s()
Returns the input string, for use with substring comparisons (this is safe because Java™ strings are immutable)- Returns:
- String input
-
cur
protected final int cur()
Returns the wide character at the current position- Returns:
- UCS-4 codepoint, or -1 if end of input is reached
-
peek
protected final int peek()
Returns the wide character after the one at the current position- Returns:
- UCS-4 codepoint, or -1 if end of input is reached
-
accept
protected final int accept()
Advances the current position to the next character- Returns:
- codepoint of the next character, or -1 if end of input is reached
- Throws:
IndexOutOfBoundsException- if end of input was already reached
-
skip
protected final int skip(BiFunction<Integer,Integer,Boolean> matcher)
Advances the current position as long as the matcher returns true and end of input is not yet reached
-
skip
protected final int skip(Function<Integer,Boolean> matcher)
Advances the current position as long as the matcher returns true and end of input is not yet reached; cf.skip(BiFunction)- Parameters:
matcher- gets called with justcur()as argument- Returns:
- codepoint of the first character where the matcher returned false, or -1
-
-