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 org.apache.http.util;
     33 
     34 /**
     35  * A resizable byte array.
     36  *
     37  * @author <a href="mailto:oleg (at) ural.ru">Oleg Kalnichevski</a>
     38  *
     39  * @version $Revision: 496070 $
     40  *
     41  * @since 4.0
     42  */
     43 public final class ByteArrayBuffer  {
     44 
     45     private byte[] buffer;
     46     private int len;
     47 
     48     public ByteArrayBuffer(int capacity) {
     49         super();
     50         if (capacity < 0) {
     51             throw new IllegalArgumentException("Buffer capacity may not be negative");
     52         }
     53         this.buffer = new byte[capacity];
     54     }
     55 
     56     private void expand(int newlen) {
     57         byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)];
     58         System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
     59         this.buffer = newbuffer;
     60     }
     61 
     62     public void append(final byte[] b, int off, int len) {
     63         if (b == null) {
     64             return;
     65         }
     66         if ((off < 0) || (off > b.length) || (len < 0) ||
     67                 ((off + len) < 0) || ((off + len) > b.length)) {
     68             throw new IndexOutOfBoundsException();
     69         }
     70         if (len == 0) {
     71             return;
     72         }
     73         int newlen = this.len + len;
     74         if (newlen > this.buffer.length) {
     75             expand(newlen);
     76         }
     77         System.arraycopy(b, off, this.buffer, this.len, len);
     78         this.len = newlen;
     79     }
     80 
     81     public void append(int b) {
     82         int newlen = this.len + 1;
     83         if (newlen > this.buffer.length) {
     84             expand(newlen);
     85         }
     86         this.buffer[this.len] = (byte)b;
     87         this.len = newlen;
     88     }
     89 
     90     public void append(final char[] b, int off, int len) {
     91         if (b == null) {
     92             return;
     93         }
     94         if ((off < 0) || (off > b.length) || (len < 0) ||
     95                 ((off + len) < 0) || ((off + len) > b.length)) {
     96             throw new IndexOutOfBoundsException();
     97         }
     98         if (len == 0) {
     99             return;
    100         }
    101         int oldlen = this.len;
    102         int newlen = oldlen + len;
    103         if (newlen > this.buffer.length) {
    104             expand(newlen);
    105         }
    106         for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
    107             this.buffer[i2] = (byte) b[i1];
    108         }
    109         this.len = newlen;
    110     }
    111 
    112     public void append(final CharArrayBuffer b, int off, int len) {
    113         if (b == null) {
    114             return;
    115         }
    116         append(b.buffer(), off, len);
    117     }
    118 
    119     public void clear() {
    120         this.len = 0;
    121     }
    122 
    123     public byte[] toByteArray() {
    124         byte[] b = new byte[this.len];
    125         if (this.len > 0) {
    126             System.arraycopy(this.buffer, 0, b, 0, this.len);
    127         }
    128         return b;
    129     }
    130 
    131     public int byteAt(int i) {
    132         return this.buffer[i];
    133     }
    134 
    135     public int capacity() {
    136         return this.buffer.length;
    137     }
    138 
    139     public int length() {
    140         return this.len;
    141     }
    142 
    143     public byte[] buffer() {
    144         return this.buffer;
    145     }
    146 
    147     public void setLength(int len) {
    148         if (len < 0 || len > this.buffer.length) {
    149             throw new IndexOutOfBoundsException();
    150         }
    151         this.len = len;
    152     }
    153 
    154     public boolean isEmpty() {
    155         return this.len == 0;
    156     }
    157 
    158     public boolean isFull() {
    159         return this.len == this.buffer.length;
    160     }
    161 
    162 }
    163