📄 bufferfactory.java
字号:
/****************************************************************************
* Package : com.ecSolutions.ecAppServer.server.buffer
* File : BufferFactory.java
* Create Date : 2007-7-20
* Author : Steven Chen
*
* Copyright(C) 2006 ecSolutions(shanghai) Co.,Limited.All Rights Reserved.
*
***************************************************************************/
package com.ecSolutions.ecAppServer.server.buffer;
import java.nio.ByteBuffer;
import com.ecSolutions.ecAppServer.server.Buffer;
import com.ecSolutions.ecAppServer.server.util.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Buffer factory.
*
* @author Steven Chen
* @version $id$
*/
public class BufferFactory {
private static final Log log = LogFactory.getLog(BufferFactory.class);
/**
* Wrap a byte array into a buffer.
*
* @param array
* byte array
* @return the new buffer
*/
public static Buffer wrap(byte[] array) {
return ByteArrayBuffer.wrap(array);
}
/**
* Wrap a byte array into a buffer.
*
* @param array
* byte array
* @param offset
* the offset of the subarray to be used
* @param length
* the length of the subarray to be used
* @return the new buffer
*/
public static Buffer wrap(byte[] array, int offset, int length) {
return ByteArrayBuffer.wrap(array, offset, length);
}
/**
* Wrap a byte buffer into a buffer.
*
* @param buffer
* byte buffer
* @return the new buffer
*/
public static Buffer wrap(ByteBuffer buffer) {
return ByteBufferBuffer.wrap(buffer);
}
/**
* Wrap a buffer array into a single buffer.
*
* @param buffers
* buffer array
* @return the new buffer
*/
public static Buffer wrap(Buffer[] buffers) {
return new LinkedBuffer(buffers);
}
private static final BufferPool pool;
private static final boolean useDirectBuffer = Configuration
.isUseDirectBuffer();
static {
String className = Configuration.getBufferPool();
BufferPool tempPool = null;
if (className != null)
try {
tempPool = (BufferPool) Class.forName(className).newInstance();
} catch (Exception e) {
log.error(e);
}
pool = tempPool == null ? new DefaultBufferPool() : tempPool;
}
/**
* Allocate a new buffer. The contents of the buffer are uninitialized, they
* will generally be garbage.
*
* @param capacity
* allocate capacity
* @return the new buffer
*/
public static Buffer allocate(int capacity) {
return allocate(capacity, useDirectBuffer);
}
/**
* Allocate a new buffer. The contents of the buffer are uninitialized, they
* will generally be garbage.
*
* @param capacity
* allocate capacity
* @param direct
* allocate direct buffer
* @return the new buffer
*/
public static Buffer allocate(int capacity, boolean direct) {
if (capacity < 0)
throw new IllegalArgumentException();
return pool.allocate(capacity, direct);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -