aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/util/io/ByteBufferInputStream.java
blob: 804f7f860aa72595d5f3a567341857b5e248e8af (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2023, SAP SE 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.util.io;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.InvalidMarkException;
import java.util.Objects;

import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.internal.JGitText;

/**
 * An {@link InputStream} backed by a {@link ByteBuffer}.
 *
 * @since 6.8
 */
public class ByteBufferInputStream extends InputStream {

	private ByteBuffer buf;

	/**
	 * Creates a {@link ByteBufferInputStream}
	 *
	 * @param buf
	 *            the ByteBuffer backing the stream
	 */
	public ByteBufferInputStream(@NonNull ByteBuffer buf) {
		this.buf = buf;
	}

	@Override
	public int read() throws IOException {
		nullCheck();
		if (buf.hasRemaining()) {
			return buf.get() & 0xFF;
		}
		return -1;
	}

	@Override
	public int read(byte[] b) throws IOException {
		nullCheck();
		return read(b, 0, b.length);
	}

	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		nullCheck();
		Objects.checkFromIndexSize(off, len, b.length);
		if (len == 0) {
			return 0;
		}
		int length = Math.min(buf.remaining(), len);
		if (length == 0) {
			return -1;
		}
		buf.get(b, off, length);
		return length;
	}

	@Override
	public byte[] readAllBytes() throws IOException {
		return readNBytes(buf.remaining());
	}

	@Override
	public byte[] readNBytes(int len) throws IOException {
		int l = Math.min(len, buf.remaining());
		byte[] b = new byte[l];
		read(b);
		return b;
	}

	@Override
	public int readNBytes(byte[] b, int off, int len) throws IOException {
		return read(b, off, len);
	}

	@Override
	public long skip(long n) throws IOException {
		nullCheck();
		if (n <= 0) {
			return 0;
		}
		// ByteBuffer index has type int
		int delta = n > Integer.MAX_VALUE ? buf.remaining()
				: Math.min((int) n, buf.remaining());
		buf.position(buf.position() + delta);
		return delta;
	}

	@Override
	public int available() throws IOException {
		nullCheck();
		return buf.remaining();
	}

	@Override
	public void close() {
		buf = null;
	}

	@Override
	public synchronized void mark(int readlimit) {
		buf.mark();
	}

	@Override
	public synchronized void reset() throws IOException {
		try {
			buf.reset();
		} catch (InvalidMarkException e) {
			throw new IOException(e);
		}
	}

	@Override
	public boolean markSupported() {
		return true;
	}

	private void nullCheck() throws IOException {
		if (buf == null) {
			throw new IOException(JGitText.get().inputStreamClosed);
		}
	}
}