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 *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
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
;
23 * A resizable byte array.
25 public final class ByteArrayBuffer
{
27 private byte[] buffer
;
30 public ByteArrayBuffer(int capacity
) {
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
) {
48 if ((off
< 0) || (off
> b
.length
) || (len
< 0) ||
49 ((off
+ len
) < 0) || ((off
+ len
) > b
.length
)) {
50 throw new IndexOutOfBoundsException();
55 int newlen
= this.len
+ len
;
56 if (newlen
> this.buffer
.length
) {
59 System
.arraycopy(b
, off
, this.buffer
, this.len
, len
);
63 public void append(int b
) {
64 int newlen
= this.len
+ 1;
65 if (newlen
> this.buffer
.length
) {
68 this.buffer
[this.len
] = (byte)b
;
72 public void append(final char[] b
, int off
, int len
) {
76 if ((off
< 0) || (off
> b
.length
) || (len
< 0) ||
77 ((off
+ len
) < 0) || ((off
+ len
) > b
.length
)) {
78 throw new IndexOutOfBoundsException();
83 int oldlen
= this.len
;
84 int newlen
= oldlen
+ len
;
85 if (newlen
> this.buffer
.length
) {
88 for (int i1
= off
, i2
= oldlen
; i2
< newlen
; i1
++, i2
++) {
89 this.buffer
[i2
] = (byte) b
[i1
];
98 public byte[] toByteArray() {
99 byte[] b
= new byte[this.len
];
101 System
.arraycopy(this.buffer
, 0, b
, 0, this.len
);
106 public int byteAt(int i
) {
107 return this.buffer
[i
];
110 public int capacity() {
111 return this.buffer
.length
;
114 public int length() {
118 public byte[] buffer() {
122 public void setLength(int len
) {
123 if (len
< 0 || len
> this.buffer
.length
) {
124 throw new IndexOutOfBoundsException();
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());