All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.embulk.deps.JarEmbeddedUrlStreamHandler Maven / Gradle / Ivy

package org.embulk.deps;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.nio.ByteBuffer;

/**
 * A special type of {@link java.net.URLStreamHandler} to be coupled with URLs created for a resource.
 */
final class JarEmbeddedUrlStreamHandler extends URLStreamHandler {
    JarEmbeddedUrlStreamHandler(final ByteBuffer buffer, final int begin, final int end) {
        this.buffer = buffer;
        this.begin = begin;
        this.end = end;
    }

    @Override
    protected URLConnection openConnection(final URL url) throws IOException {
        // Note that declaring variables here may cause unexpected behaviors.
        //
        // @see S3 Java client fails a lot with "Premature end of Content-Length delimited message body" or "java.net.SocketException Socket closed" - Stack Overflow
        // @see Android s3 download throws "Socket is closed" exception or terminates early
        return new URLConnection(url) {
            @Override
            public void connect() {
                // Do nothing.
            }

            /**
             * Breaks down the special URL built by JarEmbeddedUrlFactory, and creates InputStream to read from.
             */
            @Override
            public InputStream getInputStream() throws IOException {
                return new ByteBufferInputStream(buffer, begin, end);
            }
        };
    }

    private final ByteBuffer buffer;
    private final int begin;
    private final int end;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy