View Javadoc
1   /*
2    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> 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.BufferedInputStream;
14  import java.io.EOFException;
15  import java.io.IOException;
16  import java.io.InputStream;
17  
18  import org.eclipse.jgit.util.NB;
19  
20  class XInputStream extends BufferedInputStream {
21  	private final byte[] intbuf = new byte[8];
22  
23  	XInputStream(InputStream s) {
24  		super(s);
25  	}
26  
27  	synchronized byte[] readFully(final int len) throws IOException {
28  		final byte[] b = new byte[len];
29  		readFully(b, 0, len);
30  		return b;
31  	}
32  
33  	synchronized void readFully(byte[] b, int o, int len)
34  			throws IOException {
35  		int r;
36  		while (len > 0 && (r = read(b, o, len)) > 0) {
37  			o += r;
38  			len -= r;
39  		}
40  		if (len > 0)
41  			throw new EOFException();
42  	}
43  
44  	int readUInt8() throws IOException {
45  		final int r = read();
46  		if (r < 0)
47  			throw new EOFException();
48  		return r;
49  	}
50  
51  	long readUInt32() throws IOException {
52  		readFully(intbuf, 0, 4);
53  		return NB.decodeUInt32(intbuf, 0);
54  	}
55  }