001/*
002 * Copyright 2008 ZXing authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.itextpdf.text.pdf.qrcode;
018
019/**
020 * This class implements an array of unsigned bytes.
021 *
022 * @author dswitkin@google.com (Daniel Switkin)
023 * @since 5.0.2
024 */
025public final class ByteArray {
026
027  private static final int INITIAL_SIZE = 32;
028
029  private byte[] bytes;
030  private int size;
031
032  public ByteArray() {
033    bytes = null;
034    size = 0;
035  }
036
037  public ByteArray(int size) {
038    bytes = new byte[size];
039    this.size = size;
040  }
041
042  public ByteArray(byte[] byteArray) {
043    bytes = byteArray;
044    size = bytes.length;
045  }
046
047  /**
048   * Access an unsigned byte at location index.
049   * @param index The index in the array to access.
050   * @return The unsigned value of the byte as an int.
051   */
052  public int at(int index) {
053    return bytes[index] & 0xff;
054  }
055
056  public void set(int index, int value) {
057    bytes[index] = (byte) value;
058  }
059
060  public int size() {
061    return size;
062  }
063
064  public boolean isEmpty() {
065    return size == 0;
066  }
067
068  public void appendByte(int value) {
069    if (size == 0 || size >= bytes.length) {
070      int newSize = Math.max(INITIAL_SIZE, size << 1);
071      reserve(newSize);
072    }
073    bytes[size] = (byte) value;
074    size++;
075  }
076
077  public void reserve(int capacity) {
078    if (bytes == null || bytes.length < capacity) {
079      byte[] newArray = new byte[capacity];
080      if (bytes != null) {
081        System.arraycopy(bytes, 0, newArray, 0, bytes.length);
082      }
083      bytes = newArray;
084    }
085  }
086
087  // Copy count bytes from array source starting at offset.
088  public void set(byte[] source, int offset, int count) {
089    bytes = new byte[count];
090    size = count;
091    for (int x = 0; x < count; x++) {
092      bytes[x] = source[offset + x];
093    }
094  }
095
096}