View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.buffer;
18  
19  import io.netty.util.ResourceLeak;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.nio.ByteBuffer;
25  import java.nio.ByteOrder;
26  import java.nio.channels.GatheringByteChannel;
27  import java.nio.channels.ScatteringByteChannel;
28  import java.nio.charset.Charset;
29  
30  final class AdvancedLeakAwareByteBuf extends WrappedByteBuf {
31  
32      private final ResourceLeak leak;
33  
34      AdvancedLeakAwareByteBuf(ByteBuf buf, ResourceLeak leak) {
35          super(buf);
36          this.leak = leak;
37      }
38  
39      @Override
40      public ByteBuf order(ByteOrder endianness) {
41          leak.record();
42          if (order() == endianness) {
43              return this;
44          } else {
45              return new AdvancedLeakAwareByteBuf(super.order(endianness), leak);
46          }
47      }
48  
49      @Override
50      public ByteBuf slice() {
51          leak.record();
52          return new AdvancedLeakAwareByteBuf(super.slice(), leak);
53      }
54  
55      @Override
56      public ByteBuf slice(int index, int length) {
57          leak.record();
58          return new AdvancedLeakAwareByteBuf(super.slice(index, length), leak);
59      }
60  
61      @Override
62      public ByteBuf duplicate() {
63          leak.record();
64          return new AdvancedLeakAwareByteBuf(super.duplicate(), leak);
65      }
66  
67      @Override
68      public ByteBuf readSlice(int length) {
69          leak.record();
70          return new AdvancedLeakAwareByteBuf(super.readSlice(length), leak);
71      }
72  
73      @Override
74      public ByteBuf discardReadBytes() {
75          leak.record();
76          return super.discardReadBytes();
77      }
78  
79      @Override
80      public ByteBuf discardSomeReadBytes() {
81          leak.record();
82          return super.discardSomeReadBytes();
83      }
84  
85      @Override
86      public ByteBuf ensureWritable(int minWritableBytes) {
87          leak.record();
88          return super.ensureWritable(minWritableBytes);
89      }
90  
91      @Override
92      public int ensureWritable(int minWritableBytes, boolean force) {
93          leak.record();
94          return super.ensureWritable(minWritableBytes, force);
95      }
96  
97      @Override
98      public boolean getBoolean(int index) {
99          leak.record();
100         return super.getBoolean(index);
101     }
102 
103     @Override
104     public byte getByte(int index) {
105         leak.record();
106         return super.getByte(index);
107     }
108 
109     @Override
110     public short getUnsignedByte(int index) {
111         leak.record();
112         return super.getUnsignedByte(index);
113     }
114 
115     @Override
116     public short getShort(int index) {
117         leak.record();
118         return super.getShort(index);
119     }
120 
121     @Override
122     public int getUnsignedShort(int index) {
123         leak.record();
124         return super.getUnsignedShort(index);
125     }
126 
127     @Override
128     public int getMedium(int index) {
129         leak.record();
130         return super.getMedium(index);
131     }
132 
133     @Override
134     public int getUnsignedMedium(int index) {
135         leak.record();
136         return super.getUnsignedMedium(index);
137     }
138 
139     @Override
140     public int getInt(int index) {
141         leak.record();
142         return super.getInt(index);
143     }
144 
145     @Override
146     public long getUnsignedInt(int index) {
147         leak.record();
148         return super.getUnsignedInt(index);
149     }
150 
151     @Override
152     public long getLong(int index) {
153         leak.record();
154         return super.getLong(index);
155     }
156 
157     @Override
158     public char getChar(int index) {
159         leak.record();
160         return super.getChar(index);
161     }
162 
163     @Override
164     public float getFloat(int index) {
165         leak.record();
166         return super.getFloat(index);
167     }
168 
169     @Override
170     public double getDouble(int index) {
171         leak.record();
172         return super.getDouble(index);
173     }
174 
175     @Override
176     public ByteBuf getBytes(int index, ByteBuf dst) {
177         leak.record();
178         return super.getBytes(index, dst);
179     }
180 
181     @Override
182     public ByteBuf getBytes(int index, ByteBuf dst, int length) {
183         leak.record();
184         return super.getBytes(index, dst, length);
185     }
186 
187     @Override
188     public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
189         leak.record();
190         return super.getBytes(index, dst, dstIndex, length);
191     }
192 
193     @Override
194     public ByteBuf getBytes(int index, byte[] dst) {
195         leak.record();
196         return super.getBytes(index, dst);
197     }
198 
199     @Override
200     public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
201         leak.record();
202         return super.getBytes(index, dst, dstIndex, length);
203     }
204 
205     @Override
206     public ByteBuf getBytes(int index, ByteBuffer dst) {
207         leak.record();
208         return super.getBytes(index, dst);
209     }
210 
211     @Override
212     public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
213         leak.record();
214         return super.getBytes(index, out, length);
215     }
216 
217     @Override
218     public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
219         leak.record();
220         return super.getBytes(index, out, length);
221     }
222 
223     @Override
224     public ByteBuf setBoolean(int index, boolean value) {
225         leak.record();
226         return super.setBoolean(index, value);
227     }
228 
229     @Override
230     public ByteBuf setByte(int index, int value) {
231         leak.record();
232         return super.setByte(index, value);
233     }
234 
235     @Override
236     public ByteBuf setShort(int index, int value) {
237         leak.record();
238         return super.setShort(index, value);
239     }
240 
241     @Override
242     public ByteBuf setMedium(int index, int value) {
243         leak.record();
244         return super.setMedium(index, value);
245     }
246 
247     @Override
248     public ByteBuf setInt(int index, int value) {
249         leak.record();
250         return super.setInt(index, value);
251     }
252 
253     @Override
254     public ByteBuf setLong(int index, long value) {
255         leak.record();
256         return super.setLong(index, value);
257     }
258 
259     @Override
260     public ByteBuf setChar(int index, int value) {
261         leak.record();
262         return super.setChar(index, value);
263     }
264 
265     @Override
266     public ByteBuf setFloat(int index, float value) {
267         leak.record();
268         return super.setFloat(index, value);
269     }
270 
271     @Override
272     public ByteBuf setDouble(int index, double value) {
273         leak.record();
274         return super.setDouble(index, value);
275     }
276 
277     @Override
278     public ByteBuf setBytes(int index, ByteBuf src) {
279         leak.record();
280         return super.setBytes(index, src);
281     }
282 
283     @Override
284     public ByteBuf setBytes(int index, ByteBuf src, int length) {
285         leak.record();
286         return super.setBytes(index, src, length);
287     }
288 
289     @Override
290     public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
291         leak.record();
292         return super.setBytes(index, src, srcIndex, length);
293     }
294 
295     @Override
296     public ByteBuf setBytes(int index, byte[] src) {
297         leak.record();
298         return super.setBytes(index, src);
299     }
300 
301     @Override
302     public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
303         leak.record();
304         return super.setBytes(index, src, srcIndex, length);
305     }
306 
307     @Override
308     public ByteBuf setBytes(int index, ByteBuffer src) {
309         leak.record();
310         return super.setBytes(index, src);
311     }
312 
313     @Override
314     public int setBytes(int index, InputStream in, int length) throws IOException {
315         leak.record();
316         return super.setBytes(index, in, length);
317     }
318 
319     @Override
320     public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
321         leak.record();
322         return super.setBytes(index, in, length);
323     }
324 
325     @Override
326     public ByteBuf setZero(int index, int length) {
327         leak.record();
328         return super.setZero(index, length);
329     }
330 
331     @Override
332     public boolean readBoolean() {
333         leak.record();
334         return super.readBoolean();
335     }
336 
337     @Override
338     public byte readByte() {
339         leak.record();
340         return super.readByte();
341     }
342 
343     @Override
344     public short readUnsignedByte() {
345         leak.record();
346         return super.readUnsignedByte();
347     }
348 
349     @Override
350     public short readShort() {
351         leak.record();
352         return super.readShort();
353     }
354 
355     @Override
356     public int readUnsignedShort() {
357         leak.record();
358         return super.readUnsignedShort();
359     }
360 
361     @Override
362     public int readMedium() {
363         leak.record();
364         return super.readMedium();
365     }
366 
367     @Override
368     public int readUnsignedMedium() {
369         leak.record();
370         return super.readUnsignedMedium();
371     }
372 
373     @Override
374     public int readInt() {
375         leak.record();
376         return super.readInt();
377     }
378 
379     @Override
380     public long readUnsignedInt() {
381         leak.record();
382         return super.readUnsignedInt();
383     }
384 
385     @Override
386     public long readLong() {
387         leak.record();
388         return super.readLong();
389     }
390 
391     @Override
392     public char readChar() {
393         leak.record();
394         return super.readChar();
395     }
396 
397     @Override
398     public float readFloat() {
399         leak.record();
400         return super.readFloat();
401     }
402 
403     @Override
404     public double readDouble() {
405         leak.record();
406         return super.readDouble();
407     }
408 
409     @Override
410     public ByteBuf readBytes(int length) {
411         leak.record();
412         return super.readBytes(length);
413     }
414 
415     @Override
416     public ByteBuf readBytes(ByteBuf dst) {
417         leak.record();
418         return super.readBytes(dst);
419     }
420 
421     @Override
422     public ByteBuf readBytes(ByteBuf dst, int length) {
423         leak.record();
424         return super.readBytes(dst, length);
425     }
426 
427     @Override
428     public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) {
429         leak.record();
430         return super.readBytes(dst, dstIndex, length);
431     }
432 
433     @Override
434     public ByteBuf readBytes(byte[] dst) {
435         leak.record();
436         return super.readBytes(dst);
437     }
438 
439     @Override
440     public ByteBuf readBytes(byte[] dst, int dstIndex, int length) {
441         leak.record();
442         return super.readBytes(dst, dstIndex, length);
443     }
444 
445     @Override
446     public ByteBuf readBytes(ByteBuffer dst) {
447         leak.record();
448         return super.readBytes(dst);
449     }
450 
451     @Override
452     public ByteBuf readBytes(OutputStream out, int length) throws IOException {
453         leak.record();
454         return super.readBytes(out, length);
455     }
456 
457     @Override
458     public int readBytes(GatheringByteChannel out, int length) throws IOException {
459         leak.record();
460         return super.readBytes(out, length);
461     }
462 
463     @Override
464     public ByteBuf skipBytes(int length) {
465         leak.record();
466         return super.skipBytes(length);
467     }
468 
469     @Override
470     public ByteBuf writeBoolean(boolean value) {
471         leak.record();
472         return super.writeBoolean(value);
473     }
474 
475     @Override
476     public ByteBuf writeByte(int value) {
477         leak.record();
478         return super.writeByte(value);
479     }
480 
481     @Override
482     public ByteBuf writeShort(int value) {
483         leak.record();
484         return super.writeShort(value);
485     }
486 
487     @Override
488     public ByteBuf writeMedium(int value) {
489         leak.record();
490         return super.writeMedium(value);
491     }
492 
493     @Override
494     public ByteBuf writeInt(int value) {
495         leak.record();
496         return super.writeInt(value);
497     }
498 
499     @Override
500     public ByteBuf writeLong(long value) {
501         leak.record();
502         return super.writeLong(value);
503     }
504 
505     @Override
506     public ByteBuf writeChar(int value) {
507         leak.record();
508         return super.writeChar(value);
509     }
510 
511     @Override
512     public ByteBuf writeFloat(float value) {
513         leak.record();
514         return super.writeFloat(value);
515     }
516 
517     @Override
518     public ByteBuf writeDouble(double value) {
519         leak.record();
520         return super.writeDouble(value);
521     }
522 
523     @Override
524     public ByteBuf writeBytes(ByteBuf src) {
525         leak.record();
526         return super.writeBytes(src);
527     }
528 
529     @Override
530     public ByteBuf writeBytes(ByteBuf src, int length) {
531         leak.record();
532         return super.writeBytes(src, length);
533     }
534 
535     @Override
536     public ByteBuf writeBytes(ByteBuf src, int srcIndex, int length) {
537         leak.record();
538         return super.writeBytes(src, srcIndex, length);
539     }
540 
541     @Override
542     public ByteBuf writeBytes(byte[] src) {
543         leak.record();
544         return super.writeBytes(src);
545     }
546 
547     @Override
548     public ByteBuf writeBytes(byte[] src, int srcIndex, int length) {
549         leak.record();
550         return super.writeBytes(src, srcIndex, length);
551     }
552 
553     @Override
554     public ByteBuf writeBytes(ByteBuffer src) {
555         leak.record();
556         return super.writeBytes(src);
557     }
558 
559     @Override
560     public int writeBytes(InputStream in, int length) throws IOException {
561         leak.record();
562         return super.writeBytes(in, length);
563     }
564 
565     @Override
566     public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
567         leak.record();
568         return super.writeBytes(in, length);
569     }
570 
571     @Override
572     public ByteBuf writeZero(int length) {
573         leak.record();
574         return super.writeZero(length);
575     }
576 
577     @Override
578     public int indexOf(int fromIndex, int toIndex, byte value) {
579         leak.record();
580         return super.indexOf(fromIndex, toIndex, value);
581     }
582 
583     @Override
584     public int bytesBefore(byte value) {
585         leak.record();
586         return super.bytesBefore(value);
587     }
588 
589     @Override
590     public int bytesBefore(int length, byte value) {
591         leak.record();
592         return super.bytesBefore(length, value);
593     }
594 
595     @Override
596     public int bytesBefore(int index, int length, byte value) {
597         leak.record();
598         return super.bytesBefore(index, length, value);
599     }
600 
601     @Override
602     public int forEachByte(ByteBufProcessor processor) {
603         leak.record();
604         return super.forEachByte(processor);
605     }
606 
607     @Override
608     public int forEachByte(int index, int length, ByteBufProcessor processor) {
609         leak.record();
610         return super.forEachByte(index, length, processor);
611     }
612 
613     @Override
614     public int forEachByteDesc(ByteBufProcessor processor) {
615         leak.record();
616         return super.forEachByteDesc(processor);
617     }
618 
619     @Override
620     public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
621         leak.record();
622         return super.forEachByteDesc(index, length, processor);
623     }
624 
625     @Override
626     public ByteBuf copy() {
627         leak.record();
628         return super.copy();
629     }
630 
631     @Override
632     public ByteBuf copy(int index, int length) {
633         leak.record();
634         return super.copy(index, length);
635     }
636 
637     @Override
638     public int nioBufferCount() {
639         leak.record();
640         return super.nioBufferCount();
641     }
642 
643     @Override
644     public ByteBuffer nioBuffer() {
645         leak.record();
646         return super.nioBuffer();
647     }
648 
649     @Override
650     public ByteBuffer nioBuffer(int index, int length) {
651         leak.record();
652         return super.nioBuffer(index, length);
653     }
654 
655     @Override
656     public ByteBuffer[] nioBuffers() {
657         leak.record();
658         return super.nioBuffers();
659     }
660 
661     @Override
662     public ByteBuffer[] nioBuffers(int index, int length) {
663         leak.record();
664         return super.nioBuffers(index, length);
665     }
666 
667     @Override
668     public ByteBuffer internalNioBuffer(int index, int length) {
669         leak.record();
670         return super.internalNioBuffer(index, length);
671     }
672 
673     @Override
674     public String toString(Charset charset) {
675         leak.record();
676         return super.toString(charset);
677     }
678 
679     @Override
680     public String toString(int index, int length, Charset charset) {
681         leak.record();
682         return super.toString(index, length, charset);
683     }
684 
685     @Override
686     public ByteBuf capacity(int newCapacity) {
687         leak.record();
688         return super.capacity(newCapacity);
689     }
690 
691     @Override
692     public ByteBuf retain() {
693         leak.record();
694         return super.retain();
695     }
696 
697     @Override
698     public ByteBuf retain(int increment) {
699         leak.record();
700         return super.retain(increment);
701     }
702 
703     @Override
704     public ByteBuf touch() {
705         leak.record();
706         return this;
707     }
708 
709     @Override
710     public ByteBuf touch(Object hint) {
711         leak.record(hint);
712         return this;
713     }
714 
715     @Override
716     public boolean release() {
717         boolean deallocated = super.release();
718         if (deallocated) {
719             leak.close();
720         } else {
721             leak.record();
722         }
723         return deallocated;
724     }
725 
726     @Override
727     public boolean release(int decrement) {
728         boolean deallocated = super.release(decrement);
729         if (deallocated) {
730             leak.close();
731         } else {
732             leak.record();
733         }
734         return deallocated;
735     }
736 }