Home | History | Annotate | Download | only in util
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ByteArrayBuffer.java $
      3  * $Revision: 496070 $
      4  * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package com.android.tv.tuner.util;
     33 
     34 /**
     35  * An expandable byte buffer built on byte array.
     36  */
     37 public final class ByteArrayBuffer {
     38 
     39     private byte[] buffer;
     40     private int len;
     41 
     42     public ByteArrayBuffer(int capacity) {
     43         super();
     44         if (capacity < 0) {
     45             throw new IllegalArgumentException("Buffer capacity may not be negative");
     46         }
     47         this.buffer = new byte[capacity];
     48     }
     49 
     50     private void expand(int newlen) {
     51         byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)];
     52         System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
     53         this.buffer = newbuffer;
     54     }
     55 
     56     public void append(final byte[] b, int off, int len) {
     57         if (b == null) {
     58             return;
     59         }
     60         if ((off < 0) || (off > b.length) || (len < 0) ||
     61                 ((off + len) < 0) || ((off + len) > b.length)) {
     62             throw new IndexOutOfBoundsException();
     63         }
     64         if (len == 0) {
     65             return;
     66         }
     67         int newlen = this.len + len;
     68         if (newlen > this.buffer.length) {
     69             expand(newlen);
     70         }
     71         System.arraycopy(b, off, this.buffer, this.len, len);
     72         this.len = newlen;
     73     }
     74 
     75     public void append(int b) {
     76         int newlen = this.len + 1;
     77         if (newlen > this.buffer.length) {
     78             expand(newlen);
     79         }
     80         this.buffer[this.len] = (byte) b;
     81         this.len = newlen;
     82     }
     83 
     84     public void append(final char[] b, int off, int len) {
     85         if (b == null) {
     86             return;
     87         }
     88         if ((off < 0) || (off > b.length) || (len < 0) ||
     89                 ((off + len) < 0) || ((off + len) > b.length)) {
     90             throw new IndexOutOfBoundsException();
     91         }
     92         if (len == 0) {
     93             return;
     94         }
     95         int oldlen = this.len;
     96         int newlen = oldlen + len;
     97         if (newlen > this.buffer.length) {
     98             expand(newlen);
     99         }
    100         for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
    101             this.buffer[i2] = (byte) b[i1];
    102         }
    103         this.len = newlen;
    104     }
    105 
    106     public void clear() {
    107         this.len = 0;
    108     }
    109 
    110     public byte[] toByteArray() {
    111         byte[] b = new byte[this.len];
    112         if (this.len > 0) {
    113             System.arraycopy(this.buffer, 0, b, 0, this.len);
    114         }
    115         return b;
    116     }
    117 
    118     public int byteAt(int i) {
    119         return this.buffer[i];
    120     }
    121 
    122     public int capacity() {
    123         return this.buffer.length;
    124     }
    125 
    126     public int length() {
    127         return this.len;
    128     }
    129 
    130     public byte[] buffer() {
    131         return this.buffer;
    132     }
    133 
    134     public void setLength(int len) {
    135         if (len < 0 || len > this.buffer.length) {
    136             throw new IndexOutOfBoundsException();
    137         }
    138         this.len = len;
    139     }
    140 
    141     public boolean isEmpty() {
    142         return this.len == 0;
    143     }
    144 
    145     public boolean isFull() {
    146         return this.len == this.buffer.length;
    147     }
    148 
    149 }
    150