• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package org.jf.util;
33 
34 import javax.annotation.Nonnull;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.RandomAccessFile;
38 
39 public class RandomAccessFileInputStream extends InputStream {
40     private int filePosition;
41     @Nonnull private final RandomAccessFile raf;
42 
RandomAccessFileInputStream(@onnull RandomAccessFile raf, int filePosition)43     public RandomAccessFileInputStream(@Nonnull RandomAccessFile raf, int filePosition) {
44         this.filePosition = filePosition;
45         this.raf = raf;
46     }
47 
read()48     @Override public int read() throws IOException {
49         raf.seek(filePosition);
50         filePosition++;
51         return raf.read();
52     }
53 
read(byte[] bytes)54     @Override public int read(byte[] bytes) throws IOException {
55         raf.seek(filePosition);
56         int bytesRead = raf.read(bytes);
57         filePosition += bytesRead;
58         return bytesRead;
59     }
60 
read(byte[] bytes, int offset, int length)61     @Override public int read(byte[] bytes, int offset, int length) throws IOException {
62         raf.seek(filePosition);
63         int bytesRead = raf.read(bytes, offset, length);
64         filePosition += bytesRead;
65         return bytesRead;
66     }
67 
skip(long l)68     @Override public long skip(long l) throws IOException {
69         int skipBytes = Math.min((int)l, available());
70         filePosition += skipBytes;
71         return skipBytes;
72     }
73 
available()74     @Override public int available() throws IOException {
75         return (int)raf.length() - filePosition;
76     }
77 
markSupported()78     @Override public boolean markSupported() {
79         return false;
80     }
81 }
82