Debian Codegraph @

plugins/org.eclipse.emf.gwt.common/src/org/eclipse/emf/emul/java/io/ByteArrayOutputStream.java

raw
1 /**
2 * Copyright (c) 2010 Ed Merks and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * Ed Merks - Initial API and implementation
10 */
11 package java.io;
12
13 import java.io.IOException;
14
15 public class ByteArrayOutputStream extends OutputStream
16 {
17 protected byte [] bytes;
18 protected int index;
19
20 public ByteArrayOutputStream()
21 {
22 this(1024);
23 }
24
25 public ByteArrayOutputStream(int capacity)
26 {
27 bytes = new byte[capacity];
28 }
29
30 @Override
31 public void write(int byteValue) throws IOException
32 {
33 if (index >= bytes.length)
34 {
35 byte[] newBytes = new byte[bytes.length * 2];
36 System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
37 bytes = newBytes;
38 }
39 bytes[index++] = (byte)byteValue;
40 }
41
42 public byte[] toByteArray()
43 {
44 byte[] result = new byte[index];
45 System.arraycopy(bytes, 0, result, 0, index);
46 return result;
47 }
48 }