/*
 * #%L
 * SCIFIO library for reading and converting scientific file formats.
 * %%
 * Copyright (C) 2011 - 2015 Board of Regents of the University of
 * Wisconsin-Madison
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package io.scif.io;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import org.scijava.Context;
import org.scijava.plugin.Plugin;

/**
 * Provides random access to URLs using the IRandomAccess interface. Instances
 * of URLHandle are read-only.
 *
 * @see IRandomAccess
 * @see StreamHandle
 * @see java.net.URLConnection
 * @author Melissa Linkert
 */
@Plugin(type = IStreamAccess.class)
public class URLHandle extends StreamHandle {

	// -- Fields --

	/** URL of open socket */
	private String url;

	/** Socket underlying this stream */
	private URLConnection conn;

	// -- Constructors --

	/**
	 * Zero-parameter constructor. This instructor can be used first to see if a
	 * given file is constructable from this handle. If so, setFile can then be
	 * used.
	 */
	public URLHandle() {
		super();
	}

	public URLHandle(final Context context) {
		super(context);
	}

	/**
	 * Constructs a new URLHandle using the given URL.
	 */
	public URLHandle(final Context context, final String url) throws IOException {
		super(context);
		setURL(url);
	}

	// -- URLHandle API --

	/**
	 * Initializes this URLHandle with the provided url.
	 *
	 * @throws IOException
	 */
	public void setURL(String url) throws IOException {
		if (!isConstructable(url)) {
			throw new HandleException(url + " is not a valid url.");
		}

		if (!url.startsWith("http") && !url.startsWith("file:")) {
			url = "http://" + url;
		}
		this.url = url;
		resetStream();
	}

	// -- IRandomAccess API methods --

	@Override
	public void seek(final long pos) throws IOException {
		if (pos < getFp() && pos >= getMark()) {
			getStream().reset();
			setFp(getMark());
			skip(pos - getFp());
		}
		else super.seek(pos);
	}

	// -- IStreamAccess API methods --

	@Override
	public boolean isConstructable(final String id) throws IOException {
		return id.startsWith("http:") || id.startsWith("file:");
	}

	// -- StreamHandle API methods --

	@Override
	public void setFile(final String file) throws IOException {
		super.setFile(file);
		setURL(file);
	}

	@Override
	public void resetStream() throws IOException {
		conn = (new URL(url)).openConnection();
		setStream(new DataInputStream(new BufferedInputStream(
			conn.getInputStream(), RandomAccessInputStream.MAX_OVERHEAD)));
		setFp(0);
		setMark(0);
		setLength(conn.getContentLength());
		if (getStream() != null) getStream().mark(
			RandomAccessInputStream.MAX_OVERHEAD);
	}

	// -- Helper methods --

	/** Skip over the given number of bytes. */
	private void skip(long bytes) throws IOException {
		while (bytes >= Integer.MAX_VALUE) {
			bytes -= skipBytes(Integer.MAX_VALUE);
		}
		int skipped = skipBytes((int) bytes);
		while (skipped < bytes) {
			final int n = skipBytes((int) (bytes - skipped));
			if (n == 0) break;
			skipped += n;
		}
	}
}

@@ -2026,16 +2026,123 @@
 ndle %7B%0A%0A
+%09// -- Constants --%0A%0A%09private static final String%5B%5D SUPPORTED_PROTOCOLS = %7B %22http:%22, %22https:%22, %22file:%22 %7D;%0A%0A
 %09// -- F
@@ -3014,23 +3014,111 @@
 );%0A%09%09%7D%0A%0A
+%09%09boolean hasSupportedProtocol = false;%0A%09%09for (String protocol : SUPPORTED_PROTOCOLS) %7B%0A%09
 %09%09if (
-!
 url.star
@@ -3128,43 +3128,102 @@
 ith(
-%22http%22) && !url.startsWith(%22file:%22)
+protocol)) %7B%0A%09%09%09%09hasSupportedProtocol = true;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09if (!hasSupportedProtocol
 ) %7B%0A
@@ -3252,16 +3252,17 @@
 rl;%0A%09%09%7D%0A
+%0A
 %09%09this.u
@@ -3663,63 +3663,129 @@
 %7B%0A%09%09
-return id.startsWith(%22http:%22) %7C%7C id.startsWith(%22file:%22)
+for (String protocol : SUPPORTED_PROTOCOLS) %7B%0A%09%09%09if (id.startsWith(protocol)) %7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return false
 ;%0A%09%7D

add support for HTTPS protocol in URLHandle.