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 * For the license checker 31 * Licensed under the Apache License, Version 2.0 32 * 33 */ 34 35 package com.android.tv.tuner.util; 36 37 /** An expandable byte buffer built on byte array. */ 38 public final class ByteArrayBuffer { 39 40 private byte[] buffer; 41 private int len; 42 ByteArrayBuffer(int capacity)43 public ByteArrayBuffer(int capacity) { 44 super(); 45 if (capacity < 0) { 46 throw new IllegalArgumentException("Buffer capacity may not be negative"); 47 } 48 this.buffer = new byte[capacity]; 49 } 50 expand(int newlen)51 private void expand(int newlen) { 52 byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)]; 53 System.arraycopy(this.buffer, 0, newbuffer, 0, this.len); 54 this.buffer = newbuffer; 55 } 56 append(final byte[] b, int off, int len)57 public void append(final byte[] b, int off, int len) { 58 if (b == null) { 59 return; 60 } 61 if ((off < 0) 62 || (off > b.length) 63 || (len < 0) 64 || ((off + len) < 0) 65 || ((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 append(int b)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 append(final char[] b, int off, int len)88 public void append(final char[] b, int off, int len) { 89 if (b == null) { 90 return; 91 } 92 if ((off < 0) 93 || (off > b.length) 94 || (len < 0) 95 || ((off + len) < 0) 96 || ((off + len) > b.length)) { 97 throw new IndexOutOfBoundsException(); 98 } 99 if (len == 0) { 100 return; 101 } 102 int oldlen = this.len; 103 int newlen = oldlen + len; 104 if (newlen > this.buffer.length) { 105 expand(newlen); 106 } 107 for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) { 108 this.buffer[i2] = (byte) b[i1]; 109 } 110 this.len = newlen; 111 } 112 clear()113 public void clear() { 114 this.len = 0; 115 } 116 toByteArray()117 public byte[] toByteArray() { 118 byte[] b = new byte[this.len]; 119 if (this.len > 0) { 120 System.arraycopy(this.buffer, 0, b, 0, this.len); 121 } 122 return b; 123 } 124 byteAt(int i)125 public int byteAt(int i) { 126 return this.buffer[i]; 127 } 128 capacity()129 public int capacity() { 130 return this.buffer.length; 131 } 132 length()133 public int length() { 134 return this.len; 135 } 136 buffer()137 public byte[] buffer() { 138 return this.buffer; 139 } 140 setLength(int len)141 public void setLength(int len) { 142 if (len < 0 || len > this.buffer.length) { 143 throw new IndexOutOfBoundsException(); 144 } 145 this.len = len; 146 } 147 isEmpty()148 public boolean isEmpty() { 149 return this.len == 0; 150 } 151 isFull()152 public boolean isFull() { 153 return this.len == this.buffer.length; 154 } 155 } 156