View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import java.io.IOException;
14  import java.io.InputStream;
15  
16  class PackInputStream extends InputStream {
17  	private final WindowCursor wc;
18  
19  	private final PackFile pack;
20  
21  	private long pos;
22  
23  	PackInputStream(PackFile pack, long pos, WindowCursor wc)
24  			throws IOException {
25  		this.pack = pack;
26  		this.pos = pos;
27  		this.wc = wc;
28  
29  		// Pin the first window, to ensure the pack is open and valid.
30  		//
31  		wc.pin(pack, pos);
32  	}
33  
34  	/** {@inheritDoc} */
35  	@Override
36  	public int read(byte[] b, int off, int len) throws IOException {
37  		int n = wc.copy(pack, pos, b, off, len);
38  		pos += n;
39  		return n;
40  	}
41  
42  	/** {@inheritDoc} */
43  	@Override
44  	public int read() throws IOException {
45  		byte[] buf = new byte[1];
46  		int n = read(buf, 0, 1);
47  		return n == 1 ? buf[0] & 0xff : -1;
48  	}
49  
50  	/** {@inheritDoc} */
51  	@Override
52  	public void close() {
53  		wc.close();
54  	}
55  }