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  package io.netty.buffer;
17  
18  import java.nio.ByteOrder;
19  
20  /**
21   * A {@link ByteBuf} implementation that wraps another buffer to prevent a user from increasing or decreasing the
22   * wrapped buffer's reference count.
23   */
24  final class UnreleasableByteBuf extends WrappedByteBuf {
25  
26      private SwappedByteBuf swappedBuf;
27  
28      UnreleasableByteBuf(ByteBuf buf) {
29          super(buf instanceof UnreleasableByteBuf ? buf.unwrap() : buf);
30      }
31  
32      @Override
33      public ByteBuf order(ByteOrder endianness) {
34          if (endianness == null) {
35              throw new NullPointerException("endianness");
36          }
37          if (endianness == order()) {
38              return this;
39          }
40  
41          SwappedByteBuf swappedBuf = this.swappedBuf;
42          if (swappedBuf == null) {
43              this.swappedBuf = swappedBuf = new SwappedByteBuf(this);
44          }
45          return swappedBuf;
46      }
47  
48      @Override
49      public ByteBuf asReadOnly() {
50          return buf.isReadOnly() ? this : new UnreleasableByteBuf(buf.asReadOnly());
51      }
52  
53      @Override
54      public ByteBuf readSlice(int length) {
55          return new UnreleasableByteBuf(buf.readSlice(length));
56      }
57  
58      @Override
59      public ByteBuf readRetainedSlice(int length) {
60          // We could call buf.readSlice(..), and then call buf.release(). However this creates a leak in unit tests
61          // because the release method on UnreleasableByteBuf will never allow the leak record to be cleaned up.
62          // So we just use readSlice(..) because the end result should be logically equivalent.
63          return readSlice(length);
64      }
65  
66      @Override
67      public ByteBuf slice() {
68          return new UnreleasableByteBuf(buf.slice());
69      }
70  
71      @Override
72      public ByteBuf retainedSlice() {
73          // We could call buf.retainedSlice(), and then call buf.release(). However this creates a leak in unit tests
74          // because the release method on UnreleasableByteBuf will never allow the leak record to be cleaned up.
75          // So we just use slice() because the end result should be logically equivalent.
76          return slice();
77      }
78  
79      @Override
80      public ByteBuf slice(int index, int length) {
81          return new UnreleasableByteBuf(buf.slice(index, length));
82      }
83  
84      @Override
85      public ByteBuf retainedSlice(int index, int length) {
86          // We could call buf.retainedSlice(..), and then call buf.release(). However this creates a leak in unit tests
87          // because the release method on UnreleasableByteBuf will never allow the leak record to be cleaned up.
88          // So we just use slice(..) because the end result should be logically equivalent.
89          return slice(index, length);
90      }
91  
92      @Override
93      public ByteBuf duplicate() {
94          return new UnreleasableByteBuf(buf.duplicate());
95      }
96  
97      @Override
98      public ByteBuf retainedDuplicate() {
99          // We could call buf.retainedDuplicate(), and then call buf.release(). However this creates a leak in unit tests
100         // because the release method on UnreleasableByteBuf will never allow the leak record to be cleaned up.
101         // So we just use duplicate() because the end result should be logically equivalent.
102         return duplicate();
103     }
104 
105     @Override
106     public ByteBuf retain(int increment) {
107         return this;
108     }
109 
110     @Override
111     public ByteBuf retain() {
112         return this;
113     }
114 
115     @Override
116     public ByteBuf touch() {
117         return this;
118     }
119 
120     @Override
121     public ByteBuf touch(Object hint) {
122         return this;
123     }
124 
125     @Override
126     public boolean release() {
127         return false;
128     }
129 
130     @Override
131     public boolean release(int decrement) {
132         return false;
133     }
134 }