File: java.io.ByteArrayInputStream

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 (62 lines) | stat: -rw-r--r-- 1,209 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
// java.io.ByteArrayInputStream
// An implementation of the Java Language Specification section 22.6
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.

package java.io;

public class ByteArrayInputStream extends InputStream {

	protected byte[] buf;
	protected int pos;
	protected int count;

	public ByteArrayInputStream(byte[] b)
	{
		buf=b; pos=0; count=b.length;
	}

	public ByteArrayInputStream(byte[] b, int off, int len)
	{
		buf=b; pos=off; count=off+len;
	}

	public int read() throws NullPointerException, IndexOutOfBoundsException
	{
		if (pos==count) return -1;
		return buf[pos++] & 0xff;
	}

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

	public long skip(long n)
	{
		int skipped=count-pos;
		if (n<skipped) skipped=n;
		pos+=skipped;
		return skipped;
	}

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

	public void reset()
	{
		pos=0;
	}

}