blob: e32f381555b20d652cc4189bc06a4596ed4b9f47 [file] [log] [blame]
slowr60d4d102017-08-16 18:33:58 -07001/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4package org.onosproject.xran.codecs.ber;
5
6import java.io.IOException;
7import java.io.OutputStream;
8import java.nio.ByteBuffer;
9
10public class BerByteArrayOutputStream extends OutputStream {
11
12 private final boolean automaticResize;
13 public byte[] buffer;
14 public int index;
15
16 /**
17 * Creates a <code>BerByteArrayOutputStream</code> with a byte array of size <code>bufferSize</code>. The buffer
18 * will not be resized automatically. Use {@link #BerByteArrayOutputStream(int, boolean)} instead if you want the
19 * buffer to be dynamically resized.
20 *
21 * @param bufferSize the size of the underlying buffer
22 */
23 public BerByteArrayOutputStream(int bufferSize) {
24 this(new byte[bufferSize], bufferSize - 1, false);
25 }
26
27 public BerByteArrayOutputStream(int bufferSize, boolean automaticResize) {
28 this(new byte[bufferSize], bufferSize - 1, automaticResize);
29 }
30
31 public BerByteArrayOutputStream(byte[] buffer) {
32 this(buffer, buffer.length - 1, false);
33 }
34
35 public BerByteArrayOutputStream(byte[] buffer, int startingIndex) {
36 this(buffer, startingIndex, false);
37 }
38
39 public BerByteArrayOutputStream(byte[] buffer, int startingIndex, boolean automaticResize) {
40 if (buffer.length <= 0) {
41 throw new IllegalArgumentException("buffer size may not be <= 0");
42 }
43 this.buffer = buffer;
44 index = startingIndex;
45 this.automaticResize = automaticResize;
46 }
47
48 @Override
49 public void write(int arg0) throws IOException {
50 write((byte) arg0);
51 }
52
53 public void write(byte arg0) throws IOException {
54 try {
55 buffer[index] = arg0;
56 } catch (ArrayIndexOutOfBoundsException e) {
57 if (automaticResize) {
58 resize();
59 buffer[index] = arg0;
60 } else {
61 throw new ArrayIndexOutOfBoundsException("buffer.length = " + buffer.length);
62 }
63 }
64 index--;
65 }
66
67 private void resize() {
68 byte[] newBuffer = new byte[buffer.length * 2];
69 System.arraycopy(buffer, index + 1, newBuffer, buffer.length + index + 1, buffer.length - index - 1);
70 index += buffer.length;
71 buffer = newBuffer;
72
73 }
74
75 @Override
76 public void write(byte[] byteArray) throws IOException {
77 for (int i = byteArray.length - 1; i >= 0; i--) {
78 try {
79 buffer[index] = byteArray[i];
80 } catch (ArrayIndexOutOfBoundsException e) {
81 if (automaticResize) {
82 resize();
83 buffer[index] = byteArray[i];
84 } else {
85 throw new ArrayIndexOutOfBoundsException("buffer.length = " + buffer.length);
86 }
87 }
88 index--;
89 }
90 }
91
92 /**
93 * Returns a new array containing the subarray of the stream array that contains the coded content.
94 *
95 * @return a new array containing the subarray of the stream array
96 */
97 public byte[] getArray() {
98 if (index == -1) {
99 return buffer;
100 }
101 int subBufferLength = buffer.length - index - 1;
102 byte[] subBuffer = new byte[subBufferLength];
103 System.arraycopy(buffer, index + 1, subBuffer, 0, subBufferLength);
104 return subBuffer;
105
106 }
107
108 public ByteBuffer getByteBuffer() {
109 return ByteBuffer.wrap(buffer, index + 1, buffer.length - (index + 1));
110 }
111
112 public void reset() {
113 index = buffer.length - 1;
114 }
115}