001/** 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.http.commons.responses; 017 018import java.io.FilterInputStream; 019import java.io.IOException; 020import java.io.InputStream; 021 022import org.apache.commons.io.IOUtils; 023import org.apache.commons.io.input.BoundedInputStream; 024import org.apache.commons.io.input.ProxyInputStream; 025 026/** 027 * An InputStream wrapper that skips N bytes and only returns 028 * the data up to a certain length 029 * 030 * @author awoods 031 */ 032public class RangeRequestInputStream extends FilterInputStream { 033 034 /** 035 * Creates a <code>FilterInputStream</code> 036 * by assigning the argument <code>in</code> 037 * to the field <code>this.in</code> so as 038 * to remember it for later use. 039 * 040 * @param in the underlying input stream, or <code>null</code> if 041 * this instance is to be created without an underlying stream. 042 * @param skip the number of bytes to skip at the beginning of the stream 043 * @param length the number of bytes from the inputstream to read 044 * @throws IOException if IO exception occurred 045 */ 046 public RangeRequestInputStream(final InputStream in, 047 final long skip, 048 final long length) throws IOException { 049 super(new BoundedInputStream(new SkipInputStream(in, skip), length)); 050 } 051 052 053 /** 054 * An InputStream wrapper that skips bytes 055 * @param in 056 * @param skip 057 * @throws IOException 058 */ 059 private static class SkipInputStream extends ProxyInputStream { 060 061 /** 062 * An InputStream wrapper that always skips the first N bytes 063 * @param in 064 * @param skip 065 * @throws IOException 066 */ 067 public SkipInputStream(final InputStream in, 068 final long skip) throws IOException { 069 super(in); 070 IOUtils.skip(in, skip); 071 } 072 } 073}