Skip to content

jycr/java-data-url-handler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License MIT Publish package to the Maven Central Repository Version on Maven Central Quality Gate Status

java-data-url-handler

When adding this library in your classpath, you will be able to handle "data URLs" (RFC-2397) in your application.

Prerequisites

You need Java ≥ 11 to use this library.

Usage

When you want to get the content of a "data URL":

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;

public class Test {
	public static void main(String[] args) throws IOException {
        URL url = URI.create("data:text/plain,Hello%2C%20World!").toURL();
        try(InputStream in = url.openStream()) {
            System.out.println(new String(in.readAllBytes(), StandardCharsets.UTF_8));
        }
    }
}

Without this library, you will get an error like this:

Exception in thread "main" java.net.MalformedURLException: unknown protocol: data
	at java.base/java.net.URL.<init>(URL.java:779)
	at java.base/java.net.URL.<init>(URL.java:654)
	at java.base/java.net.URL.<init>(URL.java:590)

With this library in your classpath, you can handle the data URL and get the content of the data URL.

The output of the code above will be:

Hello, World!

Add dependency to your project

To add this library to your Maven project:

<dependency>
    <groupId>io.github.jycr</groupId>
    <artifactId>java-data-url-handler</artifactId>
    <version>${java-data-url-handler.version}</version>
</dependency>

Latest available version: Version on Maven Central