MIME4J-5 Performance patch 3, https://issues.apache.org/jira/browse/MIME4J-5. Contrib...
[mime4j.git] / src / main / java / org / apache / james / mime4j / ByteArrayBuffer.java
blob7617b58cf623f92bfaddbf0d4af35e7551dd9d03
1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
20 package org.apache.james.mime4j;
22 /**
23 * A resizable byte array.
25 public final class ByteArrayBuffer {
27 private byte[] buffer;
28 private int len;
30 public ByteArrayBuffer(int capacity) {
31 super();
32 if (capacity < 0) {
33 throw new IllegalArgumentException("Buffer capacity may not be negative");
35 this.buffer = new byte[capacity];
38 private void expand(int newlen) {
39 byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)];
40 System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
41 this.buffer = newbuffer;
44 public void append(final byte[] b, int off, int len) {
45 if (b == null) {
46 return;
48 if ((off < 0) || (off > b.length) || (len < 0) ||
49 ((off + len) < 0) || ((off + len) > b.length)) {
50 throw new IndexOutOfBoundsException();
52 if (len == 0) {
53 return;
55 int newlen = this.len + len;
56 if (newlen > this.buffer.length) {
57 expand(newlen);
59 System.arraycopy(b, off, this.buffer, this.len, len);
60 this.len = newlen;
63 public void append(int b) {
64 int newlen = this.len + 1;
65 if (newlen > this.buffer.length) {
66 expand(newlen);
68 this.buffer[this.len] = (byte)b;
69 this.len = newlen;
72 public void append(final char[] b, int off, int len) {
73 if (b == null) {
74 return;
76 if ((off < 0) || (off > b.length) || (len < 0) ||
77 ((off + len) < 0) || ((off + len) > b.length)) {
78 throw new IndexOutOfBoundsException();
80 if (len == 0) {
81 return;
83 int oldlen = this.len;
84 int newlen = oldlen + len;
85 if (newlen > this.buffer.length) {
86 expand(newlen);
88 for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
89 this.buffer[i2] = (byte) b[i1];
91 this.len = newlen;
94 public void clear() {
95 this.len = 0;
98 public byte[] toByteArray() {
99 byte[] b = new byte[this.len];
100 if (this.len > 0) {
101 System.arraycopy(this.buffer, 0, b, 0, this.len);
103 return b;
106 public int byteAt(int i) {
107 return this.buffer[i];
110 public int capacity() {
111 return this.buffer.length;
114 public int length() {
115 return this.len;
118 public byte[] buffer() {
119 return this.buffer;
122 public void setLength(int len) {
123 if (len < 0 || len > this.buffer.length) {
124 throw new IndexOutOfBoundsException();
126 this.len = len;
129 public boolean isEmpty() {
130 return this.len == 0;
133 public boolean isFull() {
134 return this.len == this.buffer.length;
137 public String toString() {
138 return new String(toByteArray());