2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.qrcode.encoder;
20 * This class implements an array of unsigned bytes.
22 * @author dswitkin@google.com (Daniel Switkin)
24 public final class ByteArray {
26 private static final int INITIAL_SIZE = 32;
36 public ByteArray(String string) {
37 bytes = string.getBytes();
41 public ByteArray(byte[] byteArray) {
47 * Access an unsigned byte at location index.
48 * @param index The index in the array to access.
49 * @return The unsigned value of the byte as an int.
51 public int at(int index) {
52 return bytes[index] & 0xff;
59 public boolean empty() {
63 public void appendByte(int value) {
64 if (size == 0 || size >= bytes.length) {
65 int newSize = Math.max(INITIAL_SIZE, size * 2);
68 bytes[size] = (byte) value;
72 public void reserve(int capacity) {
73 if (bytes == null || bytes.length < capacity) {
74 byte[] newArray = new byte[capacity];
76 System.arraycopy(bytes, 0, newArray, 0, bytes.length);
82 // This could probably be generalized to take a byte[] instead of a BitVector.
83 public void set(BitVector bits, int offset, int count) {
84 bytes = new byte[count];
86 for (int x = 0; x < count; x++) {
87 bytes[x] = (byte) bits.at(x + offset);