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
;
22 import org
.apache
.james
.mime4j
.util
.MessageUtils
;
25 * A resizable char array.
28 public final class CharArrayBuffer
{
30 private char[] buffer
;
33 public CharArrayBuffer(int capacity
) {
36 throw new IllegalArgumentException("Buffer capacity may not be negative");
38 this.buffer
= new char[capacity
];
41 private void expand(int newlen
) {
42 char newbuffer
[] = new char[Math
.max(this.buffer
.length
<< 1, newlen
)];
43 System
.arraycopy(this.buffer
, 0, newbuffer
, 0, this.len
);
44 this.buffer
= newbuffer
;
47 public void append(final char[] b
, int off
, int len
) {
51 if ((off
< 0) || (off
> b
.length
) || (len
< 0) ||
52 ((off
+ len
) < 0) || ((off
+ len
) > b
.length
)) {
53 throw new IndexOutOfBoundsException();
58 int newlen
= this.len
+ len
;
59 if (newlen
> this.buffer
.length
) {
62 System
.arraycopy(b
, off
, this.buffer
, this.len
, len
);
66 public void append(String str
) {
70 int strlen
= str
.length();
71 int newlen
= this.len
+ strlen
;
72 if (newlen
> this.buffer
.length
) {
75 str
.getChars(0, strlen
, this.buffer
, this.len
);
79 public void append(final CharArrayBuffer b
, int off
, int len
) {
83 append(b
.buffer
, off
, len
);
86 public void append(final CharArrayBuffer b
) {
90 append(b
.buffer
,0, b
.len
);
93 public void append(char ch
) {
94 int newlen
= this.len
+ 1;
95 if (newlen
> this.buffer
.length
) {
98 this.buffer
[this.len
] = ch
;
102 public void append(final byte[] b
, int off
, int len
) {
106 if ((off
< 0) || (off
> b
.length
) || (len
< 0) ||
107 ((off
+ len
) < 0) || ((off
+ len
) > b
.length
)) {
108 throw new IndexOutOfBoundsException();
113 int oldlen
= this.len
;
114 int newlen
= oldlen
+ len
;
115 if (newlen
> this.buffer
.length
) {
118 for (int i1
= off
, i2
= oldlen
; i2
< newlen
; i1
++, i2
++) {
123 this.buffer
[i2
] = (char) ch
;
128 public void append(final ByteArrayBuffer b
, int off
, int len
) {
132 append(b
.buffer(), off
, len
);
135 public void append(final Object obj
) {
136 append(String
.valueOf(obj
));
139 public void clear() {
143 public char[] toCharArray() {
144 char[] b
= new char[this.len
];
146 System
.arraycopy(this.buffer
, 0, b
, 0, this.len
);
151 public char charAt(int i
) {
152 return this.buffer
[i
];
155 public char[] buffer() {
159 public int capacity() {
160 return this.buffer
.length
;
163 public int length() {
167 public void ensureCapacity(int required
) {
168 int available
= this.buffer
.length
- this.len
;
169 if (required
> available
) {
170 expand(this.len
+ required
);
174 public void setLength(int len
) {
175 if (len
< 0 || len
> this.buffer
.length
) {
176 throw new IndexOutOfBoundsException();
181 public boolean isEmpty() {
182 return this.len
== 0;
185 public boolean isFull() {
186 return this.len
== this.buffer
.length
;
189 public int indexOf(int ch
, int beginIndex
, int endIndex
) {
190 if (beginIndex
< 0) {
193 if (endIndex
> this.len
) {
196 if (beginIndex
> endIndex
) {
199 for (int i
= beginIndex
; i
< endIndex
; i
++) {
200 if (this.buffer
[i
] == ch
) {
207 public int indexOf(int ch
) {
208 return indexOf(ch
, 0, this.len
);
211 public String
substring(int beginIndex
, int endIndex
) {
212 if (beginIndex
< 0) {
213 throw new IndexOutOfBoundsException();
215 if (endIndex
> this.len
) {
216 throw new IndexOutOfBoundsException();
218 if (beginIndex
> endIndex
) {
219 throw new IndexOutOfBoundsException();
221 return new String(this.buffer
, beginIndex
, endIndex
- beginIndex
);
224 public String
substringTrimmed(int beginIndex
, int endIndex
) {
225 if (beginIndex
< 0) {
226 throw new IndexOutOfBoundsException();
228 if (endIndex
> this.len
) {
229 throw new IndexOutOfBoundsException();
231 if (beginIndex
> endIndex
) {
232 throw new IndexOutOfBoundsException();
234 while (beginIndex
< endIndex
&& MessageUtils
.isWhitespace(this.buffer
[beginIndex
])) {
237 while (endIndex
> beginIndex
&& MessageUtils
.isWhitespace(this.buffer
[endIndex
- 1])) {
240 return new String(this.buffer
, beginIndex
, endIndex
- beginIndex
);
243 public String
toString() {
244 return new String(this.buffer
, 0, this.len
);