查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.common.support;
21  
22  import java.nio.ByteOrder;
23  import java.nio.CharBuffer;
24  import java.nio.DoubleBuffer;
25  import java.nio.FloatBuffer;
26  import java.nio.IntBuffer;
27  import java.nio.LongBuffer;
28  import java.nio.ShortBuffer;
29  
30  import org.apache.mina.common.ByteBuffer;
31  import org.apache.mina.common.ByteBufferAllocator;
32  
33  /**
34   * A base implementation of {@link ByteBuffer}.  This implementation
35   * assumes that {@link ByteBuffer#buf()} always returns a correct NIO
36   * {@link java.nio.ByteBuffer} instance.  Most implementations could
37   * extend this class and implement their own buffer management mechanism.
38   *
39   * @author The Apache Directory Project (mina-dev@directory.apache.org)
40   * @version $Rev: 555855 $, $Date: 2007-07-13 12:19:00 +0900 (Fri, 13 Jul 2007) $
41   * @noinspection StaticNonFinalField
42   * @see ByteBufferAllocator
43   */
44  public abstract class BaseByteBuffer extends ByteBuffer {
45      private boolean autoExpand;
46  
47      /**
48       * We don't have any access to Buffer.markValue(), so we need to track it down,
49       * which will cause small extra overhead.
50       */
51      private int mark = -1;
52  
53      protected BaseByteBuffer() {
54      }
55  
56      public boolean isDirect() {
57          return buf().isDirect();
58      }
59  
60      public boolean isReadOnly() {
61          return buf().isReadOnly();
62      }
63  
64      public int capacity() {
65          return buf().capacity();
66      }
67  
68      public ByteBuffer capacity(int newCapacity) {
69          if (newCapacity > capacity()) {
70              // Allocate a new buffer and transfer all settings to it.
71              int pos = position();
72              int limit = limit();
73              ByteOrder bo = order();
74  
75              capacity0(newCapacity);
76              buf().limit(limit);
77              if (mark >= 0) {
78                  buf().position(mark);
79                  buf().mark();
80              }
81              buf().position(pos);
82              buf().order(bo);
83          }
84  
85          return this;
86      }
87  
88      /**
89       * Implement this method to increase the capacity of this buffer.
90       * <tt>newCapacity</tt> is always greater than the current capacity.
91       */
92      protected abstract void capacity0(int newCapacity);
93  
94      public boolean isAutoExpand() {
95          return autoExpand;
96      }
97  
98      public ByteBuffer setAutoExpand(boolean autoExpand) {
99          this.autoExpand = autoExpand;
100         return this;
101     }
102 
103     public ByteBuffer expand(int pos, int expectedRemaining) {
104         int end = pos + expectedRemaining;
105         if (end > capacity()) {
106             // The buffer needs expansion.
107             capacity(end);
108         }
109 
110         if (end > limit()) {
111             // We call limit() directly to prevent StackOverflowError
112             buf().limit(end);
113         }
114         return this;
115     }
116 
117     public int position() {
118         return buf().position();
119     }
120 
121     public ByteBuffer position(int newPosition) {
122         autoExpand(newPosition, 0);
123         buf().position(newPosition);
124         if (mark > newPosition) {
125             mark = -1;
126         }
127         return this;
128     }
129 
130     public int limit() {
131         return buf().limit();
132     }
133 
134     public ByteBuffer limit(int newLimit) {
135         autoExpand(newLimit, 0);
136         buf().limit(newLimit);
137         if (mark > newLimit) {
138             mark = -1;
139         }
140         return this;
141     }
142 
143     public ByteBuffer mark() {
144         buf().mark();
145         mark = position();
146         return this;
147     }
148 
149     public int markValue() {
150         return mark;
151     }
152 
153     public ByteBuffer reset() {
154         buf().reset();
155         return this;
156     }
157 
158     public ByteBuffer clear() {
159         buf().clear();
160         mark = -1;
161         return this;
162     }
163 
164     public ByteBuffer flip() {
165         buf().flip();
166         mark = -1;
167         return this;
168     }
169 
170     public ByteBuffer rewind() {
171         buf().rewind();
172         mark = -1;
173         return this;
174     }
175 
176     public byte get() {
177         return buf().get();
178     }
179 
180     public ByteBuffer put(byte b) {
181         autoExpand(1);
182         buf().put(b);
183         return this;
184     }
185 
186     public byte get(int index) {
187         return buf().get(index);
188     }
189 
190     public ByteBuffer put(int index, byte b) {
191         autoExpand(index, 1);
192         buf().put(index, b);
193         return this;
194     }
195 
196     public ByteBuffer get(byte[] dst, int offset, int length) {
197         buf().get(dst, offset, length);
198         return this;
199     }
200 
201     public ByteBuffer put(java.nio.ByteBuffer src) {
202         autoExpand(src.remaining());
203         buf().put(src);
204         return this;
205     }
206 
207     public ByteBuffer put(byte[] src, int offset, int length) {
208         autoExpand(length);
209         buf().put(src, offset, length);
210         return this;
211     }
212 
213     public ByteBuffer compact() {
214         buf().compact();
215         mark = -1;
216         return this;
217     }
218 
219     public ByteOrder order() {
220         return buf().order();
221     }
222 
223     public ByteBuffer order(ByteOrder bo) {
224         buf().order(bo);
225         return this;
226     }
227 
228     public char getChar() {
229         return buf().getChar();
230     }
231 
232     public ByteBuffer putChar(char value) {
233         autoExpand(2);
234         buf().putChar(value);
235         return this;
236     }
237 
238     public char getChar(int index) {
239         return buf().getChar(index);
240     }
241 
242     public ByteBuffer putChar(int index, char value) {
243         autoExpand(index, 2);
244         buf().putChar(index, value);
245         return this;
246     }
247 
248     public CharBuffer asCharBuffer() {
249         return buf().asCharBuffer();
250     }
251 
252     public short getShort() {
253         return buf().getShort();
254     }
255 
256     public ByteBuffer putShort(short value) {
257         autoExpand(2);
258         buf().putShort(value);
259         return this;
260     }
261 
262     public short getShort(int index) {
263         return buf().getShort(index);
264     }
265 
266     public ByteBuffer putShort(int index, short value) {
267         autoExpand(index, 2);
268         buf().putShort(index, value);
269         return this;
270     }
271 
272     public ShortBuffer asShortBuffer() {
273         return buf().asShortBuffer();
274     }
275 
276     public int getInt() {
277         return buf().getInt();
278     }
279 
280     public ByteBuffer putInt(int value) {
281         autoExpand(4);
282         buf().putInt(value);
283         return this;
284     }
285 
286     public int getInt(int index) {
287         return buf().getInt(index);
288     }
289 
290     public ByteBuffer putInt(int index, int value) {
291         autoExpand(index, 4);
292         buf().putInt(index, value);
293         return this;
294     }
295 
296     public IntBuffer asIntBuffer() {
297         return buf().asIntBuffer();
298     }
299 
300     public long getLong() {
301         return buf().getLong();
302     }
303 
304     public ByteBuffer putLong(long value) {
305         autoExpand(8);
306         buf().putLong(value);
307         return this;
308     }
309 
310     public long getLong(int index) {
311         return buf().getLong(index);
312     }
313 
314     public ByteBuffer putLong(int index, long value) {
315         autoExpand(index, 8);
316         buf().putLong(index, value);
317         return this;
318     }
319 
320     public LongBuffer asLongBuffer() {
321         return buf().asLongBuffer();
322     }
323 
324     public float getFloat() {
325         return buf().getFloat();
326     }
327 
328     public ByteBuffer putFloat(float value) {
329         autoExpand(4);
330         buf().putFloat(value);
331         return this;
332     }
333 
334     public float getFloat(int index) {
335         return buf().getFloat(index);
336     }
337 
338     public ByteBuffer putFloat(int index, float value) {
339         autoExpand(index, 4);
340         buf().putFloat(index, value);
341         return this;
342     }
343 
344     public FloatBuffer asFloatBuffer() {
345         return buf().asFloatBuffer();
346     }
347 
348     public double getDouble() {
349         return buf().getDouble();
350     }
351 
352     public ByteBuffer putDouble(double value) {
353         autoExpand(8);
354         buf().putDouble(value);
355         return this;
356     }
357 
358     public double getDouble(int index) {
359         return buf().getDouble(index);
360     }
361 
362     public ByteBuffer putDouble(int index, double value) {
363         autoExpand(index, 8);
364         buf().putDouble(index, value);
365         return this;
366     }
367 
368     public DoubleBuffer asDoubleBuffer() {
369         return buf().asDoubleBuffer();
370     }
371 }