File: java.io.ByteArrayOutputStream

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

package java.io;

public class ByteArrayOutputStream extends OutputStream {

	protected byte[] buf;
	protected int count;

	public ByteArrayOutputStream() { this(32); }
	public ByteArrayOutputStream(int s) { buf=new byte[s]; count=0; }

	private void ensureSize(int len)
	{
		if (buf.length>=len) return;
		int newlen=buf.length*2;
		if (newlen<len) newlen=len;
		byte[] newbuf=new byte[newlen];
		for (int i=0; i<count; i++)
			newbuf[i]=buf[i];
		buf=newbuf;
	}

	public void write(int b)
	{
		ensureSize(count+1);
		buf[count++]=(byte) b;
	}

	public void write(byte[] b, int off, int len)
			throws NullPointerException, IndexOutOfBoundsException
	{
		if (len==0) return;
		if (off<0 || len<0 || off+len>b.length)
			throw new IndexOutOfBoundsException();
		ensureSize(count+len);
		while (len-->0) {
			buf[count++]=b[off++];
		}
	}

	public int size() { return count; }
	public void reset() { count=0; }

	public byte[] toByteArray()
	{
		byte[] rv=new byte[count];
		for (int i=0; i<count; i++)
			rv[i]=buf[i];
		return rv;
	}

	public String toString() { return toString(0); }
	public String toString(int hi) { return new String(buf, hi, 0, count); }

	public void writeTo(OutputStream out) throws IOException
	{
		out.write(buf, 0, count);
	}

}