source: contrib/MailArchiver/sources/vendor/mime4j/custom/core/src/main/java/org/apache/james/mime4j/util/ByteArrayBuffer.java @ 6785

Revision 6785, 5.2 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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 ****************************************************************/
19
20package org.apache.james.mime4j.util;
21
22
23/**
24 * A resizable byte array.
25 */
26public final class ByteArrayBuffer implements ByteSequence {
27   
28    private byte[] buffer;
29    private int len;
30
31    public ByteArrayBuffer(int capacity) {
32        super();
33        if (capacity < 0) {
34            throw new IllegalArgumentException("Buffer capacity may not be negative");
35        }
36        this.buffer = new byte[capacity];
37    }
38
39    public ByteArrayBuffer(byte[] bytes, boolean dontCopy) {
40        this(bytes, bytes.length, dontCopy);
41    }
42   
43    public ByteArrayBuffer(byte[] bytes, int len, boolean dontCopy) {
44        if (bytes == null)
45            throw new IllegalArgumentException();
46        if (len < 0 || len > bytes.length)
47            throw new IllegalArgumentException();
48
49        if (dontCopy) {
50            this.buffer = bytes;
51        } else {
52            this.buffer = new byte[len];
53            System.arraycopy(bytes, 0, this.buffer, 0, len);
54        }
55
56        this.len = len;
57    }
58
59    private void expand(int newlen) {
60        byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)];
61        System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
62        this.buffer = newbuffer;
63    }
64   
65    public void append(final byte[] b, int off, int len) {
66        if (b == null) {
67            return;
68        }
69        if ((off < 0) || (off > b.length) || (len < 0) ||
70                ((off + len) < 0) || ((off + len) > b.length)) {
71            throw new IndexOutOfBoundsException();
72        }
73        if (len == 0) {
74            return;
75        }
76        int newlen = this.len + len;
77        if (newlen > this.buffer.length) {
78            expand(newlen);
79        }
80        System.arraycopy(b, off, this.buffer, this.len, len);
81        this.len = newlen;
82    }
83
84    public void append(int b) {
85        int newlen = this.len + 1;
86        if (newlen > this.buffer.length) {
87            expand(newlen);
88        }
89        this.buffer[this.len] = (byte)b;
90        this.len = newlen;
91    }
92
93    public void clear() {
94        this.len = 0;
95    }
96   
97    public byte[] toByteArray() {
98        byte[] b = new byte[this.len];
99        if (this.len > 0) {
100            System.arraycopy(this.buffer, 0, b, 0, this.len);
101        }
102        return b;
103    }
104   
105    public byte byteAt(int i) {
106        if (i < 0 || i >= this.len)
107            throw new IndexOutOfBoundsException();
108
109        return this.buffer[i];
110    }
111   
112    public int capacity() {
113        return this.buffer.length;
114    }
115   
116    public int length() {
117        return this.len;
118    }
119
120    public byte[] buffer() {
121        return this.buffer;
122    }
123
124    public int indexOf(byte b) {
125        return indexOf(b, 0, this.len);
126    }
127
128    public int indexOf(byte b, int beginIndex, int endIndex) {
129        if (beginIndex < 0) {
130            beginIndex = 0;
131        }
132        if (endIndex > this.len) {
133            endIndex = this.len;
134        }
135        if (beginIndex > endIndex) {
136            return -1;
137        }
138        for (int i = beginIndex; i < endIndex; i++) {
139            if (this.buffer[i] == b) {
140                return i;
141            }
142        }
143        return -1;
144    }
145
146    public void setLength(int len) {
147        if (len < 0 || len > this.buffer.length) {
148            throw new IndexOutOfBoundsException();
149        }
150        this.len = len;
151    }
152   
153    public void remove(int off, int len) {
154        if ((off < 0) || (off > this.len) || (len < 0) ||
155                ((off + len) < 0) || ((off + len) > this.len)) {
156            throw new IndexOutOfBoundsException();
157        }
158        if (len == 0) {
159            return;
160        }
161        int remaining = this.len - off - len;
162        if (remaining > 0) {
163            System.arraycopy(this.buffer, off + len, this.buffer, off, remaining);
164        }
165        this.len -= len;
166    }
167   
168    public boolean isEmpty() {
169        return this.len == 0;
170    }
171   
172    public boolean isFull() {
173        return this.len == this.buffer.length;
174    }
175
176    @Override
177    public String toString() {
178        return new String(toByteArray());
179    }
180
181}
Note: See TracBrowser for help on using the repository browser.