1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.io;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.io.UnsupportedEncodingException;
21
22
23
24
25
26 public class ByteArrayBuffer extends AbstractBuffer
27 {
28 private byte[] _bytes;
29
30 public ByteArrayBuffer(byte[] bytes)
31 {
32 this(bytes, 0, bytes.length, READWRITE);
33 }
34
35 public ByteArrayBuffer(byte[] bytes, int index, int length)
36 {
37 this(bytes, index, length, READWRITE);
38 }
39
40 public ByteArrayBuffer(byte[] bytes, int index, int length, int access)
41 {
42 super(READWRITE, NON_VOLATILE);
43 _bytes = bytes;
44 setPutIndex(index + length);
45 setGetIndex(index);
46 _access = access;
47 }
48
49 public ByteArrayBuffer(byte[] bytes, int index, int length, int access, boolean isVolatile)
50 {
51 super(READWRITE, isVolatile);
52 _bytes = bytes;
53 setPutIndex(index + length);
54 setGetIndex(index);
55 _access = access;
56 }
57
58 public ByteArrayBuffer(int size)
59 {
60 this(new byte[size], 0, size, READWRITE);
61 setPutIndex(0);
62 }
63
64 public ByteArrayBuffer(String value)
65 {
66 super(READWRITE,NON_VOLATILE);
67 _bytes = Portable.getBytes(value);
68 setGetIndex(0);
69 setPutIndex(_bytes.length);
70 _access=IMMUTABLE;
71 _string = value;
72 }
73
74 public ByteArrayBuffer(String value,String encoding) throws UnsupportedEncodingException
75 {
76 super(READWRITE,NON_VOLATILE);
77 _bytes = value.getBytes(encoding);
78 setGetIndex(0);
79 setPutIndex(_bytes.length);
80 _access=IMMUTABLE;
81 _string = value;
82 }
83
84 public byte[] array()
85 {
86 return _bytes;
87 }
88
89 public int capacity()
90 {
91 return _bytes.length;
92 }
93
94 public byte get()
95 {
96 return _bytes[_get++];
97 }
98
99 public byte peek(int index)
100 {
101 return _bytes[index];
102 }
103
104 public int peek(int index, byte[] b, int offset, int length)
105 {
106 int l = length;
107 if (index + l > capacity())
108 {
109 l = capacity() - index;
110 if (l==0)
111 return -1;
112 }
113
114 if (l < 0)
115 return -1;
116
117 Portable.arraycopy(_bytes, index, b, offset, l);
118 return l;
119 }
120
121 public void poke(int index, byte b)
122 {
123 if (isReadOnly()) throw new IllegalStateException(__READONLY);
124 if (index < 0) throw new IllegalArgumentException("index<0: " + index + "<0");
125 if (index > capacity())
126 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity());
127 _bytes[index] = b;
128 }
129
130 public static class CaseInsensitive extends ByteArrayBuffer implements Buffer.CaseInsensitve
131 {
132 public CaseInsensitive(String s)
133 {
134 super(s);
135 }
136
137 public CaseInsensitive(byte[] b, int o, int l, int rw)
138 {
139 super(b,o,l,rw);
140 }
141 }
142
143
144
145
146
147
148
149
150 public void wrap(byte[] b, int off, int len)
151 {
152 if (isReadOnly()) throw new IllegalStateException(__READONLY);
153 if (isImmutable()) throw new IllegalStateException(__IMMUTABLE);
154 _bytes=b;
155 clear();
156 setGetIndex(off);
157 setPutIndex(off+len);
158 }
159
160
161
162
163
164 public void wrap(byte[] b)
165 {
166 if (isReadOnly()) throw new IllegalStateException(__READONLY);
167 if (isImmutable()) throw new IllegalStateException(__IMMUTABLE);
168 _bytes=b;
169 setGetIndex(0);
170 setPutIndex(b.length);
171 }
172
173
174 public void writeTo(OutputStream out)
175 throws IOException
176 {
177 out.write(_bytes,getIndex(),length());
178 clear();
179 }
180
181
182 public int readFrom(InputStream in,int max) throws IOException
183 {
184 if (max<0||max>space())
185 max=space();
186 int p = putIndex();
187
188 int len=0, total=0, available=max;
189 while (total<max)
190 {
191 len=in.read(_bytes,p,available);
192 if (len<0)
193 break;
194 else if (len>0)
195 {
196 p += len;
197 total += len;
198 available -= len;
199 setPutIndex(p);
200 }
201 if (in.available()<=0)
202 break;
203 }
204 if (len<0 && total==0)
205 return -1;
206 return total;
207 }
208 }