netty5/buffer/src/main/java/io/netty/buffer/HeapChannelBufferFactory.java
Trustin Lee 8663716d38 Issue #60: Make the project multi-module
Split the project into the following modules:
* common
* buffer
* codec
* codec-http
* transport
* transport-*
* handler
* example
* testsuite (integration tests that involve 2+ modules)
* all (does nothing yet, but will make it generate netty.jar)

This commit also fixes the compilation errors with transport-sctp on
non-Linux systems.  It will at least compile without complaints.
2011-12-28 19:44:04 +09:00

90 lines
3.0 KiB
Java

/*
* Copyright 2011 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* A {@link ChannelBufferFactory} which merely allocates a heap buffer with
* the specified capacity. {@link HeapChannelBufferFactory} should perform
* very well in most situations because it relies on the JVM garbage collector,
* which is highly optimized for heap allocation.
*/
public class HeapChannelBufferFactory extends AbstractChannelBufferFactory {
private static final HeapChannelBufferFactory INSTANCE_BE =
new HeapChannelBufferFactory(ByteOrder.BIG_ENDIAN);
private static final HeapChannelBufferFactory INSTANCE_LE =
new HeapChannelBufferFactory(ByteOrder.LITTLE_ENDIAN);
public static ChannelBufferFactory getInstance() {
return INSTANCE_BE;
}
public static ChannelBufferFactory getInstance(ByteOrder endianness) {
if (endianness == ByteOrder.BIG_ENDIAN) {
return INSTANCE_BE;
} else if (endianness == ByteOrder.LITTLE_ENDIAN) {
return INSTANCE_LE;
} else if (endianness == null) {
throw new NullPointerException("endianness");
} else {
throw new IllegalStateException("Should not reach here");
}
}
/**
* Creates a new factory whose default {@link ByteOrder} is
* {@link ByteOrder#BIG_ENDIAN}.
*/
public HeapChannelBufferFactory() {
}
/**
* Creates a new factory with the specified default {@link ByteOrder}.
*
* @param defaultOrder the default {@link ByteOrder} of this factory
*/
public HeapChannelBufferFactory(ByteOrder defaultOrder) {
super(defaultOrder);
}
@Override
public ChannelBuffer getBuffer(ByteOrder order, int capacity) {
return ChannelBuffers.buffer(order, capacity);
}
@Override
public ChannelBuffer getBuffer(ByteOrder order, byte[] array, int offset, int length) {
return ChannelBuffers.wrappedBuffer(order, array, offset, length);
}
@Override
public ChannelBuffer getBuffer(ByteBuffer nioBuffer) {
if (nioBuffer.hasArray()) {
return ChannelBuffers.wrappedBuffer(nioBuffer);
}
ChannelBuffer buf = getBuffer(nioBuffer.order(), nioBuffer.remaining());
int pos = nioBuffer.position();
buf.writeBytes(nioBuffer);
nioBuffer.position(pos);
return buf;
}
}