1 /**
2  *  ServingXML
3  *
4  *  Copyright (C) 2006  Daniel Parker
5  *    daniel.parker@servingxml.com
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  **/
20 
21 package com.servingxml.util;
22 
23 import java.io.IOException;
24 import java.io.InputStream;
25 
26 import com.servingxml.util.ByteArrayBuilder;
27 
28 public class ByteArrayBuilder {
29   private byte[] data;
30   private int size;
31 
ByteArrayBuilder()32   public ByteArrayBuilder() {
33     this.data = new byte[128];
34     this.size = 0;
35   }
36 
ByteArrayBuilder(int capacity)37   public ByteArrayBuilder(int capacity) {
38     this.data = new byte[capacity];
39     this.size = 0;
40   }
41 
buffer()42   public byte[] buffer() {
43     return data;
44   }
45 
length()46   public int length() {
47     return this.size;
48   }
49 
start()50   public int start() {
51     return 0;
52   }
53 
clear()54   public void clear() {
55     size = 0;
56   }
57 
grow(int n)58   public final void grow(int n) {
59 
60     if (size + n >= data.length) {
61       int capacity = data.length;
62       do {
63         capacity *= 2;
64       } while (capacity <= size + n);
65       byte[] newBuffer = new byte[capacity];
66       System.arraycopy(data,0,newBuffer,0,size);
67       data = newBuffer;
68     }
69   }
70 
append(byte b)71   public void append(byte b) {
72     grow(1);
73     data[size++] = b;
74   }
75 
put(int start, byte[] bytes)76   public void put(int start, byte[] bytes) {
77     int delta = start - size + bytes.length;
78     if (delta > 0) {
79       grow(delta);
80     }
81     System.arraycopy(bytes, 0, data, start, bytes.length);
82     if (start+bytes.length > size) {
83       size = start+bytes.length;
84     }
85   }
86 
append(byte[] bytes)87   public void append(byte[] bytes) {
88     grow(bytes.length);
89     System.arraycopy(bytes, 0, data, size, bytes.length);
90     size += bytes.length;
91   }
92 
append(byte[] bytes, int start, int len)93   public void append(byte[] bytes, int start, int len) {
94     grow(len);
95     System.arraycopy(bytes, start, data, size, len);
96     size += len;
97   }
98 
append(InputStream is, int count)99   public int append(InputStream is, int count)
100   throws IOException {
101     grow(count);
102     int len = is.read(data, size, count);
103     if (len >= 0) {
104       size += len;
105     }
106     return len;
107   }
108 
prepend(byte[] bytes, int start, int len)109   public void prepend(byte[] bytes, int start, int len) {
110     grow(len);
111     System.arraycopy(data, 0, data, len, size);
112     System.arraycopy(bytes, start, data, 0, len);
113     size += len;
114   }
115 
toByteArray()116   public byte[] toByteArray() {
117     byte[] bytes = new byte[size];
118     System.arraycopy(data,0,bytes,0,size);
119     return bytes;
120   }
121 }
122