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.IOException; 021import java.io.InputStream; 022import java.io.RandomAccessFile; 023 024/** 025 * This class wraps a <code>RandomAccessFile</code> into a usable 026 * <code>InputStream</code> which supports random re-position. 027 * Re-positioning is done by using seek() on the <code>RandomAccessFile</code> 028 * object. (@see RandomAccessFile#seek()) 029 * 030 * @author nicl 031 */ 032public class RandomAccessFileInputStream extends InputStream { 033 034 /** Encapsulated <code>RandomAccessFile</code> used for stream data. */ 035 protected RandomAccessFile raf; 036 037 /** Current mark position in file. */ 038 protected long mark_position = -1; 039 040 /** 041 * Create a new random access <code>InputStream</code> with repositioning 042 * capabilities. 043 * @param raf <code>RandomAccessFile</code> used for stream data 044 */ 045 public RandomAccessFileInputStream(RandomAccessFile raf) { 046 this.raf = raf; 047 } 048 049 /** 050 * Closing this stream has no effect. 051 * @throws IOException if an I/O error occurs while closing stream 052 */ 053 @Override 054 public void close() throws IOException { 055 raf = null; 056 } 057 058 @Override 059 public boolean markSupported() { 060 return true; 061 } 062 063 @Override 064 public synchronized void mark(int readlimit) { 065 try { 066 mark_position = raf.getFilePointer(); 067 } catch (IOException e) { 068 throw new IllegalStateException(); 069 } 070 } 071 072 @Override 073 public synchronized void reset() throws IOException { 074 if (mark_position == -1) { 075 throw new IOException("Mark not set or is invalid"); 076 } 077 raf.seek(mark_position); 078 } 079 080 @Override 081 public int available() throws IOException { 082 long avail = raf.length() - raf.getFilePointer(); 083 return (int) (Math.min(avail, Integer.MAX_VALUE)); 084 } 085 086 @Override 087 public long skip(long n) throws IOException { 088 long skip = Math.min(n, raf.length() - raf.getFilePointer()); 089 raf.seek(raf.getFilePointer() + skip); 090 return skip; 091 } 092 093 @Override 094 public int read() throws IOException { 095 return raf.read(); 096 } 097 098 @Override 099 public int read(byte[] b) throws IOException { 100 return raf.read(b); 101 } 102 103 @Override 104 public int read(byte[] b, int off, int len) throws IOException { 105 return raf.read(b, off, len); 106 } 107 108}