// Path: wayback-core/src/main/java/org/archive/wayback/exception/RangeNotSatisfiableException.java
// public class RangeNotSatisfiableException extends SpecificCaptureReplayException {
// 	private static final long serialVersionUID = 1L;
// 
// 	private Resource origResource;
// 	private long[][] requestedRanges;
// 
// 	public RangeNotSatisfiableException(Resource origResource, long[][] requestedRanges, String message) {
// 		super(message, "RangeNotSatisfiable");
// 		this.origResource = origResource;
// 		this.requestedRanges = requestedRanges;
// 	}
// 
// 	@Override
// 	public int getStatus() {
// 		return HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE;
// 	}
// 
// 	public Resource getOrigResource() {
// 		return origResource;
// 	}
// 
// 	public long[][] getRequestedRanges() {
// 		return requestedRanges;
// 	}
// }


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.archive.wayback.core.Resource;
import org.archive.wayback.exception.RangeNotSatisfiableException;
import com.google.common.io.ByteStreams;
/*
 *  This file is part of the Wayback archival access software
 *   (http://archive-access.sourceforge.net/projects/wayback/).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual
 *  contributors.
 *
 *  The IA licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.archive.wayback.replay;





/**
 * RangeResource decorates Resource to render partial content response
 * to range request.
 *
 * After constructing with wrapped Resource and requested range,
 * call {@link #parseRange()} to prepare internal state for rendering.
 * The method will throw {@link RangeNotSatisfiableException} if the requested
 * byte ranges cannot be replayed with the base Resource.
 *
 * RangeResource currently only works with:
 * <ul>
 * <li>A single-range request
 * <li>A 200 capture with {@code Content-Length}, or a valid 206 capture
 * with single range (i.e. no support for {@code multipart/byteranges})
 * </ul>
 * @see HttpHeaderOperation#parseRanges(String)
 */
public class RangeResource extends Resource {
	private Resource origResource;
	private long[][] requestedRanges;
	private long outputLength;
	private Map<String, String> httpHeaders;

	/**
	 * Initialize with base Resource and requested ranges.
	 * @param origResource base resource
	 * @param requestedRanges requested range
	 * @see HttpHeaderOperation#parseRanges(String)
	 */
	public RangeResource(Resource origResource, long[][] requestedRanges) {
		this.origResource = origResource;
		this.requestedRanges = requestedRanges;
	}

	/**
	 * Prepare response for requested range.
	 * @throws RangeNotSatisfiableException base Resource either does not have
	 * enough data to fulfill the request, or have invalid header field.
	 */
// Path: wayback-core/src/main/java/org/archive/wayback/exception/RangeNotSatisfiableException.java
// public class RangeNotSatisfiableException extends SpecificCaptureReplayException {
// 	private static final long serialVersionUID = 1L;
// 
// 	private Resource origResource;
// 	private long[][] requestedRanges;
// 
// 	public RangeNotSatisfiableException(Resource origResource, long[][] requestedRanges, String message) {
// 		super(message, "RangeNotSatisfiable");
// 		this.origResource = origResource;
// 		this.requestedRanges = requestedRanges;
// 	}
// 
// 	@Override
// 	public int getStatus() {
// 		return HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE;
// 	}
// 
// 	public Resource getOrigResource() {
// 		return origResource;
// 	}
// 
// 	public long[][] getRequestedRanges() {
// 		return requestedRanges;
// 	}
// }


// Path: wayback-core/src/main/java/org/archive/wayback/replay/RangeResource.java
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.archive.wayback.core.Resource;
import org.archive.wayback.exception.RangeNotSatisfiableException;
import com.google.common.io.ByteStreams;

/*
 *  This file is part of the Wayback archival access software
 *   (http://archive-access.sourceforge.net/projects/wayback/).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual
 *  contributors.
 *
 *  The IA licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.archive.wayback.replay;





/**
 * RangeResource decorates Resource to render partial content response
 * to range request.
 *
 * After constructing with wrapped Resource and requested range,
 * call {@link #parseRange()} to prepare internal state for rendering.
 * The method will throw {@link RangeNotSatisfiableException} if the requested
 * byte ranges cannot be replayed with the base Resource.
 *
 * RangeResource currently only works with:
 * <ul>
 * <li>A single-range request
 * <li>A 200 capture with {@code Content-Length}, or a valid 206 capture
 * with single range (i.e. no support for {@code multipart/byteranges})
 * </ul>
 * @see HttpHeaderOperation#parseRanges(String)
 */
public class RangeResource extends Resource {
	private Resource origResource;
	private long[][] requestedRanges;
	private long outputLength;
	private Map<String, String> httpHeaders;

	/**
	 * Initialize with base Resource and requested ranges.
	 * @param origResource base resource
	 * @param requestedRanges requested range
	 * @see HttpHeaderOperation#parseRanges(String)
	 */
	public RangeResource(Resource origResource, long[][] requestedRanges) {
		this.origResource = origResource;
		this.requestedRanges = requestedRanges;
	}

	/**
	 * Prepare response for requested range.
	 * @throws RangeNotSatisfiableException base Resource either does not have
	 * enough data to fulfill the request, or have invalid header field.
	 */
	public void parseRange() throws RangeNotSatisfiableException, IOException {