File: XByteBuffer.java

package info (click to toggle)
tomcat10 10.1.52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,900 kB
  • sloc: java: 375,756; xml: 59,410; jsp: 4,741; sh: 1,381; perl: 324; makefile: 25; ansic: 14
file content (617 lines) | stat: -rw-r--r-- 20,094 bytes parent folder | download | duplicates (5)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.catalina.tribes.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.catalina.tribes.util.StringManager;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

/**
 * The XByteBuffer provides a dual functionality. One, it stores message bytes and automatically extends the byte buffer
 * if needed.<BR>
 * Two, it can encode and decode packages so that they can be defined and identified as they come in on a socket. <br>
 * <b>THIS CLASS IS NOT THREAD SAFE</B><BR>
 * <br>
 * Transfer package:
 * <ul>
 * <li><b>START_DATA</b>- 7 bytes - <i>FLT2002</i></li>
 * <li><b>SIZE</b> - 4 bytes - size of the data package</li>
 * <li><b>DATA</b> - should be as many bytes as the prev SIZE</li>
 * <li><b>END_DATA</b> - 7 bytes - <i>TLF2003</i></li>
 * </ul>
 */
public class XByteBuffer implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final Log log = LogFactory.getLog(XByteBuffer.class);
    protected static final StringManager sm = StringManager.getManager(XByteBuffer.class);

    /**
     * This is a package header, 7 bytes (FLT2002)
     */
    private static final byte[] START_DATA = { 70, 76, 84, 50, 48, 48, 50 };

    /**
     * This is the package footer, 7 bytes (TLF2003)
     */
    private static final byte[] END_DATA = { 84, 76, 70, 50, 48, 48, 51 };

    /**
     * Variable to hold the data
     */
    protected byte[] buf;

    /**
     * Current length of data in the buffer
     */
    protected int bufSize = 0;

    /**
     * Flag for discarding invalid packages If this flag is set to true, and append(byte[],...) is called, the data
     * added will be inspected, and if it doesn't start with <code>START_DATA</code> it will be thrown away.
     */
    protected boolean discard;

    /**
     * Constructs a new XByteBuffer.<br>
     * TODO use a pool of byte[] for performance
     *
     * @param size    the initial size of the byte buffer
     * @param discard Flag for discarding invalid packages
     */
    public XByteBuffer(int size, boolean discard) {
        buf = new byte[size];
        this.discard = discard;
    }

    public XByteBuffer(byte[] data, boolean discard) {
        this(data, data.length + 128, discard);
    }

    public XByteBuffer(byte[] data, int size, boolean discard) {
        int length = Math.max(data.length, size);
        buf = new byte[length];
        System.arraycopy(data, 0, buf, 0, data.length);
        bufSize = data.length;
        this.discard = discard;
    }

    public int getLength() {
        return bufSize;
    }

    public void setLength(int size) {
        if (size > buf.length) {
            throw new ArrayIndexOutOfBoundsException(sm.getString("xByteBuffer.size.larger.buffer"));
        }
        bufSize = size;
    }

    public void trim(int length) {
        if ((bufSize - length) < 0) {
            throw new ArrayIndexOutOfBoundsException(
                    sm.getString("xByteBuffer.unableTrim", Integer.toString(bufSize), Integer.toString(length)));
        }
        bufSize -= length;
    }

    public void reset() {
        bufSize = 0;
    }

    public byte[] getBytesDirect() {
        return this.buf;
    }

    /**
     * @return the bytes in the buffer, in its exact length
     */
    public byte[] getBytes() {
        byte[] b = new byte[bufSize];
        System.arraycopy(buf, 0, b, 0, bufSize);
        return b;
    }

    /**
     * Resets the buffer
     */
    public void clear() {
        bufSize = 0;
    }

    /**
     * Appends the data to the buffer. If the data is incorrectly formatted, ie, the data should always start with the
     * header, false will be returned and the data will be discarded.
     *
     * @param b   - bytes to be appended
     * @param len - the number of bytes to append.
     *
     * @return true if the data was appended correctly. Returns false if the package is incorrect, ie missing header or
     *             something, or the length of data is 0
     */
    public boolean append(ByteBuffer b, int len) {
        int newcount = bufSize + len;
        if (newcount > buf.length) {
            expand(newcount);
        }
        b.get(buf, bufSize, len);

        bufSize = newcount;

        if (discard) {
            if (bufSize > START_DATA.length && (firstIndexOf(buf, 0, START_DATA) == -1)) {
                bufSize = 0;
                log.error(sm.getString("xByteBuffer.discarded.invalidHeader"));
                return false;
            }
        }
        return true;

    }

    public boolean append(byte i) {
        int newcount = bufSize + 1;
        if (newcount > buf.length) {
            expand(newcount);
        }
        buf[bufSize] = i;
        bufSize = newcount;
        return true;
    }


    public boolean append(boolean i) {
        int newcount = bufSize + 1;
        if (newcount > buf.length) {
            expand(newcount);
        }
        toBytes(i, buf, bufSize);
        bufSize = newcount;
        return true;
    }

    public boolean append(long i) {
        int newcount = bufSize + 8;
        if (newcount > buf.length) {
            expand(newcount);
        }
        toBytes(i, buf, bufSize);
        bufSize = newcount;
        return true;
    }

    public boolean append(int i) {
        int newcount = bufSize + 4;
        if (newcount > buf.length) {
            expand(newcount);
        }
        toBytes(i, buf, bufSize);
        bufSize = newcount;
        return true;
    }

    public boolean append(byte[] b, int off, int len) {
        if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return false;
        }

        int newcount = bufSize + len;
        if (newcount > buf.length) {
            expand(newcount);
        }
        System.arraycopy(b, off, buf, bufSize, len);
        bufSize = newcount;

        if (discard) {
            if (bufSize > START_DATA.length && (firstIndexOf(buf, 0, START_DATA) == -1)) {
                bufSize = 0;
                log.error(sm.getString("xByteBuffer.discarded.invalidHeader"));
                return false;
            }
        }
        return true;
    }

    public void expand(int newcount) {
        // don't change the allocation strategy
        byte[] newbuf = new byte[Math.max(buf.length << 1, newcount)];
        System.arraycopy(buf, 0, newbuf, 0, bufSize);
        buf = newbuf;
    }

    public int getCapacity() {
        return buf.length;
    }


    /**
     * Internal mechanism to make a check if a complete package exists within the buffer
     *
     * @return - true if a complete package (header,compress,size,data,footer) exists within the buffer
     */
    public int countPackages() {
        return countPackages(false);
    }

    public int countPackages(boolean first) {
        int cnt = 0;
        int pos = START_DATA.length;
        int start = 0;

        while (start < bufSize) {
            // first check start header
            int index = firstIndexOf(buf, start, START_DATA);
            // if the header (START_DATA) isn't the first thing or
            // the buffer isn't even 14 bytes
            if (index != start || ((bufSize - start) < 14)) {
                break;
            }
            // next 4 bytes are compress flag not needed for count packages
            // then get the size 4 bytes
            int size = toInt(buf, pos);
            // now the total buffer has to be long enough to hold
            // START_DATA.length+4+size+END_DATA.length
            pos = start + START_DATA.length + 4 + size;
            if ((pos + END_DATA.length) > bufSize) {
                break;
            }
            // and finally check the footer of the package END_DATA
            int newpos = firstIndexOf(buf, pos, END_DATA);
            // mismatch, there is no package
            if (newpos != pos) {
                break;
            }
            // increase the packet count
            cnt++;
            // reset the values
            start = pos + END_DATA.length;
            pos = start + START_DATA.length;
            // we only want to verify that we have at least one package
            if (first) {
                break;
            }
        }
        return cnt;
    }

    /**
     * Method to check if a package exists in this byte buffer.
     *
     * @return - true if a complete package (header,options,size,data,footer) exists within the buffer
     */
    public boolean doesPackageExist() {
        return (countPackages(true) > 0);
    }

    /**
     * Extracts the message bytes from a package. If no package exists, a IllegalStateException will be thrown.
     *
     * @param clearFromBuffer - if true, the package will be removed from the byte buffer
     *
     * @return - returns the actual message bytes (header, compress,size and footer not included).
     */
    public XByteBuffer extractDataPackage(boolean clearFromBuffer) {
        int psize = countPackages(true);
        if (psize == 0) {
            throw new IllegalStateException(sm.getString("xByteBuffer.no.package"));
        }
        int size = toInt(buf, START_DATA.length);
        XByteBuffer xbuf = BufferPool.getBufferPool().getBuffer(size, false);
        xbuf.setLength(size);
        System.arraycopy(buf, START_DATA.length + 4, xbuf.getBytesDirect(), 0, size);
        if (clearFromBuffer) {
            int totalsize = START_DATA.length + 4 + size + END_DATA.length;
            bufSize = bufSize - totalsize;
            System.arraycopy(buf, totalsize, buf, 0, bufSize);
        }
        return xbuf;

    }

    public ChannelData extractPackage(boolean clearFromBuffer) {
        XByteBuffer xbuf = extractDataPackage(clearFromBuffer);
        return ChannelData.getDataFromPackage(xbuf);
    }

    /**
     * Creates a complete data package
     *
     * @param cdata - the message data to be contained within the package
     *
     * @return - a full package (header,size,data,footer)
     */
    public static byte[] createDataPackage(ChannelData cdata) {
        // return createDataPackage(cdata.getDataPackage());
        // avoid one extra byte array creation
        int dlength = cdata.getDataPackageLength();
        int length = getDataPackageLength(dlength);
        byte[] data = new byte[length];
        int offset = 0;
        System.arraycopy(START_DATA, 0, data, offset, START_DATA.length);
        offset += START_DATA.length;
        toBytes(dlength, data, START_DATA.length);
        offset += 4;
        cdata.getDataPackage(data, offset);
        offset += dlength;
        System.arraycopy(END_DATA, 0, data, offset, END_DATA.length);
        offset += END_DATA.length;
        return data;
    }

    public static byte[] createDataPackage(byte[] data, int doff, int dlength, byte[] buffer, int bufoff) {
        if ((buffer.length - bufoff) > getDataPackageLength(dlength)) {
            throw new ArrayIndexOutOfBoundsException(sm.getString("xByteBuffer.unableCreate"));
        }
        System.arraycopy(START_DATA, 0, buffer, bufoff, START_DATA.length);
        toBytes(data.length, buffer, bufoff + START_DATA.length);
        System.arraycopy(data, doff, buffer, bufoff + START_DATA.length + 4, dlength);
        System.arraycopy(END_DATA, 0, buffer, bufoff + START_DATA.length + 4 + data.length, END_DATA.length);
        return buffer;
    }


    public static int getDataPackageLength(int datalength) {
        return START_DATA.length + // header length
                4 + // data length indicator
                datalength + // actual data length
                END_DATA.length; // footer length
    }

    public static byte[] createDataPackage(byte[] data) {
        int length = getDataPackageLength(data.length);
        byte[] result = new byte[length];
        return createDataPackage(data, 0, data.length, result, 0);
    }


    /**
     * Convert four bytes to an int
     *
     * @param b   - the byte array containing the four bytes
     * @param off - the offset
     *
     * @return the integer value constructed from the four bytes
     */
    public static int toInt(byte[] b, int off) {
        return ((b[off + 3]) & 0xFF) + (((b[off + 2]) & 0xFF) << 8) + (((b[off + 1]) & 0xFF) << 16) +
                (((b[off]) & 0xFF) << 24);
    }

    /**
     * Convert eight bytes to a long
     *
     * @param b   - the byte array containing the four bytes
     * @param off - the offset
     *
     * @return the long value constructed from the eight bytes
     */
    public static long toLong(byte[] b, int off) {
        return (((long) b[off + 7]) & 0xFF) + ((((long) b[off + 6]) & 0xFF) << 8) +
                ((((long) b[off + 5]) & 0xFF) << 16) + ((((long) b[off + 4]) & 0xFF) << 24) +
                ((((long) b[off + 3]) & 0xFF) << 32) + ((((long) b[off + 2]) & 0xFF) << 40) +
                ((((long) b[off + 1]) & 0xFF) << 48) + ((((long) b[off]) & 0xFF) << 56);
    }


    /**
     * Converts a boolean and put it in a byte array.
     *
     * @param bool   the integer
     * @param data   the byte buffer in which the boolean will be placed
     * @param offset the offset in the byte array
     *
     * @return the byte array
     */
    public static byte[] toBytes(boolean bool, byte[] data, int offset) {
        data[offset] = (byte) (bool ? 1 : 0);
        return data;
    }

    /**
     * Converts a byte array entry to boolean.
     *
     * @param b      byte array
     * @param offset within byte array
     *
     * @return true if byte array entry is non-zero, false otherwise
     */
    public static boolean toBoolean(byte[] b, int offset) {
        return b[offset] != 0;
    }


    /**
     * Converts an integer to four bytes.
     *
     * @param n      the integer
     * @param b      the byte buffer in which the integer will be placed
     * @param offset the offset in the byte array
     *
     * @return four bytes in an array
     */
    public static byte[] toBytes(int n, byte[] b, int offset) {
        b[offset + 3] = (byte) (n);
        n >>>= 8;
        b[offset + 2] = (byte) (n);
        n >>>= 8;
        b[offset + 1] = (byte) (n);
        n >>>= 8;
        b[offset] = (byte) (n);
        return b;
    }

    /**
     * Converts a long to eight bytes.
     *
     * @param n      the long
     * @param b      the byte buffer in which the integer will be placed
     * @param offset the offset in the byte array
     *
     * @return eight bytes in an array
     */
    public static byte[] toBytes(long n, byte[] b, int offset) {
        b[offset + 7] = (byte) (n);
        n >>>= 8;
        b[offset + 6] = (byte) (n);
        n >>>= 8;
        b[offset + 5] = (byte) (n);
        n >>>= 8;
        b[offset + 4] = (byte) (n);
        n >>>= 8;
        b[offset + 3] = (byte) (n);
        n >>>= 8;
        b[offset + 2] = (byte) (n);
        n >>>= 8;
        b[offset + 1] = (byte) (n);
        n >>>= 8;
        b[offset] = (byte) (n);
        return b;
    }

    /**
     * Similar to a String.IndexOf, but uses pure bytes.
     *
     * @param src    - the source bytes to be searched
     * @param srcOff - offset on the source buffer
     * @param find   - the string to be found within src
     *
     * @return - the index of the first matching byte. -1 if the find array is not found
     */
    public static int firstIndexOf(byte[] src, int srcOff, byte[] find) {
        int result = -1;
        if (find.length > src.length) {
            return result;
        }
        if (find.length == 0) {
            return result;
        }
        if (srcOff >= src.length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        boolean found = false;
        int srclen = src.length;
        int findlen = find.length;
        byte first = find[0];
        int pos = srcOff;
        while (!found) {
            // find the first byte
            while (pos < srclen) {
                if (first == src[pos]) {
                    break;
                }
                pos++;
            }
            if (pos >= srclen) {
                return -1;
            }

            // we found the first character
            // match the rest of the bytes - they have to match
            if ((srclen - pos) < findlen) {
                return -1;
            }
            // assume it does exist
            found = true;
            for (int i = 1; ((i < findlen) && found); i++) {
                found = (find[i] == src[pos + i]);
            }
            if (found) {
                result = pos;
            } else if ((srclen - pos) < findlen) {
                return -1; // no more matches possible
            } else {
                pos++;
            }
        }
        return result;
    }


    public static Serializable deserialize(byte[] data) throws IOException, ClassNotFoundException, ClassCastException {
        return deserialize(data, 0, data.length);
    }

    public static Serializable deserialize(byte[] data, int offset, int length)
            throws IOException, ClassNotFoundException, ClassCastException {
        return deserialize(data, offset, length, null);
    }

    private static final AtomicInteger invokecount = new AtomicInteger(0);

    public static Serializable deserialize(byte[] data, int offset, int length, ClassLoader[] cls)
            throws IOException, ClassNotFoundException, ClassCastException {
        invokecount.addAndGet(1);
        Object message = null;
        if (cls == null) {
            cls = new ClassLoader[0];
        }
        if (data != null && length > 0) {
            InputStream instream = new ByteArrayInputStream(data, offset, length);
            ObjectInputStream stream;
            stream = (cls.length > 0) ? new ReplicationStream(instream, cls) : new ObjectInputStream(instream);
            message = stream.readObject();
            instream.close();
            stream.close();
        }
        if (message == null) {
            return null;
        } else if (message instanceof Serializable) {
            return (Serializable) message;
        } else {
            throw new ClassCastException(sm.getString("xByteBuffer.wrong.class", message.getClass().getName()));
        }
    }

    /**
     * Serializes a message into cluster data
     *
     * @param msg ClusterMessage
     *
     * @return serialized content as byte[] array
     *
     * @throws IOException Serialization error
     */
    public static byte[] serialize(Serializable msg) throws IOException {
        ByteArrayOutputStream outs = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(outs);
        out.writeObject(msg);
        out.flush();
        return outs.toByteArray();
    }

    public void setDiscard(boolean discard) {
        this.discard = discard;
    }

    public boolean getDiscard() {
        return discard;
    }

}