View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
28   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
29   * @version $Rev: 2197 $, $Date: 2010-02-23 09:43:15 +0900 (Tue, 23 Feb 2010) $
30   */
31  public class HeapChannelBufferFactory extends AbstractChannelBufferFactory {
32  
33      private static final HeapChannelBufferFactory INSTANCE_BE =
34          new HeapChannelBufferFactory(ByteOrder.BIG_ENDIAN);
35  
36      private static final HeapChannelBufferFactory INSTANCE_LE =
37          new HeapChannelBufferFactory(ByteOrder.LITTLE_ENDIAN);
38  
39      public static ChannelBufferFactory getInstance() {
40          return INSTANCE_BE;
41      }
42  
43      public static ChannelBufferFactory getInstance(ByteOrder endianness) {
44          if (endianness == ByteOrder.BIG_ENDIAN) {
45              return INSTANCE_BE;
46          } else if (endianness == ByteOrder.LITTLE_ENDIAN) {
47              return INSTANCE_LE;
48          } else if (endianness == null) {
49              throw new NullPointerException("endianness");
50          } else {
51              throw new IllegalStateException("Should not reach here");
52          }
53      }
54  
55      /**
56       * Creates a new factory whose default {@link ByteOrder} is
57       * {@link ByteOrder#BIG_ENDIAN}.
58       */
59      public HeapChannelBufferFactory() {
60          super();
61      }
62  
63      /**
64       * Creates a new factory with the specified default {@link ByteOrder}.
65       *
66       * @param defaultOrder the default {@link ByteOrder} of this factory
67       */
68      public HeapChannelBufferFactory(ByteOrder defaultOrder) {
69          super(defaultOrder);
70      }
71  
72      public ChannelBuffer getBuffer(ByteOrder order, int capacity) {
73          return ChannelBuffers.buffer(order, capacity);
74      }
75  
76      public ChannelBuffer getBuffer(ByteOrder order, byte[] array, int offset, int length) {
77          return ChannelBuffers.wrappedBuffer(order, array, offset, length);
78      }
79  
80      public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
81          if (nioBuffer.hasArray()) {
82              return ChannelBuffers.wrappedBuffer(nioBuffer);
83          }
84  
85          ChannelBuffer buf = getBuffer(nioBuffer.order(), nioBuffer.remaining());
86          int pos = nioBuffer.position();
87          buf.writeBytes(nioBuffer);
88          nioBuffer.position(pos);
89          return buf;
90      }
91  }