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