Skip to main content
summaryrefslogtreecommitdiffstats
blob: f026bdab4b3fe1b7a8ac2de24360437c2c5c5372 (plain) (blame)
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
package org.eclipse.wst.sse.core.internal.encoding.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

/**
 * This is a pretty limited implementation, sort of specific 
 * to the way its used by tokenizers (JFlex). To really 
 * be general purpose, would need more work. 
 *
 */


public class BufferedLimitedReader extends BufferedReader {
	private int limitedCount;
	private int nRead;

	public BufferedLimitedReader(Reader reader, int size) {
		super(reader, size);
		if (reader.markSupported()) {
			try {
				mark(size);
			}
			catch (IOException e) {
				// impossible
				e.printStackTrace();
			}
		}
		limitedCount = size;
	}

	public int read() throws IOException {
		int result = 0;
		nRead++;
		if (nRead > limitedCount) {
			result = -1;
		}
		else {
			result = super.read();
		}
		return result;

	}

	public int read(char cbuf[], int off, int len) throws IOException {
		int result = 0;
		if (nRead + len > limitedCount) {
			result = -1;
		}
		else {
			result = super.read(cbuf, off, len);
			nRead = nRead + result;
		}
		return result;
	}
	
}

Back to the top