View Javadoc

1   /*
2    * Copyright 2012 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 org.jboss.netty.buffer;
17  
18  import java.nio.ByteBuffer;
19  import java.nio.ByteOrder;
20  
21  /**
22   * A {@link ChannelBufferFactory} which merely allocates a heap buffer with
23   * the specified capacity.  {@link HeapChannelBufferFactory} should perform
24   * very well in most situations because it relies on the JVM garbage collector,
25   * which is highly optimized for heap allocation.
26   */
27  public class HeapChannelBufferFactory extends AbstractChannelBufferFactory {
28  
29      private static final HeapChannelBufferFactory INSTANCE_BE =
30          new HeapChannelBufferFactory(ByteOrder.BIG_ENDIAN);
31  
32      private static final HeapChannelBufferFactory INSTANCE_LE =
33          new HeapChannelBufferFactory(ByteOrder.LITTLE_ENDIAN);
34  
35      public static ChannelBufferFactory getInstance() {
36          return INSTANCE_BE;
37      }
38  
39      public static ChannelBufferFactory getInstance(ByteOrder endianness) {
40          if (endianness == ByteOrder.BIG_ENDIAN) {
41              return INSTANCE_BE;
42          } else if (endianness == ByteOrder.LITTLE_ENDIAN) {
43              return INSTANCE_LE;
44          } else if (endianness == null) {
45              throw new NullPointerException("endianness");
46          } else {
47              throw new IllegalStateException("Should not reach here");
48          }
49      }
50  
51      /**
52       * Creates a new factory whose default {@link ByteOrder} is
53       * {@link ByteOrder#BIG_ENDIAN}.
54       */
55      public HeapChannelBufferFactory() {
56          super();
57      }
58  
59      /**
60       * Creates a new factory with the specified default {@link ByteOrder}.
61       *
62       * @param defaultOrder the default {@link ByteOrder} of this factory
63       */
64      public HeapChannelBufferFactory(ByteOrder defaultOrder) {
65          super(defaultOrder);
66      }
67  
68      public ChannelBuffer getBuffer(ByteOrder order, int capacity) {
69          return ChannelBuffers.buffer(order, capacity);
70      }
71  
72      public ChannelBuffer getBuffer(ByteOrder order, byte[] array, int offset, int length) {
73          return ChannelBuffers.wrappedBuffer(order, array, offset, length);
74      }
75  
76      public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
77          if (nioBuffer.hasArray()) {
78              return ChannelBuffers.wrappedBuffer(nioBuffer);
79          }
80  
81          ChannelBuffer buf = getBuffer(nioBuffer.order(), nioBuffer.remaining());
82          int pos = nioBuffer.position();
83          buf.writeBytes(nioBuffer);
84          nioBuffer.position(pos);
85          return buf;
86      }
87  }