001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io.protocols.data;
003
004import java.io.ByteArrayInputStream;
005import java.io.IOException;
006import java.io.InputStream;
007import java.net.URL;
008import java.net.URLConnection;
009import java.util.Base64;
010
011/**
012 * Connection for "data:" protocol allowing to read inlined base64 images.
013 * <p>
014 * See <a href="http://stackoverflow.com/a/9388757/2257172">StackOverflow</a>.
015 * @since 10931
016 */
017public class DataConnection extends URLConnection {
018
019    /**
020     * Constructs a new {@code DataConnection}.
021     * @param u data url
022     */
023    public DataConnection(URL u) {
024        super(u);
025    }
026
027    @Override
028    public void connect() throws IOException {
029        connected = true;
030    }
031
032    @Override
033    public InputStream getInputStream() throws IOException {
034        return new ByteArrayInputStream(Base64.getDecoder().decode(url.toString().replaceFirst("^.*;base64,", "")));
035    }
036}