Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/debugtools/DDR_VM/src/com/ibm/j9ddr/libraries/SlidingFileInputStream.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 1991, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
23
package com.ibm.j9ddr.libraries;
24
25
//sliding input stream which presents a portion of a file as the complete stream
26
27
import java.io.File;
28
import java.io.FileNotFoundException;
29
import java.io.IOException;
30
import java.io.InputStream;
31
32
import javax.imageio.stream.FileImageInputStream;
33
import javax.imageio.stream.ImageInputStream;
34
35
/**
36
* @author GB0048506
37
*
38
*/
39
public class SlidingFileInputStream extends InputStream {
40
private final long length; //length of the stream
41
private final ImageInputStream stream;
42
private final byte[] buffer = new byte[4096];
43
private int bytesAvailable = 0;
44
private boolean EOF = false;
45
private int bufferPos = 0;
46
private long bytesRead = 0;
47
48
public SlidingFileInputStream(File file, long start, long length) throws FileNotFoundException, IOException {
49
this.length = length;
50
stream = new FileImageInputStream(file);
51
stream.seek(start);
52
}
53
54
public SlidingFileInputStream(ImageInputStream iis, long start, long length) throws FileNotFoundException, IOException {
55
this.length = length;
56
stream = iis;
57
stream.seek(start);
58
}
59
60
@Override
61
public int read() throws IOException {
62
if(EOF) return -1; //end of file
63
if(bytesRead == length) {
64
EOF = true;
65
return -1;
66
}
67
if(bytesAvailable == bufferPos) {
68
bytesAvailable = stream.read(buffer);
69
if(bytesAvailable == -1) {
70
EOF = true;
71
return -1;
72
}
73
bufferPos = 0;
74
}
75
bytesRead++;
76
return 0xFF & buffer[bufferPos++]; //return and increment buffer pointer
77
}
78
79
80
/**
81
* Actually closes the underlying stream. The close() method does not close the stream so as to allow its reuse.
82
* @throws IOException
83
*/
84
public void disposeStream() throws IOException {
85
stream.close();
86
}
87
}
88
89