Skip to main content
summaryrefslogblamecommitdiffstats
blob: 624f752c6106af6db839f25f7959e05bf1bdeb1e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439






















































































































































































































































































































































































































































                                                                                                             
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.eclipse.jetty.util.StringUtil;

/* ------------------------------------------------------------------------------- */
/**
 * 
 */
public class ByteArrayBuffer extends AbstractBuffer
{
    // Set a maximum size to a write for the writeTo method, to ensure that very large content is not
    // written as a single write (which may fall foul to write timeouts if consumed slowly).
    final static int MAX_WRITE=Integer.getInteger("org.eclipse.jetty.io.ByteArrayBuffer.MAX_WRITE",128*1024);
    final protected byte[] _bytes;

    protected ByteArrayBuffer(int size, int access, boolean isVolatile)
    {
        this(new byte[size],0,0,access, isVolatile);
    }
    
    public ByteArrayBuffer(byte[] bytes)
    {
        this(bytes, 0, bytes.length, READWRITE);
    }

    public ByteArrayBuffer(byte[] bytes, int index, int length)
    {
        this(bytes, index, length, READWRITE);
    }

    public ByteArrayBuffer(byte[] bytes, int index, int length, int access)
    {
        super(READWRITE, NON_VOLATILE);
        _bytes = bytes;
        setPutIndex(index + length);
        setGetIndex(index);
        _access = access;
    }

    public ByteArrayBuffer(byte[] bytes, int index, int length, int access, boolean isVolatile)
    {
        super(READWRITE, isVolatile);
        _bytes = bytes;
        setPutIndex(index + length);
        setGetIndex(index);
        _access = access;
    }

    public ByteArrayBuffer(int size)
    {
        this(new byte[size], 0, 0, READWRITE);
        setPutIndex(0);
    }

    public ByteArrayBuffer(String value)
    {
        super(READWRITE,NON_VOLATILE);
        _bytes = StringUtil.getBytes(value);
        setGetIndex(0);
        setPutIndex(_bytes.length);
        _access=IMMUTABLE;
        _string = value;
    }
    
    public ByteArrayBuffer(String value,boolean immutable)
    {
        super(READWRITE,NON_VOLATILE);
        _bytes = StringUtil.getBytes(value);
        setGetIndex(0);
        setPutIndex(_bytes.length);
        if (immutable)
        {
            _access=IMMUTABLE;
            _string = value;
        }
    }

    public ByteArrayBuffer(String value,String encoding) throws UnsupportedEncodingException
    {
        super(READWRITE,NON_VOLATILE);
        _bytes = value.getBytes(encoding);
        setGetIndex(0);
        setPutIndex(_bytes.length);
        _access=IMMUTABLE;
        _string = value;
    }

    public byte[] array()
    {
        return _bytes;
    }

    public int capacity()
    {
        return _bytes.length;
    }
    
    @Override
    public void compact()
    {
        if (isReadOnly()) 
            throw new IllegalStateException(__READONLY);
        int s = markIndex() >= 0 ? markIndex() : getIndex();
        if (s > 0)
        {
            int length = putIndex() - s;
            if (length > 0)
            {
                System.arraycopy(_bytes, s,_bytes, 0, length);
            }
            if (markIndex() > 0) setMarkIndex(markIndex() - s);
            setGetIndex(getIndex() - s);
            setPutIndex(putIndex() - s);
        }
    }


    @Override
    public boolean equals(Object obj)
    {
        if (obj==this)
            return true;

        if (obj == null || !(obj instanceof Buffer)) 
            return false;
        
        if (obj instanceof Buffer.CaseInsensitve)
            return equalsIgnoreCase((Buffer)obj);
        

        Buffer b = (Buffer) obj;
        
        // reject different lengths
        if (b.length() != length()) 
            return false;

        // reject AbstractBuffer with different hash value
        if (_hash != 0 && obj instanceof AbstractBuffer)
        {
            AbstractBuffer ab = (AbstractBuffer) obj;
            if (ab._hash != 0 && _hash != ab._hash) 
                return false;
        }

        // Nothing for it but to do the hard grind.
        int get=getIndex();
        int bi=b.putIndex();
        for (int i = putIndex(); i-->get;)
        {
            byte b1 = _bytes[i];
            byte b2 = b.peek(--bi);
            if (b1 != b2) return false;
        }
        return true;
    }


    @Override
    public boolean equalsIgnoreCase(Buffer b)
    {
        if (b==this)
            return true;
        
        // reject different lengths
        if (b==null || b.length() != length()) 
            return false;

        // reject AbstractBuffer with different hash value
        if (_hash != 0 && b instanceof AbstractBuffer)
        {
            AbstractBuffer ab = (AbstractBuffer) b;
            if (ab._hash != 0 && _hash != ab._hash) return false;
        }

        // Nothing for it but to do the hard grind.
        int get=getIndex();
        int bi=b.putIndex();
        byte[] barray=b.array();
        if (barray==null)
        {
            for (int i = putIndex(); i-->get;)
            {
                byte b1 = _bytes[i];
                byte b2 = b.peek(--bi);
                if (b1 != b2)
                {
                    if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A');
                    if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A');
                    if (b1 != b2) return false;
                }
            }
        }
        else
        {
            for (int i = putIndex(); i-->get;)
            {
                byte b1 = _bytes[i];
                byte b2 = barray[--bi];
                if (b1 != b2)
                {
                    if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A');
                    if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A');
                    if (b1 != b2) return false;
                }
            }
        }
        return true;
    }

    @Override
    public byte get()
    {
        return _bytes[_get++];
    }

    @Override
    public int hashCode()
    {
        if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) 
        {
            int get=getIndex();
            for (int i = putIndex(); i-- >get;)
            {
                byte b = _bytes[i];
                if ('a' <= b && b <= 'z') 
                    b = (byte) (b - 'a' + 'A');
                _hash = 31 * _hash + b;
            }
            if (_hash == 0) 
                _hash = -1;
            _hashGet=_get;
            _hashPut=_put;
        }
        return _hash;
    }
    
    
    public byte peek(int index)
    {
        return _bytes[index];
    }
    
    public int peek(int index, byte[] b, int offset, int length)
    {
        int l = length;
        if (index + l > capacity())
        {
            l = capacity() - index;
            if (l==0)
                return -1;
        }
        
        if (l < 0) 
            return -1;
        
        System.arraycopy(_bytes, index, b, offset, l);
        return l;
    }

    public void poke(int index, byte b)
    {
        /* 
        if (isReadOnly()) 
            throw new IllegalStateException(__READONLY);
        
        if (index < 0) 
            throw new IllegalArgumentException("index<0: " + index + "<0");
        if (index > capacity())
                throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity());
        */
        _bytes[index] = b;
    }
    
    @Override
    public int poke(int index, Buffer src)
    {
        _hash=0;
        
        /* 
        if (isReadOnly()) 
            throw new IllegalStateException(__READONLY);
        if (index < 0) 
            throw new IllegalArgumentException("index<0: " + index + "<0");
        */
        
        int length=src.length();
        if (index + length > capacity())
        {
            length=capacity()-index;
            /*
            if (length<0)
                throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity());
            */
        }
        
        byte[] src_array = src.array();
        if (src_array != null)
            System.arraycopy(src_array, src.getIndex(), _bytes, index, length);
        else 
        {
            int s=src.getIndex();
            for (int i=0;i<length;i++)
                _bytes[index++]=src.peek(s++);
        }
        
        return length;
    }
    

    @Override
    public int poke(int index, byte[] b, int offset, int length)
    {
        _hash=0;
        /*
        if (isReadOnly()) 
            throw new IllegalStateException(__READONLY);
        if (index < 0) 
            throw new IllegalArgumentException("index<0: " + index + "<0");
        */
        
        if (index + length > capacity())
        {
            length=capacity()-index;
            /* if (length<0)
                throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity());
            */
        }
        
        System.arraycopy(b, offset, _bytes, index, length);
        
        return length;
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public void writeTo(OutputStream out)
        throws IOException
    {
        int len=length();
        if (MAX_WRITE>0 && len>MAX_WRITE)
        {
            int off=getIndex();
            while(len>0)
            {
                int c=len>MAX_WRITE?MAX_WRITE:len;
                out.write(_bytes,off,c);
                off+=c;
                len-=c;
            }
        }
        else
            out.write(_bytes,getIndex(),len);
        if (!isImmutable())
            clear();
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public int readFrom(InputStream in,int max) throws IOException
    {
        if (max<0||max>space())
            max=space();
        int p = putIndex();
        
        int len=0, total=0, available=max;
        while (total<max) 
        {
            len=in.read(_bytes,p,available);
            if (len<0)
                break;
            else if (len>0)
            {
                p += len;
                total += len;
                available -= len;
                setPutIndex(p);
            }
            if (in.available()<=0)
                break;
        }
        if (len<0 && total==0)
            return -1;
        return total;
    }

    /* ------------------------------------------------------------ */
    @Override
    public int space()
    {
        return _bytes.length - _put;
    }

    
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    public static class CaseInsensitive extends ByteArrayBuffer implements Buffer.CaseInsensitve
    {
        public CaseInsensitive(String s)
        {
            super(s);
        }
        
        public CaseInsensitive(byte[] b, int o, int l, int rw)
        {
            super(b,o,l,rw);
        }

        @Override
        public boolean equals(Object obj)
        {
            return obj instanceof Buffer && equalsIgnoreCase((Buffer)obj);
        }
        
    }
}

Back to the top