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);
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 readSlice(int length) {
50          return new UnreleasableByteBuf(buf.readSlice(length));
51      }
52  
53      @Override
54      public ByteBuf slice() {
55          return new UnreleasableByteBuf(buf.slice());
56      }
57  
58      @Override
59      public ByteBuf slice(int index, int length) {
60          return new UnreleasableByteBuf(buf.slice(index, length));
61      }
62  
63      @Override
64      public ByteBuf duplicate() {
65          return new UnreleasableByteBuf(buf.duplicate());
66      }
67  
68      @Override
69      public ByteBuf retain(int increment) {
70          return this;
71      }
72  
73      @Override
74      public ByteBuf retain() {
75          return this;
76      }
77  
78      @Override
79      public boolean release() {
80          return false;
81      }
82  
83      @Override
84      public boolean release(int decrement) {
85          return false;
86      }
87  }