aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/XInputStream.java
blob: 8a829660f6bfa6fb9433fb5ff3080e6a74f95f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.internal.storage.file;

import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.jgit.util.NB;

class XInputStream extends BufferedInputStream {
	private final byte[] intbuf = new byte[8];

	XInputStream(InputStream s) {
		super(s);
	}

	synchronized byte[] readFully(final int len) throws IOException {
		final byte[] b = new byte[len];
		readFully(b, 0, len);
		return b;
	}

	synchronized void readFully(byte[] b, int o, int len)
			throws IOException {
		int r;
		while (len > 0 && (r = read(b, o, len)) > 0) {
			o += r;
			len -= r;
		}
		if (len > 0)
			throw new EOFException();
	}

	int readUInt8() throws IOException {
		final int r = read();
		if (r < 0)
			throw new EOFException();
		return r;
	}

	long readUInt32() throws IOException {
		readFully(intbuf, 0, 4);
		return NB.decodeUInt32(intbuf, 0);
	}
}