001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package org.apache.harmony.awt;
021:
022: import org.apache.harmony.awt.internal.nls.Messages;
023:
024: /**
025: * Analog of StringBuffer but works with bytes.
026: */
027: public class ByteVector {
028:
029: private static final int DEF_CAPACITY = 16;
030:
031: private byte[] value;
032: private int count;
033:
034: /**
035: * Constructor of new byte buffer.
036: */
037: public ByteVector() {
038: value = new byte[DEF_CAPACITY];
039: count = 0;
040: }
041:
042: /**
043: * Constructor of new byte buffer.
044: *
045: * @param capacity - initial capacity of new buffer.
046: */
047: public ByteVector(int capacity) {
048: value = new byte[capacity];
049: count = 0;
050: }
051:
052: /**
053: * Appends byte to the end of this buffer.
054: *
055: * @param b - byte to be appended.
056: */
057: public ByteVector append(byte b) {
058: ensureCapacity(count + 1);
059: value[count++] = b;
060:
061: return this ;
062: }
063:
064: /**
065: * Appends char (2 bytes) to the end of this buffer.
066: *
067: * @param c - char to be appended.
068: */
069: public ByteVector append(char c) {
070: ensureCapacity(count + 2);
071: value[count++] = (byte) c;
072: value[count++] = (byte) (c >> 8);
073:
074: return this ;
075: }
076:
077: /**
078: * Appends bytes array to the end of this buffer.
079: *
080: * @param bytes - bytes to be appended.
081: */
082: public ByteVector append(byte[] bytes) {
083: int length = bytes.length;
084:
085: ensureCapacity(count + length);
086: System.arraycopy(bytes, 0, value, count, length);
087: count += length;
088:
089: return this ;
090: }
091:
092: /**
093: * Appends bytes from param's buffer to the end of this buffer.
094: *
095: * @param vector - buffer with bytes to be appended.
096: */
097: public ByteVector append(ByteVector vector) {
098: append(vector.getAll());
099:
100: return this ;
101: }
102:
103: /**
104: * Gets amount of bytes in buffer.
105: *
106: * @return number of bytes in buffer.
107: */
108: public int length() {
109: return count;
110: }
111:
112: /**
113: * Gets the byte value in this buffer at the specified index.
114: *
115: * @param index - index of byte to be returned.
116: * @return byte value at specified position.
117: */
118: public byte get(int index) {
119: if ((index < 0) || (index >= count)) {
120: // awt.33=index is out of range
121: throw new RuntimeException(Messages.getString("awt.33")); //$NON-NLS-1$
122: }
123:
124: return value[index];
125: }
126:
127: /**
128: * Gets all the byte from this buffer.
129: *
130: * @return array of bytes in this buffer.
131: */
132: public byte[] getAll() {
133: byte[] res;
134:
135: res = new byte[count];
136: System.arraycopy(value, 0, res, 0, count);
137:
138: return res;
139: }
140:
141: private void ensureCapacity(int minimumCapacity) {
142: if (minimumCapacity <= value.length) {
143: return;
144: }
145:
146: int newLength = value.length * 2 + 2;
147: byte[] newArray;
148:
149: if (minimumCapacity > newLength) {
150: newLength = minimumCapacity;
151: }
152: newArray = new byte[newLength];
153: System.arraycopy(value, 0, newArray, 0, count);
154: value = newArray;
155: }
156:
157: }
|