Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/opwvhk/data-url

Java support for RFC2397 data: URLs by java.net.URL
https://github.com/opwvhk/data-url

Last synced: 9 days ago
JSON representation

Java support for RFC2397 data: URLs by java.net.URL

Awesome Lists containing this project

README

        

![Build Status](https://github.com/opwvhk/data-url/workflows/Maven%20Build/badge.svg)
[![license](doc/license-APACHE-2.0-brightgreen.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/net.sf.opk/data-url/badge.svg)](https://maven-badges.herokuapp.com/maven-central/net.sf.opk/data-url)

# data-url

Support for `data:` URLs for JVM languages.

## Rationale

By default, the Java class `java.net.URL` does not support `data:` URLs (specified in [RFC 2397](https://www.ietf.org/rfc/rfc2397.txt)).

As a result, code snippets like this throw a `MalformedURLException` (unknown protocol) on the first line:

``` java
java.net.URL url = new java.net.URL("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
java.awt.image.BufferedImage image = javax.imageio.ImageIO.read(URL);
```

## Usage

This library hooks into the Java ServiceLoader architecture for URL stream handlers and provides support for `data:` URLs.
To use it, simply add it to the classpath (it has no additional, transitive dependencies).

For e.g., Maven, this means adding this dependency:

```xml

net.fs.opk
data-url
1.0

```

With this dependency on the classpath, the code snippet above succeeds, and reads an image: ![red dot](doc/red-dot-5px.png "red dot")

(note that an actual image file is used in this readme because GitHub does not allow `data:` URLs)