Home » HttpComponents-Core-4.0.1 » org.apache.http.io » [javadoc | source]

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

Home » HttpComponents-Core-4.0.1 » org.apache.http.io » [javadoc | source]