home *** CD-ROM | disk | FTP | other *** search
- package java.io;
-
- public class ByteArrayOutputStream extends OutputStream {
- protected byte[] buf;
- protected int count;
-
- public ByteArrayOutputStream() {
- this(32);
- }
-
- public ByteArrayOutputStream(int size) {
- this.buf = new byte[size];
- }
-
- public synchronized void write(int b) {
- int newcount = this.count + 1;
- if (newcount > this.buf.length) {
- byte[] newbuf = new byte[Math.max(this.buf.length << 1, newcount)];
- System.arraycopy(this.buf, 0, newbuf, 0, this.count);
- this.buf = newbuf;
- }
-
- this.buf[this.count] = (byte)b;
- this.count = newcount;
- }
-
- public synchronized void write(byte[] b, int off, int len) {
- int newcount = this.count + len;
- if (newcount > this.buf.length) {
- byte[] newbuf = new byte[Math.max(this.buf.length << 1, newcount)];
- System.arraycopy(this.buf, 0, newbuf, 0, this.count);
- this.buf = newbuf;
- }
-
- System.arraycopy(b, off, this.buf, this.count, len);
- this.count = newcount;
- }
-
- public synchronized void writeTo(OutputStream out) throws IOException {
- out.write(this.buf, 0, this.count);
- }
-
- public synchronized void reset() {
- this.count = 0;
- }
-
- public synchronized byte[] toByteArray() {
- byte[] newbuf = new byte[this.count];
- System.arraycopy(this.buf, 0, newbuf, 0, this.count);
- return newbuf;
- }
-
- public int size() {
- return this.count;
- }
-
- public String toString() {
- return new String(this.toByteArray(), 0);
- }
-
- public String toString(int hibyte) {
- return new String(this.toByteArray(), hibyte);
- }
- }
-