001/** 002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC 003 * and GZip files. (http://jwat.org/) 004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/) 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.jwat.common; 019 020import java.io.FilterInputStream; 021import java.io.IOException; 022import java.io.InputStream; 023 024/** 025 * The standard <code>FileInputStream</code> has a known bug concerning it's 026 * skip method which does not correctly report the number of bytes skipped. 027 * This class provides a simple <code>InputStream</code> wrapper which ensures 028 * all skips are in turn done as reads. 029 * 030 * @author nicl 031 */ 032public class InputStreamNoSkip extends FilterInputStream { 033 034 /** Buffer size to use when read skipping. */ 035 public static final int SKIP_READ_BUFFER_SIZE = 8192; 036 037 /** Read buffer used by the skip method. */ 038 protected byte[] skip_read_buffer = new byte[SKIP_READ_BUFFER_SIZE]; 039 040 /** 041 * Construct a <code>InputStream</code> with the skip method 042 * overridden. 043 * @param in input stream to no skip on 044 */ 045 public InputStreamNoSkip(InputStream in) { 046 super(in); 047 } 048 049 @Override 050 public long skip(long n) throws IOException { 051 long remaining = n; 052 long skipped = 0; 053 long readLast = 0; 054 while (remaining > 0 && readLast != -1) { 055 remaining -= readLast; 056 skipped += readLast; 057 readLast = read(skip_read_buffer, 0, (int) Math.min(remaining, 058 SKIP_READ_BUFFER_SIZE)); 059 } 060 return skipped; 061 } 062 063}