Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/libraries/SlidingFileInputStream.java
6005 views
/*******************************************************************************1* Copyright (c) 1991, 2019 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122package com.ibm.j9ddr.libraries;2324//sliding input stream which presents a portion of a file as the complete stream2526import java.io.File;27import java.io.FileNotFoundException;28import java.io.IOException;29import java.io.InputStream;3031import javax.imageio.stream.FileImageInputStream;32import javax.imageio.stream.ImageInputStream;3334/**35* @author GB004850636*37*/38public class SlidingFileInputStream extends InputStream {39private final long length; //length of the stream40private final ImageInputStream stream;41private final byte[] buffer = new byte[4096];42private int bytesAvailable = 0;43private boolean EOF = false;44private int bufferPos = 0;45private long bytesRead = 0;4647public SlidingFileInputStream(File file, long start, long length) throws FileNotFoundException, IOException {48this.length = length;49stream = new FileImageInputStream(file);50stream.seek(start);51}5253public SlidingFileInputStream(ImageInputStream iis, long start, long length) throws FileNotFoundException, IOException {54this.length = length;55stream = iis;56stream.seek(start);57}5859@Override60public int read() throws IOException {61if(EOF) return -1; //end of file62if(bytesRead == length) {63EOF = true;64return -1;65}66if(bytesAvailable == bufferPos) {67bytesAvailable = stream.read(buffer);68if(bytesAvailable == -1) {69EOF = true;70return -1;71}72bufferPos = 0;73}74bytesRead++;75return 0xFF & buffer[bufferPos++]; //return and increment buffer pointer76}777879/**80* Actually closes the underlying stream. The close() method does not close the stream so as to allow its reuse.81* @throws IOException82*/83public void disposeStream() throws IOException {84stream.close();85}86}878889