1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.buffer;
17
18 import io.netty.buffer.CompositeByteBuf.ByteWrapper;
19 import io.netty.util.internal.ObjectUtil;
20 import io.netty.util.CharsetUtil;
21 import io.netty.util.internal.PlatformDependent;
22
23 import java.nio.ByteBuffer;
24 import java.nio.ByteOrder;
25 import java.nio.CharBuffer;
26 import java.nio.charset.Charset;
27 import java.util.Arrays;
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 public final class Unpooled {
74
75 private static final ByteBufAllocator ALLOC = UnpooledByteBufAllocator.DEFAULT;
76
77
78
79
80 public static final ByteOrder BIG_ENDIAN = ByteOrder.BIG_ENDIAN;
81
82
83
84
85 public static final ByteOrder LITTLE_ENDIAN = ByteOrder.LITTLE_ENDIAN;
86
87
88
89
90 @SuppressWarnings("checkstyle:StaticFinalBuffer")
91 public static final ByteBuf EMPTY_BUFFER = ALLOC.buffer(0, 0);
92
93 static {
94 assert EMPTY_BUFFER instanceof EmptyByteBuf: "EMPTY_BUFFER must be an EmptyByteBuf.";
95 }
96
97
98
99
100
101 public static ByteBuf buffer() {
102 return ALLOC.heapBuffer();
103 }
104
105
106
107
108
109 public static ByteBuf directBuffer() {
110 return ALLOC.directBuffer();
111 }
112
113
114
115
116
117
118 public static ByteBuf buffer(int initialCapacity) {
119 return ALLOC.heapBuffer(initialCapacity);
120 }
121
122
123
124
125
126
127 public static ByteBuf directBuffer(int initialCapacity) {
128 return ALLOC.directBuffer(initialCapacity);
129 }
130
131
132
133
134
135
136
137 public static ByteBuf buffer(int initialCapacity, int maxCapacity) {
138 return ALLOC.heapBuffer(initialCapacity, maxCapacity);
139 }
140
141
142
143
144
145
146
147 public static ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
148 return ALLOC.directBuffer(initialCapacity, maxCapacity);
149 }
150
151
152
153
154
155
156 public static ByteBuf wrappedBuffer(byte[] array) {
157 if (array.length == 0) {
158 return EMPTY_BUFFER;
159 }
160 return new UnpooledHeapByteBuf(ALLOC, array, array.length);
161 }
162
163
164
165
166
167
168 public static ByteBuf wrappedBuffer(byte[] array, int offset, int length) {
169 if (length == 0) {
170 return EMPTY_BUFFER;
171 }
172
173 if (offset == 0 && length == array.length) {
174 return wrappedBuffer(array);
175 }
176
177 return wrappedBuffer(array).slice(offset, length);
178 }
179
180
181
182
183
184
185 public static ByteBuf wrappedBuffer(ByteBuffer buffer) {
186 if (!buffer.hasRemaining()) {
187 return EMPTY_BUFFER;
188 }
189 if (!buffer.isDirect() && buffer.hasArray()) {
190 return wrappedBuffer(
191 buffer.array(),
192 buffer.arrayOffset() + buffer.position(),
193 buffer.remaining()).order(buffer.order());
194 } else if (PlatformDependent.hasUnsafe()) {
195 if (buffer.isReadOnly()) {
196 if (buffer.isDirect()) {
197 return new ReadOnlyUnsafeDirectByteBuf(ALLOC, buffer);
198 } else {
199 return new ReadOnlyByteBufferBuf(ALLOC, buffer);
200 }
201 } else {
202 return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining());
203 }
204 } else {
205 if (buffer.isReadOnly()) {
206 return new ReadOnlyByteBufferBuf(ALLOC, buffer);
207 } else {
208 return new UnpooledDirectByteBuf(ALLOC, buffer, buffer.remaining());
209 }
210 }
211 }
212
213
214
215
216
217 public static ByteBuf wrappedBuffer(long memoryAddress, int size, boolean doFree) {
218 return new WrappedUnpooledUnsafeDirectByteBuf(ALLOC, memoryAddress, size, doFree);
219 }
220
221
222
223
224
225
226
227
228
229 public static ByteBuf wrappedBuffer(ByteBuf buffer) {
230 if (buffer.isReadable()) {
231 return buffer.slice();
232 } else {
233 buffer.release();
234 return EMPTY_BUFFER;
235 }
236 }
237
238
239
240
241
242
243 public static ByteBuf wrappedBuffer(byte[]... arrays) {
244 return wrappedBuffer(arrays.length, arrays);
245 }
246
247
248
249
250
251
252
253
254 public static ByteBuf wrappedBuffer(ByteBuf... buffers) {
255 return wrappedBuffer(buffers.length, buffers);
256 }
257
258
259
260
261
262
263 public static ByteBuf wrappedBuffer(ByteBuffer... buffers) {
264 return wrappedBuffer(buffers.length, buffers);
265 }
266
267 static <T> ByteBuf wrappedBuffer(int maxNumComponents, ByteWrapper<T> wrapper, T[] array) {
268 switch (array.length) {
269 case 0:
270 break;
271 case 1:
272 if (!wrapper.isEmpty(array[0])) {
273 return wrapper.wrap(array[0]);
274 }
275 break;
276 default:
277 for (int i = 0, len = array.length; i < len; i++) {
278 T bytes = array[i];
279 if (bytes == null) {
280 return EMPTY_BUFFER;
281 }
282 if (!wrapper.isEmpty(bytes)) {
283 return new CompositeByteBuf(ALLOC, false, maxNumComponents, wrapper, array, i);
284 }
285 }
286 }
287
288 return EMPTY_BUFFER;
289 }
290
291
292
293
294
295
296 public static ByteBuf wrappedBuffer(int maxNumComponents, byte[]... arrays) {
297 return wrappedBuffer(maxNumComponents, CompositeByteBuf.BYTE_ARRAY_WRAPPER, arrays);
298 }
299
300
301
302
303
304
305
306
307
308
309 public static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuf... buffers) {
310 switch (buffers.length) {
311 case 0:
312 break;
313 case 1:
314 ByteBuf buffer = buffers[0];
315 if (buffer.isReadable()) {
316 return wrappedBuffer(buffer.order(BIG_ENDIAN));
317 } else {
318 buffer.release();
319 }
320 break;
321 default:
322 for (int i = 0; i < buffers.length; i++) {
323 ByteBuf buf = buffers[i];
324 if (buf.isReadable()) {
325 return new CompositeByteBuf(ALLOC, false, maxNumComponents, buffers, i);
326 }
327 buf.release();
328 }
329 break;
330 }
331 return EMPTY_BUFFER;
332 }
333
334
335
336
337
338
339 public static ByteBuf wrappedBuffer(int maxNumComponents, ByteBuffer... buffers) {
340 return wrappedBuffer(maxNumComponents, CompositeByteBuf.BYTE_BUFFER_WRAPPER, buffers);
341 }
342
343
344
345
346 public static CompositeByteBuf compositeBuffer() {
347 return compositeBuffer(AbstractByteBufAllocator.DEFAULT_MAX_COMPONENTS);
348 }
349
350
351
352
353 public static CompositeByteBuf compositeBuffer(int maxNumComponents) {
354 return new CompositeByteBuf(ALLOC, false, maxNumComponents);
355 }
356
357
358
359
360
361
362 public static ByteBuf copiedBuffer(byte[] array) {
363 if (array.length == 0) {
364 return EMPTY_BUFFER;
365 }
366 return wrappedBuffer(array.clone());
367 }
368
369
370
371
372
373
374
375 public static ByteBuf copiedBuffer(byte[] array, int offset, int length) {
376 if (length == 0) {
377 return EMPTY_BUFFER;
378 }
379 byte[] copy = PlatformDependent.allocateUninitializedArray(length);
380 System.arraycopy(array, offset, copy, 0, length);
381 return wrappedBuffer(copy);
382 }
383
384
385
386
387
388
389
390 public static ByteBuf copiedBuffer(ByteBuffer buffer) {
391 int length = buffer.remaining();
392 if (length == 0) {
393 return EMPTY_BUFFER;
394 }
395 byte[] copy = PlatformDependent.allocateUninitializedArray(length);
396
397
398 ByteBuffer duplicate = buffer.duplicate();
399 duplicate.get(copy);
400 return wrappedBuffer(copy).order(duplicate.order());
401 }
402
403
404
405
406
407
408
409 public static ByteBuf copiedBuffer(ByteBuf buffer) {
410 int readable = buffer.readableBytes();
411 if (readable > 0) {
412 ByteBuf copy = buffer(readable);
413 copy.writeBytes(buffer, buffer.readerIndex(), readable);
414 return copy;
415 } else {
416 return EMPTY_BUFFER;
417 }
418 }
419
420
421
422
423
424
425
426 public static ByteBuf copiedBuffer(byte[]... arrays) {
427 switch (arrays.length) {
428 case 0:
429 return EMPTY_BUFFER;
430 case 1:
431 if (arrays[0].length == 0) {
432 return EMPTY_BUFFER;
433 } else {
434 return copiedBuffer(arrays[0]);
435 }
436 }
437
438
439 int length = 0;
440 for (byte[] a: arrays) {
441 if (Integer.MAX_VALUE - length < a.length) {
442 throw new IllegalArgumentException(
443 "The total length of the specified arrays is too big.");
444 }
445 length += a.length;
446 }
447
448 if (length == 0) {
449 return EMPTY_BUFFER;
450 }
451
452 byte[] mergedArray = PlatformDependent.allocateUninitializedArray(length);
453 for (int i = 0, j = 0; i < arrays.length; i ++) {
454 byte[] a = arrays[i];
455 System.arraycopy(a, 0, mergedArray, j, a.length);
456 j += a.length;
457 }
458
459 return wrappedBuffer(mergedArray);
460 }
461
462
463
464
465
466
467
468
469
470
471
472 public static ByteBuf copiedBuffer(ByteBuf... buffers) {
473 switch (buffers.length) {
474 case 0:
475 return EMPTY_BUFFER;
476 case 1:
477 return copiedBuffer(buffers[0]);
478 }
479
480
481 ByteOrder order = null;
482 int length = 0;
483 for (ByteBuf b: buffers) {
484 int bLen = b.readableBytes();
485 if (bLen <= 0) {
486 continue;
487 }
488 if (Integer.MAX_VALUE - length < bLen) {
489 throw new IllegalArgumentException(
490 "The total length of the specified buffers is too big.");
491 }
492 length += bLen;
493 if (order != null) {
494 if (!order.equals(b.order())) {
495 throw new IllegalArgumentException("inconsistent byte order");
496 }
497 } else {
498 order = b.order();
499 }
500 }
501
502 if (length == 0) {
503 return EMPTY_BUFFER;
504 }
505
506 byte[] mergedArray = PlatformDependent.allocateUninitializedArray(length);
507 for (int i = 0, j = 0; i < buffers.length; i ++) {
508 ByteBuf b = buffers[i];
509 int bLen = b.readableBytes();
510 b.getBytes(b.readerIndex(), mergedArray, j, bLen);
511 j += bLen;
512 }
513
514 return wrappedBuffer(mergedArray).order(order);
515 }
516
517
518
519
520
521
522
523
524
525
526
527 public static ByteBuf copiedBuffer(ByteBuffer... buffers) {
528 switch (buffers.length) {
529 case 0:
530 return EMPTY_BUFFER;
531 case 1:
532 return copiedBuffer(buffers[0]);
533 }
534
535
536 ByteOrder order = null;
537 int length = 0;
538 for (ByteBuffer b: buffers) {
539 int bLen = b.remaining();
540 if (bLen <= 0) {
541 continue;
542 }
543 if (Integer.MAX_VALUE - length < bLen) {
544 throw new IllegalArgumentException(
545 "The total length of the specified buffers is too big.");
546 }
547 length += bLen;
548 if (order != null) {
549 if (!order.equals(b.order())) {
550 throw new IllegalArgumentException("inconsistent byte order");
551 }
552 } else {
553 order = b.order();
554 }
555 }
556
557 if (length == 0) {
558 return EMPTY_BUFFER;
559 }
560
561 byte[] mergedArray = PlatformDependent.allocateUninitializedArray(length);
562 for (int i = 0, j = 0; i < buffers.length; i ++) {
563
564
565 ByteBuffer b = buffers[i].duplicate();
566 int bLen = b.remaining();
567 b.get(mergedArray, j, bLen);
568 j += bLen;
569 }
570
571 return wrappedBuffer(mergedArray).order(order);
572 }
573
574
575
576
577
578
579
580 public static ByteBuf copiedBuffer(CharSequence string, Charset charset) {
581 ObjectUtil.checkNotNull(string, "string");
582 if (CharsetUtil.UTF_8.equals(charset)) {
583 return copiedBufferUtf8(string);
584 }
585 if (CharsetUtil.US_ASCII.equals(charset)) {
586 return copiedBufferAscii(string);
587 }
588 if (string instanceof CharBuffer) {
589 return copiedBuffer((CharBuffer) string, charset);
590 }
591
592 return copiedBuffer(CharBuffer.wrap(string), charset);
593 }
594
595 private static ByteBuf copiedBufferUtf8(CharSequence string) {
596 boolean release = true;
597
598 ByteBuf buffer = ALLOC.heapBuffer(ByteBufUtil.utf8Bytes(string));
599 try {
600 ByteBufUtil.writeUtf8(buffer, string);
601 release = false;
602 return buffer;
603 } finally {
604 if (release) {
605 buffer.release();
606 }
607 }
608 }
609
610 private static ByteBuf copiedBufferAscii(CharSequence string) {
611 boolean release = true;
612
613 ByteBuf buffer = ALLOC.heapBuffer(string.length());
614 try {
615 ByteBufUtil.writeAscii(buffer, string);
616 release = false;
617 return buffer;
618 } finally {
619 if (release) {
620 buffer.release();
621 }
622 }
623 }
624
625
626
627
628
629
630
631 public static ByteBuf copiedBuffer(
632 CharSequence string, int offset, int length, Charset charset) {
633 ObjectUtil.checkNotNull(string, "string");
634 if (length == 0) {
635 return EMPTY_BUFFER;
636 }
637
638 if (string instanceof CharBuffer) {
639 CharBuffer buf = (CharBuffer) string;
640 if (buf.hasArray()) {
641 return copiedBuffer(
642 buf.array(),
643 buf.arrayOffset() + buf.position() + offset,
644 length, charset);
645 }
646
647 buf = buf.slice();
648 buf.limit(length);
649 buf.position(offset);
650 return copiedBuffer(buf, charset);
651 }
652
653 return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
654 }
655
656
657
658
659
660
661
662 public static ByteBuf copiedBuffer(char[] array, Charset charset) {
663 ObjectUtil.checkNotNull(array, "array");
664 return copiedBuffer(array, 0, array.length, charset);
665 }
666
667
668
669
670
671
672
673 public static ByteBuf copiedBuffer(char[] array, int offset, int length, Charset charset) {
674 ObjectUtil.checkNotNull(array, "array");
675 if (length == 0) {
676 return EMPTY_BUFFER;
677 }
678 return copiedBuffer(CharBuffer.wrap(array, offset, length), charset);
679 }
680
681 private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset) {
682 return ByteBufUtil.encodeString0(ALLOC, true, buffer, charset, 0);
683 }
684
685
686
687
688
689
690
691
692
693 @Deprecated
694 public static ByteBuf unmodifiableBuffer(ByteBuf buffer) {
695 ByteOrder endianness = buffer.order();
696 if (endianness == BIG_ENDIAN) {
697 return newReadyOnlyBuffer(buffer);
698 }
699
700 return newReadyOnlyBuffer(buffer.order(BIG_ENDIAN)).order(LITTLE_ENDIAN);
701 }
702
703 private static ReadOnlyByteBuf newReadyOnlyBuffer(ByteBuf buffer) {
704 return buffer instanceof AbstractByteBuf ?
705 new ReadOnlyAbstractByteBuf((AbstractByteBuf) buffer) :
706 new ReadOnlyByteBuf(buffer);
707 }
708
709
710
711
712 public static ByteBuf copyInt(int value) {
713 ByteBuf buf = buffer(4);
714 buf.writeInt(value);
715 return buf;
716 }
717
718
719
720
721 public static ByteBuf copyInt(int... values) {
722 if (values == null || values.length == 0) {
723 return EMPTY_BUFFER;
724 }
725 ByteBuf buffer = buffer(values.length * 4);
726 for (int v: values) {
727 buffer.writeInt(v);
728 }
729 return buffer;
730 }
731
732
733
734
735 public static ByteBuf copyShort(int value) {
736 ByteBuf buf = buffer(2);
737 buf.writeShort(value);
738 return buf;
739 }
740
741
742
743
744 public static ByteBuf copyShort(short... values) {
745 if (values == null || values.length == 0) {
746 return EMPTY_BUFFER;
747 }
748 ByteBuf buffer = buffer(values.length * 2);
749 for (int v: values) {
750 buffer.writeShort(v);
751 }
752 return buffer;
753 }
754
755
756
757
758 public static ByteBuf copyShort(int... values) {
759 if (values == null || values.length == 0) {
760 return EMPTY_BUFFER;
761 }
762 ByteBuf buffer = buffer(values.length * 2);
763 for (int v: values) {
764 buffer.writeShort(v);
765 }
766 return buffer;
767 }
768
769
770
771
772 public static ByteBuf copyMedium(int value) {
773 ByteBuf buf = buffer(3);
774 buf.writeMedium(value);
775 return buf;
776 }
777
778
779
780
781 public static ByteBuf copyMedium(int... values) {
782 if (values == null || values.length == 0) {
783 return EMPTY_BUFFER;
784 }
785 ByteBuf buffer = buffer(values.length * 3);
786 for (int v: values) {
787 buffer.writeMedium(v);
788 }
789 return buffer;
790 }
791
792
793
794
795 public static ByteBuf copyLong(long value) {
796 ByteBuf buf = buffer(8);
797 buf.writeLong(value);
798 return buf;
799 }
800
801
802
803
804 public static ByteBuf copyLong(long... values) {
805 if (values == null || values.length == 0) {
806 return EMPTY_BUFFER;
807 }
808 ByteBuf buffer = buffer(values.length * 8);
809 for (long v: values) {
810 buffer.writeLong(v);
811 }
812 return buffer;
813 }
814
815
816
817
818 public static ByteBuf copyBoolean(boolean value) {
819 ByteBuf buf = buffer(1);
820 buf.writeBoolean(value);
821 return buf;
822 }
823
824
825
826
827 public static ByteBuf copyBoolean(boolean... values) {
828 if (values == null || values.length == 0) {
829 return EMPTY_BUFFER;
830 }
831 ByteBuf buffer = buffer(values.length);
832 for (boolean v: values) {
833 buffer.writeBoolean(v);
834 }
835 return buffer;
836 }
837
838
839
840
841 public static ByteBuf copyFloat(float value) {
842 ByteBuf buf = buffer(4);
843 buf.writeFloat(value);
844 return buf;
845 }
846
847
848
849
850 public static ByteBuf copyFloat(float... values) {
851 if (values == null || values.length == 0) {
852 return EMPTY_BUFFER;
853 }
854 ByteBuf buffer = buffer(values.length * 4);
855 for (float v: values) {
856 buffer.writeFloat(v);
857 }
858 return buffer;
859 }
860
861
862
863
864 public static ByteBuf copyDouble(double value) {
865 ByteBuf buf = buffer(8);
866 buf.writeDouble(value);
867 return buf;
868 }
869
870
871
872
873 public static ByteBuf copyDouble(double... values) {
874 if (values == null || values.length == 0) {
875 return EMPTY_BUFFER;
876 }
877 ByteBuf buffer = buffer(values.length * 8);
878 for (double v: values) {
879 buffer.writeDouble(v);
880 }
881 return buffer;
882 }
883
884
885
886
887 public static ByteBuf unreleasableBuffer(ByteBuf buf) {
888 return new UnreleasableByteBuf(buf);
889 }
890
891
892
893
894
895
896
897 @Deprecated
898 public static ByteBuf unmodifiableBuffer(ByteBuf... buffers) {
899 return wrappedUnmodifiableBuffer(true, buffers);
900 }
901
902
903
904
905
906
907
908 public static ByteBuf wrappedUnmodifiableBuffer(ByteBuf... buffers) {
909 return wrappedUnmodifiableBuffer(false, buffers);
910 }
911
912 private static ByteBuf wrappedUnmodifiableBuffer(boolean copy, ByteBuf... buffers) {
913 switch (buffers.length) {
914 case 0:
915 return EMPTY_BUFFER;
916 case 1:
917 return buffers[0].asReadOnly();
918 default:
919 if (copy) {
920 buffers = Arrays.copyOf(buffers, buffers.length, ByteBuf[].class);
921 }
922 return new FixedCompositeByteBuf(ALLOC, buffers);
923 }
924 }
925
926 private Unpooled() {
927
928 }
929 }