snf4j / snf4j

@@ -28,7 +28,6 @@
Loading
28 28
import java.nio.BufferUnderflowException;
29 29
import java.nio.ByteBuffer;
30 30
import java.nio.InvalidMarkException;
31 -
import java.util.Collection;
32 31
33 32
/**
34 33
 * A byte buffer array wrapper providing absolute and relative get methods that
@@ -38,30 +37,19 @@
Loading
38 37
 */
39 38
public class ByteBufferArray {
40 39
41 -
	private final ByteBuffer[] array;
40 +
	final ByteBuffer[] array;
42 41
43 -
	private final int off;
42 +
	final int offset;
44 43
	
45 -
	private final int size;
44 +
	final int size;
46 45
	
47 -
	private final int end;
46 +
	final int end;
48 47
	
49 48
	private int index;
50 49
51 50
	private long mark = -1;
52 51
53 -
	private ByteBuffer buffer;
54 -
55 -
	/**
56 -
	 * Constructs a byte buffer array wrapper that will be backed by an array
57 -
	 * containing {@link ByteBuffer} objects from the given collection, in the order
58 -
	 * they are returned by the collection's iterator.
59 -
	 * 
60 -
	 * @param collection the collection with {@link ByteBuffer} objects
61 -
	 */
62 -
	public ByteBufferArray(Collection<ByteBuffer> collection) {
63 -
		this(collection.toArray(new ByteBuffer[collection.size()]));
64 -
	}
52 +
	ByteBuffer buffer;
65 53
66 54
	/**
67 55
	 * Constructs a byte buffer array wrapper that will be backed by the given
@@ -69,12 +57,12 @@
Loading
69 57
	 * 
70 58
	 * @param array The array that will back this buffer array wrapper
71 59
	 */
72 -
	public ByteBufferArray(ByteBuffer[] array) {
60 +
	ByteBufferArray(ByteBuffer[] array) {
73 61
		this.array = array;
74 62
		size = end = array.length; 
75 -
		off = 0;
63 +
		offset = 0;
76 64
		if (size > 0) {
77 -
			buffer = this.array[off];
65 +
			buffer = this.array[offset];
78 66
		}
79 67
	}
80 68
@@ -82,22 +70,52 @@
Loading
82 70
	 * Constructs a byte buffer array wrapper that will be backed by the given
83 71
	 * {@link ByteBuffer} array.
84 72
	 * 
85 -
	 * @param array The array that will back this buffer array wrapper
86 -
	 * @param off   The offset of the subarray to be used
87 -
	 * @param len   The length of the subarray to be used
73 +
	 * @param array  The array that will back this buffer array wrapper
74 +
	 * @param offset The offset of the subarray to be used
75 +
	 * @param length The length of the subarray to be used
88 76
	 * @throws IndexOutOfBoundsException If the preconditions on the offset and
89 77
	 *                                   length parameters do not hold
90 78
	 */
91 -
	public ByteBufferArray(ByteBuffer[] array, int off, int len) {
92 -
		InternalSession.checkBounds(off, len, array.length);
79 +
	ByteBufferArray(ByteBuffer[] array, int offset, int length) {
80 +
		InternalSession.checkBounds(offset, length, array.length);
93 81
		this.array = array;
94 -
		if (len > 0) {
95 -
			buffer = this.array[off];
82 +
		if (length > 0) {
83 +
			buffer = this.array[offset];
96 84
		}
97 -
		this.off = off;
98 -
		this.size = len;
99 -
		end = off + len;
100 -
		index = off;
85 +
		this.offset = offset;
86 +
		this.size = length;
87 +
		end = offset + length;
88 +
		index = offset;
89 +
	}
90 +
	
91 +
	/**
92 +
	 * Wraps an array of byte buffers into a byte buffer array wrapper that will be
93 +
	 * backed by the given array.
94 +
	 * 
95 +
	 * @param array The array that will back the returned buffer array wrapper
96 +
	 * @return The new byte buffer array wrapper
97 +
	 */
98 +
	public static ByteBufferArray wrap(ByteBuffer[] array) {
99 +
		return array.length == 1 
100 +
				? new OneByteBufferArray(array) 
101 +
				: new ByteBufferArray(array);
102 +
	}
103 +
104 +
	/**
105 +
	 * Wraps an array of byte buffers into a byte buffer array wrapper that will be
106 +
	 * backed by the given array.
107 +
	 * 
108 +
	 * @param array  The array that will back the returned buffer array wrapper
109 +
	 * @param offset The offset of the subarray to be used
110 +
	 * @param length The length of the subarray to be used
111 +
	 * @return The new byte buffer array wrapper
112 +
	 * @throws IndexOutOfBoundsException If the preconditions on the offset and
113 +
	 *                                   length parameters do not hold
114 +
	 */
115 +
	public static ByteBufferArray wrap(ByteBuffer[] array, int offset, int length) {
116 +
		return length == 1 
117 +
				? new OneByteBufferArray(array, offset, length) 
118 +
				: new ByteBufferArray(array, offset, length);
101 119
	}
102 120
	
103 121
	/**
@@ -117,7 +135,7 @@
Loading
117 135
	 *         buffer
118 136
	 */
119 137
	public int arrayOffset() {
120 -
		return off;
138 +
		return offset;
121 139
	}
122 140
	
123 141
	/**
@@ -195,7 +213,7 @@
Loading
195 213
	public long limit() {
196 214
		long limit = 0;
197 215
198 -
		for (int i = off; i < end; ++i) {
216 +
		for (int i = offset; i < end; ++i) {
199 217
			limit += array[i].limit();
200 218
		}
201 219
		return limit;
@@ -210,7 +228,7 @@
Loading
210 228
		if (buffer != null) {
211 229
			long position = 0;
212 230
213 -
			for (int i=off; i < index; ++i) {
231 +
			for (int i=offset; i < index; ++i) {
214 232
				position += array[i].limit();
215 233
			}
216 234
			return buffer.position() + position;
@@ -234,7 +252,7 @@
Loading
234 252
235 253
	private ByteBufferArray position(long newPosition, boolean reset) {
236 254
		if (newPosition >= 0) {
237 -
			int limit = -1, i = off, len = end;
255 +
			int limit = -1, i = offset, len = end;
238 256
			long position = newPosition;
239 257
			ByteBuffer buffer = null;
240 258
			boolean positioned = false;
@@ -264,7 +282,7 @@
Loading
264 282
				positioned = true;
265 283
			}
266 284
			if (positioned) {
267 -
				int j = off;
285 +
				int j = offset;
268 286
				for (; j<i; ++j) {
269 287
					array[j].position(array[j].limit());
270 288
				}
@@ -311,13 +329,13 @@
Loading
311 329
	 * @return This buffer array wrapper
312 330
	 */
313 331
	public ByteBufferArray rewind() {
314 -
		for (int i = off; i < end; ++i) {
332 +
		for (int i = offset; i < end; ++i) {
315 333
			array[i].rewind();
316 334
		}
317 335
		if (size > 0) {
318 -
			buffer = this.array[off];
336 +
			buffer = this.array[offset];
319 337
		}
320 -
		index = off;
338 +
		index = offset;
321 339
		mark = -1;
322 340
		return this;
323 341
	}
@@ -336,10 +354,10 @@
Loading
336 354
	public ByteBufferArray duplicate() {
337 355
		ByteBuffer[] dup = new ByteBuffer[array.length];
338 356
339 -
		for (int i = off; i < end; ++i) {
357 +
		for (int i = offset; i < end; ++i) {
340 358
			dup[i] = array[i].duplicate();
341 359
		}
342 -
		ByteBufferArray dupArray = new ByteBufferArray(dup, off, size);
360 +
		ByteBufferArray dupArray = new ByteBufferArray(dup, offset, size);
343 361
		dupArray.index = index;
344 362
		dupArray.mark = mark;
345 363
		if (buffer != null) {
@@ -379,7 +397,7 @@
Loading
379 397
		if (i < 0) {
380 398
			throw new IndexOutOfBoundsException();
381 399
		}
382 -
		for (int b = off; b < end; ++b) {
400 +
		for (int b = offset; b < end; ++b) {
383 401
			ByteBuffer buf = array[b];
384 402
			int l = buf.limit();
385 403
@@ -438,39 +456,39 @@
Loading
438 456
	 * into the given array and the position of this buffer array wrapper is
439 457
	 * incremented by number of copied bytes.
440 458
	 * 
441 -
	 * @param dst the destination byte array
442 -
	 * @param off the offset within the array of the first byte to be copied
443 -
	 * @param len the number of bytes to be copied into the given array
459 +
	 * @param dst    the destination byte array
460 +
	 * @param offset the offset within the array of the first byte to be copied
461 +
	 * @param length the number of bytes to be copied into the given array
444 462
	 * @return This buffer array wrapper
445 463
	 * @throws BufferUnderflowException  If there are fewer than length bytes
446 464
	 *                                   remaining in this buffer buffer wrapper
447 465
	 * @throws IndexOutOfBoundsException If the preconditions on the offset and
448 466
	 *                                   length parameters do not hold
449 467
	 */
450 -
	public ByteBufferArray get(byte[] dst, int off, int len) {
451 -
		InternalSession.checkBounds(off, len, dst.length);
452 -
		if (len == 0) {
468 +
	public ByteBufferArray get(byte[] dst, int offset, int length) {
469 +
		InternalSession.checkBounds(offset, length, dst.length);
470 +
		if (length == 0) {
453 471
			return this;
454 472
		}
455 473
456 474
		ByteBuffer buffer = buffer();
457 475
458 476
		int remaining = buffer.remaining();
459 -
		if (len <= remaining) {
460 -
			buffer.get(dst, off, len);
477 +
		if (length <= remaining) {
478 +
			buffer.get(dst, offset, length);
461 479
			return this;
462 480
		}
463 481
464 482
		for (int i = index + 1; i < end; ++i) {
465 483
			remaining += array[i].remaining();
466 -
			if (remaining >= len) {
484 +
			if (remaining >= length) {
467 485
				while (true) {
468 -
					int size = Math.min(len, buffer.remaining());
486 +
					int size = Math.min(length, buffer.remaining());
469 487
470 -
					buffer.get(dst, off, size);
471 -
					off += size;
472 -
					len -= size;
473 -
					if (len > 0) {
488 +
					buffer.get(dst, offset, size);
489 +
					offset += size;
490 +
					length -= size;
491 +
					if (length > 0) {
474 492
						buffer = buffer();
475 493
					} else {
476 494
						return this;
@@ -481,6 +499,59 @@
Loading
481 499
		throw new BufferUnderflowException();
482 500
	}
483 501
502 +
	static void checkBounds(ByteBuffer dst, int length) {
503 +
		if (length < 0 || dst.remaining() < length) {
504 +
			throw new IndexOutOfBoundsException();
505 +
		}
506 +
	}
507 +
	
508 +
	/**
509 +
	 * Relative get method that transfers bytes from this buffer array wrapper into
510 +
	 * the given destination byte buffer.
511 +
	 * <p>
512 +
	 * If there are fewer bytes remaining in the buffer than are required to satisfy
513 +
	 * the request then no bytes are transferred and a
514 +
	 * {@link BufferUnderflowException} is thrown. Otherwise, the bytes are copied
515 +
	 * into the given buffer and the position of this buffer array wrapper is
516 +
	 * incremented by number of copied bytes.
517 +
	 * 
518 +
	 * @param dst    the destination byte buffer
519 +
	 * @param length the number of bytes to be copied into the given buffer
520 +
	 * @return This buffer array wrapper
521 +
	 * @throws BufferUnderflowException  If there are fewer than length bytes
522 +
	 *                                   remaining in this buffer buffer wrapper
523 +
	 * @throws IndexOutOfBoundsException If the preconditions on the length
524 +
	 *                                   parameter do not hold
525 +
	 */
526 +
	public ByteBufferArray get(ByteBuffer dst, int length) {
527 +
		checkBounds(dst, length);
528 +
		int i = index;
529 +
		
530 +
		for (; i < end; ++i) {
531 +
			length -= array[i].remaining();
532 +
			if (length == 0) {
533 +
				break;
534 +
			}
535 +
			else if (length < 0) {
536 +
				--i;
537 +
				break;
538 +
			}
539 +
		}
540 +
		if (length > 0) {
541 +
			throw new BufferUnderflowException();
542 +
		}
543 +
		for (int j = index; j<=i; ++j) {
544 +
			dst.put(array[j]);
545 +
		}
546 +
		if (length < 0) {
547 +
			ByteBuffer dup = array[++i].duplicate();
548 +
			dup.limit(dup.limit() + length);
549 +
			dst.put(dup);
550 +
			array[i].position(dup.limit());
551 +
		}
552 +
		return this;
553 +
	}
554 +
	
484 555
	/**
485 556
	 * Relative get method that reads the byte at this buffer array wrapper's
486 557
	 * current position, and then increments the position.
@@ -732,4 +803,170 @@
Loading
732 803
		return buffer(8, indexArray).getDouble(indexArray[0]);
733 804
	}
734 805
806 +
	static class OneByteBufferArray extends ByteBufferArray {
807 +
808 +
		public OneByteBufferArray(ByteBuffer[] array) {
809 +
			super(array);
810 +
		}
811 +
812 +
		public OneByteBufferArray(ByteBuffer[] array, int off, int len) {
813 +
			super(array, off, len);
814 +
		}
815 +
816 +
		@Override
817 +
		public int arrayIndex() {
818 +
			return offset;
819 +
		}
820 +
		
821 +
		public ByteBufferArray duplicate() {
822 +
			ByteBuffer[] dup = new ByteBuffer[array.length];
823 +
			dup[offset] = array[offset].duplicate();
824 +
			return new OneByteBufferArray(dup, offset, size);
825 +
		}
826 +
		
827 +
		@Override
828 +
		public boolean hasRemaining() {
829 +
			return buffer.hasRemaining();
830 +
		}
831 +
832 +
		@Override
833 +
		public long remaining() {
834 +
			return buffer.remaining();
835 +
		}
836 +
837 +
		@Override
838 +
		public long limit() {
839 +
			return buffer.limit();
840 +
		}
841 +
842 +
		@Override
843 +
		public long position() {
844 +
			return buffer.position();
845 +
		}
846 +
847 +
		@Override
848 +
		public ByteBufferArray position(long newPosition) {
849 +
			buffer.position((int) newPosition);
850 +
			return this;
851 +
		}
852 +
		
853 +
		@Override
854 +
		public ByteBufferArray mark() {
855 +
			buffer.mark();
856 +
			return this;
857 +
		}
858 +
859 +
		@Override
860 +
		public ByteBufferArray reset() {
861 +
			buffer.reset();
862 +
			return this;
863 +
		}
864 +
865 +
		@Override
866 +
		public ByteBufferArray rewind() {
867 +
			buffer.rewind();
868 +
			return this;
869 +
		}
870 +
871 +
		@Override
872 +
		public ByteBufferArray get(byte[] dst) {
873 +
			buffer.get(dst);
874 +
			return this;
875 +
		}
876 +
877 +
		@Override
878 +
		public ByteBufferArray get(byte[] dst, int off, int len) {
879 +
			buffer.get(dst, off, len);
880 +
			return this;
881 +
		}
882 +
883 +
		public ByteBufferArray get(ByteBuffer dst, int length) {
884 +
			checkBounds(dst, length);
885 +
			length -= buffer.remaining();
886 +
			if (length > 0) {
887 +
				throw new BufferUnderflowException();
888 +
			}
889 +
			if (length == 0) {
890 +
				dst.put(buffer);
891 +
			}
892 +
			else {
893 +
				ByteBuffer dup = buffer.duplicate();
894 +
				dup.limit(dup.limit() + length);
895 +
				dst.put(dup);
896 +
				buffer.position(dup.limit());
897 +
			}
898 +
			return this;
899 +
		}
900 +
		
901 +
		@Override
902 +
		public byte get() {
903 +
			return buffer.get();
904 +
		}
905 +
906 +
		@Override
907 +
		public byte get(int position) {
908 +
			return buffer.get(position);
909 +
		}
910 +
911 +
		@Override
912 +
		public char getChar() {
913 +
			return buffer.getChar();
914 +
		}
915 +
916 +
		@Override
917 +
		public char getChar(int position) {
918 +
			return buffer.getChar(position);
919 +
		}
920 +
921 +
		@Override
922 +
		public short getShort() {
923 +
			return buffer.getShort();
924 +
		}
925 +
926 +
		@Override
927 +
		public short getShort(int position) {
928 +
			return buffer.getShort(position);
929 +
		}
930 +
931 +
		@Override
932 +
		public int getInt() {
933 +
			return buffer.getInt();
934 +
		}
935 +
936 +
		@Override
937 +
		public int getInt(int position) {
938 +
			return buffer.getInt(position);
939 +
		}
940 +
941 +
		@Override
942 +
		public long getLong() {
943 +
			return buffer.getLong();
944 +
		}
945 +
946 +
		@Override
947 +
		public long getLong(int position) {
948 +
			return buffer.getLong(position);
949 +
		}
950 +
951 +
		@Override
952 +
		public float getFloat() {
953 +
			return buffer.getFloat();
954 +
		}
955 +
956 +
		@Override
957 +
		public float getFloat(int position) {
958 +
			return buffer.getFloat(position);
959 +
		}
960 +
961 +
		@Override
962 +
		public double getDouble() {
963 +
			return buffer.getDouble();
964 +
		}
965 +
966 +
		@Override
967 +
		public double getDouble(int position) {
968 +
			return buffer.getDouble(position);
969 +
		}
970 +
		
971 +
	}
735 972
}
Files Coverage
snf4j-core-log4j2/src/main/java/org/snf4j/core/logger/impl 96.66%
snf4j-core-slf4j/src/main/java/org/snf4j/core/logger/impl 96.66%
snf4j-core/src/main/java/org/snf4j/core 98.09%
snf4j-sctp/src/main/java/org/snf4j/core 98.58%
snf4j-websocket/src/main/java/org/snf4j/websocket 99.37%
Project Totals (242 files) 98.31%
codecov-umbrella
Build #2747864976 -
1
coverage:
2
  range: "70...100"
3
  round: down
4
  precision: 2
5
  
6
  status:
7
    project:
8
      default:
9
        enabled: yes
10
        target: 94%
11
        threshold: 5%
12
        
13
    patch:
14
      default:
15
        enabled: yes
16
        target: 85%
17
        threshold: 5%
18
        
19
    changes: no
20
    
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading