File: java.io.BufferedInputStream

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (146 lines) | stat: -rw-r--r-- 2,918 bytes parent folder | download | duplicates (3)
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
138
139
140
141
142
143
144
145
146
// java.io.BufferedInputStream
// An implementation of the Java Language Specification section 20.10
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.

package java.io;

public class BufferedInputStream extends FilterInputStream {

	protected byte[] buf;
	protected int count=0;
	protected int pos=0;
	protected int markpos=-1;
	protected int marklimit=0;

	public BufferedInputStream(InputStream s)
	{
		this(s, 1024);
	}

	public BufferedInputStream(InputStream s, int z)
	{
		super(s);
		if (z<4) z=4;
		buf=new byte[z];
	}

	// Make sure we have enough room to read in SIZE bytes
	private void makeRoom(int readSize)
	{
		int preserveFrom=markpos;
		if (preserveFrom==-1) preserveFrom=pos;
		if (pos-preserveFrom>marklimit) preserveFrom=pos;
		byte[] newbuf;
		if (count-preserveFrom+readSize>buf.length) {
			// Grow the buffer
			int newlen=count-preserveFrom+readSize;
			if (marklimit>newlen) {
				if (marklimit>newlen*4) {
					newlen*=4;
				} else {
					newlen=marklimit;
				}
			}
			newbuf=new byte[newlen];
		} else if (count+readSize>buf.length) {
			// Shuffle up
			newbuf=buf;
		} else {
			return;
		}
		if (preserveFrom>markpos) markpos=-1;
		for (int i=preserveFrom; i<count; i++) {
			newbuf[i-preserveFrom]=buf[i];
		}
		count-=preserveFrom;
		pos-=preserveFrom;
		markpos-=preserveFrom;
		buf=newbuf;
	}

	private int getMore(int reqlen) throws IOException
	{
		if (count+reqlen>buf.length) {
			// make room first
			makeRoom(reqlen);
		}
		int read=in.read(buf, count, buf.length-count);
		if (read>=0) count+=read;
		return read;
	}

	public int read() throws IOException
	{
		if (pos>=count) {
			// get more input
			getMore(1);
		}
		if (pos<count)
			return buf[pos++];
		else
			return -1;
	}

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

	public int read(byte[] b, int off, int len)
		throws IOException, NullPointerException,
		       IndexOutOfBoundsException
	{
		if (off<0 || len<0 || off+len>b.length)
			throw new IndexOutOfBoundsException();
		if (len==0) return 0;
		if (pos>=count) getMore(len);
		if (pos>=count) return -1;
		int read=count-pos;
		if (read>len) read=len;
		for (int i=0; i<read; i++) {
			b[i]=buf[pos+i];
		}
		pos+=read;
		return read;
	}

	public long skip(long n) throws IOException
	{
		if (pos>=count && markpos>=0) {
			if (pos-markpos+n<=marklimit) {
				getMore((int) n);
			} else {
				markpos=-1;
			}
		}
		if (pos<count) {
			if (pos+n>count) n=count-pos;
			pos+=(int) n;
			return n;
		}
		return in.skip(n);
	}

	public int available() throws IOException
	{
		return count-pos;
	}

	public void mark(int readlimit)
	{
		marklimit=readlimit;
		markpos=pos;
	}

	public void reset() throws IOException
	{
		if (markpos==-1) throw new IOException("No mark (or mark invalidated)");
		pos=markpos;
	}

	public boolean markSupported()
	{
		return true;
	}

}