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 org.jboss.netty.buffer;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.nio.ByteBuffer;
23  import java.nio.channels.GatheringByteChannel;
24  import java.nio.channels.ScatteringByteChannel;
25  
26  /**
27   * An immutable empty buffer implementation. Typically used as a singleton via
28   * {@link ChannelBuffers#EMPTY_BUFFER} and returned by {@link ChannelBuffers#buffer(int)} etc when
29   * an empty buffer is requested.
30   *
31   * <p>Note: For backwards compatibility, this class extends {@link BigEndianHeapChannelBuffer}.
32   * However, it never makes any writes to the reader and writer indices, which avoids contention
33   * when the singleton instance is used concurrently.
34   */
35  public class EmptyChannelBuffer extends BigEndianHeapChannelBuffer {
36  
37      private static final byte[] BUFFER = {};
38  
39      EmptyChannelBuffer() {
40          super(BUFFER);
41      }
42  
43      @Override
44      public void clear() {
45      }
46  
47      @Override
48      public void readerIndex(int readerIndex) {
49          if (readerIndex != 0) {
50              throw new IndexOutOfBoundsException("Invalid readerIndex: "
51                      + readerIndex + " - Maximum is 0");
52          }
53      }
54  
55      @Override
56      public void writerIndex(int writerIndex) {
57          if (writerIndex != 0) {
58              throw new IndexOutOfBoundsException("Invalid writerIndex: "
59                      + writerIndex + " - Maximum is 0");
60          }
61      }
62  
63      @Override
64      public void setIndex(int readerIndex, int writerIndex) {
65          if (writerIndex != 0 || readerIndex != 0) {
66              throw new IndexOutOfBoundsException("Invalid writerIndex: "
67                      + writerIndex + " - Maximum is " + readerIndex + " or "
68                      + capacity());
69          }
70      }
71  
72      @Override
73      public void markReaderIndex() {
74      }
75  
76      @Override
77      public void resetReaderIndex() {
78      }
79  
80      @Override
81      public void markWriterIndex() {
82      }
83  
84      @Override
85      public void resetWriterIndex() {
86      }
87  
88      @Override
89      public void discardReadBytes() {
90      }
91  
92      @Override
93      public ChannelBuffer readBytes(int length) {
94          checkReadableBytes(length);
95          return this;
96      }
97  
98      @Override
99      public ChannelBuffer readSlice(int length) {
100         checkReadableBytes(length);
101         return this;
102     }
103 
104     @Override
105     public void readBytes(byte[] dst, int dstIndex, int length) {
106         checkReadableBytes(length);
107     }
108 
109     @Override
110     public void readBytes(byte[] dst) {
111         checkReadableBytes(dst.length);
112     }
113 
114     @Override
115     public void readBytes(ChannelBuffer dst) {
116         checkReadableBytes(dst.writableBytes());
117     }
118 
119     @Override
120     public void readBytes(ChannelBuffer dst, int length) {
121         checkReadableBytes(length);
122     }
123 
124     @Override
125     public void readBytes(ChannelBuffer dst, int dstIndex, int length) {
126         checkReadableBytes(length);
127     }
128 
129     @Override
130     public void readBytes(ByteBuffer dst) {
131         checkReadableBytes(dst.remaining());
132     }
133 
134     @Override
135     public int readBytes(GatheringByteChannel out, int length) throws IOException {
136         checkReadableBytes(length);
137         return 0;
138     }
139 
140     @Override
141     public void readBytes(OutputStream out, int length) throws IOException {
142         checkReadableBytes(length);
143     }
144 
145     @Override
146     public void skipBytes(int length) {
147         checkReadableBytes(length);
148     }
149 
150     @Override
151     public void writeBytes(byte[] src, int srcIndex, int length) {
152         checkWritableBytes(length);
153     }
154 
155     @Override
156     public void writeBytes(ChannelBuffer src, int length) {
157         checkWritableBytes(length);
158     }
159 
160     @Override
161     public void writeBytes(ChannelBuffer src, int srcIndex, int length) {
162         checkWritableBytes(length);
163     }
164 
165     @Override
166     public void writeBytes(ByteBuffer src) {
167         checkWritableBytes(src.remaining());
168     }
169 
170     @Override
171     public int writeBytes(InputStream in, int length) throws IOException {
172         checkWritableBytes(length);
173         return 0;
174     }
175 
176     @Override
177     public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
178         checkWritableBytes(length);
179         return 0;
180     }
181 
182     @Override
183     public void writeZero(int length) {
184         checkWritableBytes(length);
185     }
186 
187     /**
188      * Throws an {@link IndexOutOfBoundsException} the length is not 0.
189      */
190     private void checkWritableBytes(int length) {
191         if (length == 0) {
192             return;
193         }
194         if (length > 0) {
195             throw new IndexOutOfBoundsException("Writable bytes exceeded - Need "
196                     + length + ", maximum is " + 0);
197         } else {
198             throw new IndexOutOfBoundsException("length < 0");
199         }
200     }
201 
202     /**
203      * Throws an {@link IndexOutOfBoundsException} the length is not 0.
204      */
205     protected void checkReadableBytes(int length) {
206         if (length == 0) {
207             return;
208         }
209         if (length > 0) {
210             throw new IndexOutOfBoundsException("Not enough readable bytes - Need "
211                     + length + ", maximum is " + readableBytes());
212         } else {
213             throw new IndexOutOfBoundsException("length < 0");
214         }
215     }
216 }