1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.buffer;
18
19 import io.netty.util.internal.EmptyArrays;
20 import io.netty.util.internal.PlatformDependent;
21 import io.netty.util.internal.StringUtil;
22
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.ByteBuffer;
26 import java.nio.ByteOrder;
27 import java.nio.ReadOnlyBufferException;
28 import java.nio.channels.GatheringByteChannel;
29 import java.nio.channels.ScatteringByteChannel;
30 import java.nio.charset.Charset;
31
32
33
34
35 public final class EmptyByteBuf extends ByteBuf {
36
37 private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.allocateDirect(0);
38 private static final long EMPTY_BYTE_BUFFER_ADDRESS;
39
40 static {
41 long emptyByteBufferAddress = 0;
42 try {
43 if (PlatformDependent.hasUnsafe()) {
44 emptyByteBufferAddress = PlatformDependent.directBufferAddress(EMPTY_BYTE_BUFFER);
45 }
46 } catch (Throwable t) {
47
48 }
49 EMPTY_BYTE_BUFFER_ADDRESS = emptyByteBufferAddress;
50 }
51
52 private final ByteBufAllocator alloc;
53 private final ByteOrder order;
54 private final String str;
55 private EmptyByteBuf swapped;
56
57 public EmptyByteBuf(ByteBufAllocator alloc) {
58 this(alloc, ByteOrder.BIG_ENDIAN);
59 }
60
61 private EmptyByteBuf(ByteBufAllocator alloc, ByteOrder order) {
62 if (alloc == null) {
63 throw new NullPointerException("alloc");
64 }
65
66 this.alloc = alloc;
67 this.order = order;
68 str = StringUtil.simpleClassName(this) + (order == ByteOrder.BIG_ENDIAN? "BE" : "LE");
69 }
70
71 @Override
72 public int capacity() {
73 return 0;
74 }
75
76 @Override
77 public ByteBuf capacity(int newCapacity) {
78 throw new ReadOnlyBufferException();
79 }
80
81 @Override
82 public ByteBufAllocator alloc() {
83 return alloc;
84 }
85
86 @Override
87 public ByteOrder order() {
88 return order;
89 }
90
91 @Override
92 public ByteBuf unwrap() {
93 return null;
94 }
95
96 @Override
97 public boolean isDirect() {
98 return true;
99 }
100
101 @Override
102 public int maxCapacity() {
103 return 0;
104 }
105
106 @Override
107 public ByteBuf order(ByteOrder endianness) {
108 if (endianness == null) {
109 throw new NullPointerException("endianness");
110 }
111 if (endianness == order()) {
112 return this;
113 }
114
115 EmptyByteBuf swapped = this.swapped;
116 if (swapped != null) {
117 return swapped;
118 }
119
120 this.swapped = swapped = new EmptyByteBuf(alloc(), endianness);
121 return swapped;
122 }
123
124 @Override
125 public int readerIndex() {
126 return 0;
127 }
128
129 @Override
130 public ByteBuf readerIndex(int readerIndex) {
131 return checkIndex(readerIndex);
132 }
133
134 @Override
135 public int writerIndex() {
136 return 0;
137 }
138
139 @Override
140 public ByteBuf writerIndex(int writerIndex) {
141 return checkIndex(writerIndex);
142 }
143
144 @Override
145 public ByteBuf setIndex(int readerIndex, int writerIndex) {
146 checkIndex(readerIndex);
147 checkIndex(writerIndex);
148 return this;
149 }
150
151 @Override
152 public int readableBytes() {
153 return 0;
154 }
155
156 @Override
157 public int writableBytes() {
158 return 0;
159 }
160
161 @Override
162 public int maxWritableBytes() {
163 return 0;
164 }
165
166 @Override
167 public boolean isReadable() {
168 return false;
169 }
170
171 @Override
172 public boolean isWritable() {
173 return false;
174 }
175
176 @Override
177 public ByteBuf clear() {
178 return this;
179 }
180
181 @Override
182 public ByteBuf markReaderIndex() {
183 return this;
184 }
185
186 @Override
187 public ByteBuf resetReaderIndex() {
188 return this;
189 }
190
191 @Override
192 public ByteBuf markWriterIndex() {
193 return this;
194 }
195
196 @Override
197 public ByteBuf resetWriterIndex() {
198 return this;
199 }
200
201 @Override
202 public ByteBuf discardReadBytes() {
203 return this;
204 }
205
206 @Override
207 public ByteBuf discardSomeReadBytes() {
208 return this;
209 }
210
211 @Override
212 public ByteBuf ensureWritable(int minWritableBytes) {
213 if (minWritableBytes < 0) {
214 throw new IllegalArgumentException("minWritableBytes: " + minWritableBytes + " (expected: >= 0)");
215 }
216 if (minWritableBytes != 0) {
217 throw new IndexOutOfBoundsException();
218 }
219 return this;
220 }
221
222 @Override
223 public int ensureWritable(int minWritableBytes, boolean force) {
224 if (minWritableBytes < 0) {
225 throw new IllegalArgumentException("minWritableBytes: " + minWritableBytes + " (expected: >= 0)");
226 }
227
228 if (minWritableBytes == 0) {
229 return 0;
230 }
231
232 return 1;
233 }
234
235 @Override
236 public boolean getBoolean(int index) {
237 throw new IndexOutOfBoundsException();
238 }
239
240 @Override
241 public byte getByte(int index) {
242 throw new IndexOutOfBoundsException();
243 }
244
245 @Override
246 public short getUnsignedByte(int index) {
247 throw new IndexOutOfBoundsException();
248 }
249
250 @Override
251 public short getShort(int index) {
252 throw new IndexOutOfBoundsException();
253 }
254
255 @Override
256 public int getUnsignedShort(int index) {
257 throw new IndexOutOfBoundsException();
258 }
259
260 @Override
261 public int getMedium(int index) {
262 throw new IndexOutOfBoundsException();
263 }
264
265 @Override
266 public int getUnsignedMedium(int index) {
267 throw new IndexOutOfBoundsException();
268 }
269
270 @Override
271 public int getInt(int index) {
272 throw new IndexOutOfBoundsException();
273 }
274
275 @Override
276 public long getUnsignedInt(int index) {
277 throw new IndexOutOfBoundsException();
278 }
279
280 @Override
281 public long getLong(int index) {
282 throw new IndexOutOfBoundsException();
283 }
284
285 @Override
286 public char getChar(int index) {
287 throw new IndexOutOfBoundsException();
288 }
289
290 @Override
291 public float getFloat(int index) {
292 throw new IndexOutOfBoundsException();
293 }
294
295 @Override
296 public double getDouble(int index) {
297 throw new IndexOutOfBoundsException();
298 }
299
300 @Override
301 public ByteBuf getBytes(int index, ByteBuf dst) {
302 return checkIndex(index, dst.writableBytes());
303 }
304
305 @Override
306 public ByteBuf getBytes(int index, ByteBuf dst, int length) {
307 return checkIndex(index, length);
308 }
309
310 @Override
311 public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
312 return checkIndex(index, length);
313 }
314
315 @Override
316 public ByteBuf getBytes(int index, byte[] dst) {
317 return checkIndex(index, dst.length);
318 }
319
320 @Override
321 public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
322 return checkIndex(index, length);
323 }
324
325 @Override
326 public ByteBuf getBytes(int index, ByteBuffer dst) {
327 return checkIndex(index, dst.remaining());
328 }
329
330 @Override
331 public ByteBuf getBytes(int index, OutputStream out, int length) {
332 return checkIndex(index, length);
333 }
334
335 @Override
336 public int getBytes(int index, GatheringByteChannel out, int length) {
337 checkIndex(index, length);
338 return 0;
339 }
340
341 @Override
342 public ByteBuf setBoolean(int index, boolean value) {
343 throw new IndexOutOfBoundsException();
344 }
345
346 @Override
347 public ByteBuf setByte(int index, int value) {
348 throw new IndexOutOfBoundsException();
349 }
350
351 @Override
352 public ByteBuf setShort(int index, int value) {
353 throw new IndexOutOfBoundsException();
354 }
355
356 @Override
357 public ByteBuf setMedium(int index, int value) {
358 throw new IndexOutOfBoundsException();
359 }
360
361 @Override
362 public ByteBuf setInt(int index, int value) {
363 throw new IndexOutOfBoundsException();
364 }
365
366 @Override
367 public ByteBuf setLong(int index, long value) {
368 throw new IndexOutOfBoundsException();
369 }
370
371 @Override
372 public ByteBuf setChar(int index, int value) {
373 throw new IndexOutOfBoundsException();
374 }
375
376 @Override
377 public ByteBuf setFloat(int index, float value) {
378 throw new IndexOutOfBoundsException();
379 }
380
381 @Override
382 public ByteBuf setDouble(int index, double value) {
383 throw new IndexOutOfBoundsException();
384 }
385
386 @Override
387 public ByteBuf setBytes(int index, ByteBuf src) {
388 throw new IndexOutOfBoundsException();
389 }
390
391 @Override
392 public ByteBuf setBytes(int index, ByteBuf src, int length) {
393 return checkIndex(index, length);
394 }
395
396 @Override
397 public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
398 return checkIndex(index, length);
399 }
400
401 @Override
402 public ByteBuf setBytes(int index, byte[] src) {
403 return checkIndex(index, src.length);
404 }
405
406 @Override
407 public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
408 return checkIndex(index, length);
409 }
410
411 @Override
412 public ByteBuf setBytes(int index, ByteBuffer src) {
413 return checkIndex(index, src.remaining());
414 }
415
416 @Override
417 public int setBytes(int index, InputStream in, int length) {
418 checkIndex(index, length);
419 return 0;
420 }
421
422 @Override
423 public int setBytes(int index, ScatteringByteChannel in, int length) {
424 checkIndex(index, length);
425 return 0;
426 }
427
428 @Override
429 public ByteBuf setZero(int index, int length) {
430 return checkIndex(index, length);
431 }
432
433 @Override
434 public boolean readBoolean() {
435 throw new IndexOutOfBoundsException();
436 }
437
438 @Override
439 public byte readByte() {
440 throw new IndexOutOfBoundsException();
441 }
442
443 @Override
444 public short readUnsignedByte() {
445 throw new IndexOutOfBoundsException();
446 }
447
448 @Override
449 public short readShort() {
450 throw new IndexOutOfBoundsException();
451 }
452
453 @Override
454 public int readUnsignedShort() {
455 throw new IndexOutOfBoundsException();
456 }
457
458 @Override
459 public int readMedium() {
460 throw new IndexOutOfBoundsException();
461 }
462
463 @Override
464 public int readUnsignedMedium() {
465 throw new IndexOutOfBoundsException();
466 }
467
468 @Override
469 public int readInt() {
470 throw new IndexOutOfBoundsException();
471 }
472
473 @Override
474 public long readUnsignedInt() {
475 throw new IndexOutOfBoundsException();
476 }
477
478 @Override
479 public long readLong() {
480 throw new IndexOutOfBoundsException();
481 }
482
483 @Override
484 public char readChar() {
485 throw new IndexOutOfBoundsException();
486 }
487
488 @Override
489 public float readFloat() {
490 throw new IndexOutOfBoundsException();
491 }
492
493 @Override
494 public double readDouble() {
495 throw new IndexOutOfBoundsException();
496 }
497
498 @Override
499 public ByteBuf readBytes(int length) {
500 return checkLength(length);
501 }
502
503 @Override
504 public ByteBuf readSlice(int length) {
505 return checkLength(length);
506 }
507
508 @Override
509 public ByteBuf readBytes(ByteBuf dst) {
510 return checkLength(dst.writableBytes());
511 }
512
513 @Override
514 public ByteBuf readBytes(ByteBuf dst, int length) {
515 return checkLength(length);
516 }
517
518 @Override
519 public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
520 return checkLength(length);
521 }
522
523 @Override
524 public ByteBuf readBytes(byte[] dst) {
525 return checkLength(dst.length);
526 }
527
528 @Override
529 public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
530 return checkLength(length);
531 }
532
533 @Override
534 public ByteBuf readBytes(ByteBuffer dst) {
535 return checkLength(dst.remaining());
536 }
537
538 @Override
539 public ByteBuf readBytes(OutputStream out, int length) {
540 return checkLength(length);
541 }
542
543 @Override
544 public int readBytes(GatheringByteChannel out, int length) {
545 checkLength(length);
546 return 0;
547 }
548
549 @Override
550 public ByteBuf skipBytes(int length) {
551 return checkLength(length);
552 }
553
554 @Override
555 public ByteBuf writeBoolean(boolean value) {
556 throw new IndexOutOfBoundsException();
557 }
558
559 @Override
560 public ByteBuf writeByte(int value) {
561 throw new IndexOutOfBoundsException();
562 }
563
564 @Override
565 public ByteBuf writeShort(int value) {
566 throw new IndexOutOfBoundsException();
567 }
568
569 @Override
570 public ByteBuf writeMedium(int value) {
571 throw new IndexOutOfBoundsException();
572 }
573
574 @Override
575 public ByteBuf writeInt(int value) {
576 throw new IndexOutOfBoundsException();
577 }
578
579 @Override
580 public ByteBuf writeLong(long value) {
581 throw new IndexOutOfBoundsException();
582 }
583
584 @Override
585 public ByteBuf writeChar(int value) {
586 throw new IndexOutOfBoundsException();
587 }
588
589 @Override
590 public ByteBuf writeFloat(float value) {
591 throw new IndexOutOfBoundsException();
592 }
593
594 @Override
595 public ByteBuf writeDouble(double value) {
596 throw new IndexOutOfBoundsException();
597 }
598
599 @Override
600 public ByteBuf writeBytes(ByteBuf src) {
601 return checkLength(src.readableBytes());
602 }
603
604 @Override
605 public ByteBuf writeBytes(ByteBuf src, int length) {
606 return checkLength(length);
607 }
608
609 @Override
610 public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
611 return checkLength(length);
612 }
613
614 @Override
615 public ByteBuf writeBytes(byte[] src) {
616 return checkLength(src.length);
617 }
618
619 @Override
620 public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
621 return checkLength(length);
622 }
623
624 @Override
625 public ByteBuf writeBytes(ByteBuffer src) {
626 return checkLength(src.remaining());
627 }
628
629 @Override
630 public int writeBytes(InputStream in, int length) {
631 checkLength(length);
632 return 0;
633 }
634
635 @Override
636 public int writeBytes(ScatteringByteChannel in, int length) {
637 checkLength(length);
638 return 0;
639 }
640
641 @Override
642 public ByteBuf writeZero(int length) {
643 return checkLength(length);
644 }
645
646 @Override
647 public int indexOf(int fromIndex, int toIndex, byte value) {
648 checkIndex(fromIndex);
649 checkIndex(toIndex);
650 return -1;
651 }
652
653 @Override
654 public int bytesBefore(byte value) {
655 return -1;
656 }
657
658 @Override
659 public int bytesBefore(int length, byte value) {
660 checkLength(length);
661 return -1;
662 }
663
664 @Override
665 public int bytesBefore(int index, int length, byte value) {
666 checkIndex(index, length);
667 return -1;
668 }
669
670 @Override
671 public int forEachByte(ByteBufProcessor processor) {
672 return -1;
673 }
674
675 @Override
676 public int forEachByte(int index, int length, ByteBufProcessor processor) {
677 checkIndex(index, length);
678 return -1;
679 }
680
681 @Override
682 public int forEachByteDesc(ByteBufProcessor processor) {
683 return -1;
684 }
685
686 @Override
687 public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
688 checkIndex(index, length);
689 return -1;
690 }
691
692 @Override
693 public ByteBuf copy() {
694 return this;
695 }
696
697 @Override
698 public ByteBuf copy(int index, int length) {
699 return checkIndex(index, length);
700 }
701
702 @Override
703 public ByteBuf slice() {
704 return this;
705 }
706
707 @Override
708 public ByteBuf slice(int index, int length) {
709 return checkIndex(index, length);
710 }
711
712 @Override
713 public ByteBuf duplicate() {
714 return this;
715 }
716
717 @Override
718 public int nioBufferCount() {
719 return 1;
720 }
721
722 @Override
723 public ByteBuffer nioBuffer() {
724 return EMPTY_BYTE_BUFFER;
725 }
726
727 @Override
728 public ByteBuffer nioBuffer(int index, int length) {
729 checkIndex(index, length);
730 return nioBuffer();
731 }
732
733 @Override
734 public ByteBuffer[] nioBuffers() {
735 return new ByteBuffer[] { EMPTY_BYTE_BUFFER };
736 }
737
738 @Override
739 public ByteBuffer[] nioBuffers(int index, int length) {
740 checkIndex(index, length);
741 return nioBuffers();
742 }
743
744 @Override
745 public ByteBuffer internalNioBuffer(int index, int length) {
746 return EMPTY_BYTE_BUFFER;
747 }
748
749 @Override
750 public boolean hasArray() {
751 return true;
752 }
753
754 @Override
755 public byte[] array() {
756 return EmptyArrays.EMPTY_BYTES;
757 }
758
759 @Override
760 public int arrayOffset() {
761 return 0;
762 }
763
764 @Override
765 public boolean hasMemoryAddress() {
766 return EMPTY_BYTE_BUFFER_ADDRESS != 0;
767 }
768
769 @Override
770 public long memoryAddress() {
771 if (hasMemoryAddress()) {
772 return EMPTY_BYTE_BUFFER_ADDRESS;
773 } else {
774 throw new UnsupportedOperationException();
775 }
776 }
777
778 @Override
779 public String toString(Charset charset) {
780 return "";
781 }
782
783 @Override
784 public String toString(int index, int length, Charset charset) {
785 checkIndex(index, length);
786 return toString(charset);
787 }
788
789 @Override
790 public int hashCode() {
791 return 0;
792 }
793
794 @Override
795 public boolean equals(Object obj) {
796 return obj instanceof ByteBuf && !((ByteBuf) obj).isReadable();
797 }
798
799 @Override
800 public int compareTo(ByteBuf buffer) {
801 return buffer.isReadable()? -1 : 0;
802 }
803
804 @Override
805 public String toString() {
806 return str;
807 }
808
809 @Override
810 public boolean isReadable(int size) {
811 return false;
812 }
813
814 @Override
815 public boolean isWritable(int size) {
816 return false;
817 }
818
819 @Override
820 public int refCnt() {
821 return 1;
822 }
823
824 @Override
825 public ByteBuf retain() {
826 return this;
827 }
828
829 @Override
830 public ByteBuf retain(int increment) {
831 return this;
832 }
833
834 @Override
835 public boolean release() {
836 return false;
837 }
838
839 @Override
840 public boolean release(int decrement) {
841 return false;
842 }
843
844 private ByteBuf checkIndex(int index) {
845 if (index != 0) {
846 throw new IndexOutOfBoundsException();
847 }
848 return this;
849 }
850
851 private ByteBuf checkIndex(int index, int length) {
852 if (length < 0) {
853 throw new IllegalArgumentException("length: " + length);
854 }
855 if (index != 0 || length != 0) {
856 throw new IndexOutOfBoundsException();
857 }
858 return this;
859 }
860
861 private ByteBuf checkLength(int length) {
862 if (length < 0) {
863 throw new IllegalArgumentException("length: " + length + " (expected: >= 0)");
864 }
865 if (length != 0) {
866 throw new IndexOutOfBoundsException();
867 }
868 return this;
869 }
870 }