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 */ 045 public RangeRequestInputStream(final InputStream in, 046 final long skip, 047 final long length) throws IOException { 048 super(new BoundedInputStream(new SkipInputStream(in, skip), length)); 049 } 050 051 052 /** 053 * An InputStream wrapper that skips bytes 054 * @param in 055 * @param skip 056 * @throws IOException 057 */ 058 private static class SkipInputStream extends ProxyInputStream { 059 060 /** 061 * An InputStream wrapper that always skips the first N bytes 062 * @param in 063 * @param skip 064 * @throws IOException 065 */ 066 public SkipInputStream(final InputStream in, 067 final long skip) throws IOException { 068 super(in); 069 IOUtils.skip(in, skip); 070 } 071 } 072}