001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/util/ByteArrayBuffer.java $
003: * $Revision: 496070 $
004: * $Date: 2007-01-14 13:18:34 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.util;
033:
034: /**
035: * A resizable byte array.
036: *
037: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
038: *
039: * @version $Revision: 496070 $
040: *
041: * @since 4.0
042: */
043: public final class ByteArrayBuffer {
044:
045: private byte[] buffer;
046: private int len;
047:
048: public ByteArrayBuffer(int capacity) {
049: super ();
050: if (capacity < 0) {
051: throw new IllegalArgumentException(
052: "Buffer capacity may not be negative");
053: }
054: this .buffer = new byte[capacity];
055: }
056:
057: private void expand(int newlen) {
058: byte newbuffer[] = new byte[Math.max(this .buffer.length << 1,
059: newlen)];
060: System.arraycopy(this .buffer, 0, newbuffer, 0, this .len);
061: this .buffer = newbuffer;
062: }
063:
064: public void append(final byte[] b, int off, int len) {
065: if (b == null) {
066: return;
067: }
068: if ((off < 0) || (off > b.length) || (len < 0)
069: || ((off + len) < 0) || ((off + len) > b.length)) {
070: throw new IndexOutOfBoundsException();
071: }
072: if (len == 0) {
073: return;
074: }
075: int newlen = this .len + len;
076: if (newlen > this .buffer.length) {
077: expand(newlen);
078: }
079: System.arraycopy(b, off, this .buffer, this .len, len);
080: this .len = newlen;
081: }
082:
083: public void append(int b) {
084: int newlen = this .len + 1;
085: if (newlen > this .buffer.length) {
086: expand(newlen);
087: }
088: this .buffer[this .len] = (byte) b;
089: this .len = newlen;
090: }
091:
092: public void append(final char[] b, int off, int len) {
093: if (b == null) {
094: return;
095: }
096: if ((off < 0) || (off > b.length) || (len < 0)
097: || ((off + len) < 0) || ((off + len) > b.length)) {
098: throw new IndexOutOfBoundsException();
099: }
100: if (len == 0) {
101: return;
102: }
103: int oldlen = this .len;
104: int newlen = oldlen + len;
105: if (newlen > this .buffer.length) {
106: expand(newlen);
107: }
108: for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
109: this .buffer[i2] = (byte) b[i1];
110: }
111: this .len = newlen;
112: }
113:
114: public void append(final CharArrayBuffer b, int off, int len) {
115: if (b == null) {
116: return;
117: }
118: append(b.buffer(), off, len);
119: }
120:
121: public void clear() {
122: this .len = 0;
123: }
124:
125: public byte[] toByteArray() {
126: byte[] b = new byte[this .len];
127: if (this .len > 0) {
128: System.arraycopy(this .buffer, 0, b, 0, this .len);
129: }
130: return b;
131: }
132:
133: public int byteAt(int i) {
134: return this .buffer[i];
135: }
136:
137: public int capacity() {
138: return this .buffer.length;
139: }
140:
141: public int length() {
142: return this .len;
143: }
144:
145: public byte[] buffer() {
146: return this .buffer;
147: }
148:
149: public void setLength(int len) {
150: if (len < 0 || len > this .buffer.length) {
151: throw new IndexOutOfBoundsException();
152: }
153: this .len = len;
154: }
155:
156: public boolean isEmpty() {
157: return this .len == 0;
158: }
159:
160: public boolean isFull() {
161: return this.len == this.buffer.length;
162: }
163:
164: }
|