-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
167 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,197 @@ | ||
# Laivy's java-address | ||
# Java Address 📬 | ||
|
||
This library provides robust classes for representing and manipulating network addresses and ports, including IPv4 and IPv6 addresses. It ensures valid formatting, supports various address operations, and offers utilities for common networking tasks. | ||
![Java](https://img.shields.io/badge/Java-ED8B00?style=for-the-badge&logo=java&logoColor=white) | ||
![License](https://img.shields.io/github/license/ItsLaivy/java-address?style=for-the-badge) | ||
|
||
## Table of Contents | ||
## Overview 🌐 | ||
|
||
- [Port](#port) | ||
- [Address](#address) | ||
- [IPv4Address](#ipv4address) | ||
- [IPv6Address](#ipv6address) | ||
`java-address` is a lightweight library for handling internet addresses and communication protocols, including IPv4, IPv6 and Domains. It provides robust utilities for validation, parsing, and manipulation of network addresses and ports. | ||
|
||
## Port | ||
## Features ✨ | ||
|
||
The `Port` class represents a network port and ensures it adheres to the valid port number range (0 to 65535). It is an immutable and thread-safe class that extends `Number` and implements `Comparable`. | ||
- **Validation and Parsing**: Validate and create instances of IPv4 and IPv6 addresses from strings. | ||
- **Port Handling**: Represent and manipulate network ports, ensuring they are within the valid range (0 to 65535). | ||
- **Address Utilities**: Retrieve raw bytes and formatted names of addresses. | ||
- **Domain Names**: A fully complete Domain classes including **SLD**, **TLD** and **Subdomain** classes with all verifications. | ||
|
||
### Key Features | ||
## Installation 📦 | ||
|
||
- **Validation**: Check if a string is a valid port number. | ||
- **Parsing**: Create `Port` instances from strings or integers. | ||
- **Port Type Classification**: Determine if a port is well-known, registered, or dynamic/private. | ||
- **Utilities**: Methods for incrementing, decrementing, and checking port characteristics. | ||
For now, there's no public artifact at the Maven Central for this. | ||
To use the **Java Address** library. | ||
You should install it manually at your project | ||
using [Maven Guide to installing 3rd party JARs](https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html) | ||
|
||
### Example Methods | ||
## Usage 🛠️ | ||
|
||
- `validate(String string)`: Validates if a string is a valid port number. | ||
- `parse(String port)`: Parses a string to create a `Port` object. | ||
- `create(int port)`: Creates a `Port` object from an integer. | ||
- `isWellKnown()`: Checks if the port is a well-known port (0-1023). | ||
- `isRegistered()`: Checks if the port is a registered port (1024-49151). | ||
- `isDynamicPrivate()`: Checks if the port is a dynamic or private port (49152-65535). | ||
- `getPortType()`: Returns the type of the port. | ||
### Basic Example | ||
|
||
## Address | ||
```java | ||
// Parses the 8080 into a port object | ||
Port port = Port.create(8080); | ||
// Parses the ipv4 into an object | ||
IPv4Address address = IPv4Address.parse("192.168.1.1"); | ||
// The Host object includes information about the address and port | ||
Host host = Host.create(address, port); | ||
|
||
The `Address` interface represents a network address, providing methods for handling both IPv4 and IPv6 addresses. It includes static methods to validate and parse addresses, and abstract methods to retrieve address bytes and names. | ||
// Print objects | ||
System.out.println("Port: " + port); | ||
System.out.println("Address: " + address); | ||
System.out.println("Host: " + host); | ||
``` | ||
|
||
### Key Features | ||
### Advanced Usage | ||
|
||
- **Validation**: Validate addresses in IPv4 or IPv6 format. | ||
- **Parsing**: Create address instances from strings. | ||
- **Address Utilities**: Retrieve raw bytes and formatted names of the address. | ||
- **Port Handling**: Methods to convert addresses to strings with ports appended. | ||
#### Working with Ports | ||
|
||
### Example Methods | ||
```java | ||
Port wellKnownPort = Port.create(80); // HTTP port | ||
Port registeredPort = Port.create(8080); // Custom application port | ||
|
||
- `validate(String string)`: Validates if a string is a valid IPv4 or IPv6 address. | ||
- `parse(String string)`: Parses a string to create an `Address` object. | ||
- `getBytes()`: Returns the raw byte values of the address. | ||
- `getName()`: Returns the formatted string representation of the address. | ||
- `toString(Port port)`: Converts the address into a string representation with a port appended. | ||
System.out.println("Is well-known port: " + wellKnownPort.isWellKnown()); | ||
System.out.println("Registered Port: " + registeredPort.isRegistered()); | ||
``` | ||
|
||
## IPv4Address | ||
## IPv4 Address 🌐 | ||
|
||
The `IPv4Address` class represents an IPv4 network address. IPv4 addresses are 32-bit numerical labels separated by dots, e.g., `192.168.1.1`. | ||
The `IPv4Address` class represents an IPv4 network address. IPv4 addresses are 32-bit numerical labels written in decimal and separated by dots, e.g., `192.168.0.1`. | ||
|
||
### Key Features | ||
### Usage Examples | ||
|
||
- **Validation**: Ensures the address is a valid IPv4 address. | ||
- **Conversion**: Converts the address to and from its byte representation. | ||
- **Address Utilities**: Methods to check if the address is localhost, or within specific subnets or broadcast addresses. | ||
#### Validating an IPv4 Address | ||
|
||
### Example Methods | ||
```java | ||
boolean isValid = IPv4Address.validate("192.168.0.1"); | ||
System.out.println("Is valid: " + isValid); // Returns 'true' | ||
|
||
- `validate(String string)`: Validates if a string is a valid IPv4 address. | ||
- `parse(String string)`: Parses a string to create an `IPv4Address` object. | ||
- `getOctets()`: Returns the individual octets of the IPv4 address. | ||
- `isLocalHost()`: Checks if the address is a localhost address. | ||
- `isInSubnet(IPv4Address subnetMask)`: Checks if the address is within the range defined by a subnet mask. | ||
- `isBroadcast(IPv4Address subnetMask)`: Checks if the address is a broadcast address for the given subnet mask. | ||
isValid = IPv4Address.validate("255.255.255.255"); | ||
System.out.println("Is valid: " + isValid); // Returns 'true' | ||
|
||
## IPv6Address | ||
isValid = IPv4Address.validate("999.999.999.999"); | ||
System.out.println("Is valid: " + isValid); // Returns 'false' | ||
``` | ||
|
||
The `IPv6Address` class represents an IPv6 network address. IPv6 addresses are 128-bit numerical labels separated by colons, e.g., `2001:0db8:85a3:0000:0000:8a2e:0370:7334`. | ||
#### Parsing an IPv4 Address | ||
|
||
### Key Features | ||
```java | ||
IPv4Address ipv4Address = IPv4Address.parse("192.168.0.1"); | ||
System.out.println("Parsed address: " + ipv4Address.getName()); | ||
``` | ||
|
||
- **Validation**: Ensures the address is a valid IPv6 address. | ||
- **Compression Handling**: Handles address compression and decompression. | ||
- **Address Utilities**: Methods to check if the address is a loopback address, or within specific subnets or broadcast addresses. | ||
#### Getting Byte Representation | ||
|
||
### Example Methods | ||
```java | ||
IPv4Address ipv4Address = IPv4Address.parse("192.168.0.1"); | ||
System.out.println("Byte representation: " + Arrays.toString(ipv4Address.getBytes())); | ||
``` | ||
|
||
- `validate(String string)`: Validates if a string is a valid IPv6 address. | ||
- `parse(String string)`: Parses a string to create an `IPv6Address` object. | ||
- `getGroups()`: Returns the individual groups of the IPv6 address. | ||
- `getName()`: Returns the address in its compressed format. | ||
- `getRawName()`: Returns the raw, uncompressed string representation of the address. | ||
- `isLocalHost()`: Checks if the address is a loopback address. | ||
- `isInSubnet(IPv6Address subnetMask)`: Checks if the address is within the range defined by a subnet mask. | ||
- `isBroadcast(IPv6Address subnetMask)`: Checks if the address is a broadcast address for the given subnet mask. | ||
#### Checking if Address is local | ||
|
||
```java | ||
IPv4Address ipv4Address = IPv4Address.parse("127.0.0.1"); | ||
System.out.println("Is local: " + ipv4Address.isLocal()); | ||
``` | ||
|
||
## IPv6 Addresses 🌐 | ||
|
||
The `IPv6Address` class represents an IPv6 network address. IPv6 addresses are 128-bit numerical labels written in hexadecimal and separated by colons, e.g., `2001:0db8:85a3:0000:0000:8a2e:0370:7334`. | ||
|
||
### Usage Examples | ||
|
||
#### Validating an IPv6 Address | ||
|
||
```java | ||
boolean isValid = IPv6Address.validate("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); | ||
System.out.println("Is valid: " + isValid); // Prints 'true' | ||
|
||
isValid = IPv6Address.validate("::ffff:192.0.2.128"); | ||
System.out.println("Is valid: " + isValid); // Prints 'true' | ||
|
||
isValid = IPv6Address.validate("2001:0db8:85a3::85a3::7334"); | ||
System.out.println("Is valid: " + isValid); // Prints 'false' | ||
``` | ||
|
||
#### Parsing an IPv6 Address | ||
|
||
```java | ||
IPv6Address ipv6Address = IPv6Address.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); | ||
System.out.println("Parsed address: " + ipv6Address.getName()); | ||
``` | ||
|
||
#### Getting Byte Representation | ||
|
||
```java | ||
IPv6Address ipv6Address = IPv6Address.parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); | ||
System.out.println("Byte representation: " + Arrays.toString(ipv6Address.getBytes())); | ||
``` | ||
|
||
#### Checking if Address is local | ||
|
||
```java | ||
IPv6Address ipv6Address = IPv6Address.parse("::1"); | ||
System.out.println("Is local: " + ipv6Address.isLocal()); | ||
``` | ||
|
||
## Domain Names 🌐 | ||
|
||
### Domain | ||
The `Domain` class represents a fully qualified domain name (FQDN). It provides methods to parse and validate domain names, ensuring they conform to standard formats. | ||
|
||
#### Example | ||
```java | ||
Domain domain = Domain.parse("example.com"); | ||
System.out.println("Domain: " + domain); | ||
``` | ||
|
||
### TLD (Top-Level Domain) | ||
The `TLD` class represents the top-level domain of a domain name, such as `.com`, `.org`, or `.net`. It includes methods for validation and retrieval of the TLD from a given domain. | ||
|
||
#### Example | ||
```java | ||
TLD tld = TLD.parse("com"); | ||
System.out.println("Top-Level Domain: " + tld); | ||
``` | ||
|
||
### SLD (Second-Level Domain) | ||
The `SLD` class represents the second-level domain, which is the part of the domain name directly to the left of the TLD. For example, in `example.com`, `example` is the SLD. This class provides methods to parse and validate the SLD. | ||
|
||
#### Example | ||
```java | ||
SLD sld = SLD.parse("example"); | ||
System.out.println("Second-Level Domain: " + sld); | ||
``` | ||
|
||
### Subdomain | ||
The `Subdomain` class represents the subdomain part of a domain name, which is the part to the left of the SLD. For example, in `blog.example.com`, `blog` is the subdomain. This class provides methods to parse and validate subdomains. | ||
|
||
#### Example | ||
```java | ||
Subdomain subdomain = Subdomain.parse("blog"); | ||
System.out.println("Subdomain: " + subdomain); | ||
``` | ||
|
||
These classes work together to provide a comprehensive toolkit for handling and manipulating domain names in your applications. | ||
|
||
## Contributing 🤝 | ||
|
||
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes. | ||
|
||
1. Fork the repository | ||
2. Create your feature branch (`git checkout -b feature/AmazingFeature`) | ||
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) | ||
4. Push to the branch (`git push origin feature/AmazingFeature`) | ||
5. Open a pull request | ||
|
||
## License 📄 | ||
|
||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
|
||
## Acknowledgements 🙏 | ||
|
||
- Thanks to all the contributors who have helped improve this project. | ||
- Special thanks to the open-source community for their continuous support and contributions. | ||
|
||
--- | ||
|
||
For more details and usage examples, please refer to the API documentation or the source code of the library. Feel free to contribute and enhance the library by opening issues or submitting pull requests. | ||
Feel free to reach out if you have any questions or need further assistance! | ||
|
||
Happy coding! 🚀 |