summaryrefslogtreecommitdiff
path: root/platform/java/src/com/artifex/mupdf/fitz/BufferInputStream.java
blob: b13556123ca66b11728ba9808202a4ed513af408 (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
package com.artifex.mupdf.fitz;

import java.io.IOException;
import java.io.InputStream;

public class BufferInputStream extends InputStream
{
	protected Buffer buffer;
	protected int position;
	protected int resetPosition;

	public BufferInputStream(Buffer buffer) {
		super();
		this.buffer = buffer;
		this.position = 0;
		this.resetPosition = -1;
	}

	public int available() {
		return buffer.getLength();
	}

	public void mark(int readlimit) {
		resetPosition = position;
	}

	public boolean markSupported() {
		return true;
	}

	public int read() {
		int c = buffer.readByte(position);
		if (c >= 0)
			position++;
		return c;
	}

	public int read(byte[] b) {
		int n = buffer.readBytes(position, b);
		if (n >= 0)
			position += n;
		return n;
	}

	public int read(byte[] b, int off, int len) {
		int n = buffer.readBytesInto(position, b, off, len);
		if (n >= 0)
			position += n;
		return n;
	}

	public void reset() throws IOException {
		if (resetPosition < 0)
			throw  new IOException("cannot reset because mark never set");
		if (resetPosition >= buffer.getLength())
			throw  new IOException("cannot reset because mark set outside of buffer");

		position = resetPosition;
	}
}