/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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 org.apache.mina.common;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.HashSet;
import java.util.Set;
import org.apache.mina.common.support.ByteBufferHexDumper;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
/**
* A byte buffer used by MINA applications.
* <p>
* This is a replacement for {@link java.nio.ByteBuffer}. Please refer to
* {@link java.nio.ByteBuffer} and {@link java.nio.Buffer} documentation for
* usage. MINA does not use NIO {@link java.nio.ByteBuffer} directly for two
* reasons:
* <ul>
* <li>It doesn't provide useful getters and putters such as
* <code>fill</code>, <code>get/putString</code>, and
* <code>get/putAsciiInt()</code> enough.</li>
* <li>It is hard to distinguish if the buffer is created from MINA buffer
* pool or not. MINA have to return used buffers back to pool.</li>
* <li>It is difficult to write variable-length data due to its fixed
* capacity</li>
* </ul>
* </p>
*
* <h2>Allocation</h2>
* <p>
* You can get a heap buffer from buffer pool:
* <pre>
* ByteBuffer buf = ByteBuffer.allocate(1024, false);
* </pre>
* you can also get a direct buffer from buffer pool:
* <pre>
* ByteBuffer buf = ByteBuffer.allocate(1024, true);
* </pre>
* or you can let MINA choose:
* <pre>
* ByteBuffer buf = ByteBuffer.allocate(1024);
* </pre>
* </p>
*
* <h2>Acquire/Release</h2>
* <p>
* <b>Please note that you never need to release the allocated buffer</b>
* because MINA will release it automatically when:
* <ul>
* <li>You pass the buffer by calling {@link IoSession#write(Object)}.</li>
* <li>You pass the buffer by calling {@link IoFilter.NextFilter#filterWrite(IoSession,IoFilter.WriteRequest)}.</li>
* <li>You pass the buffer by calling {@link ProtocolEncoderOutput#write(ByteBuffer)}.</li>
* </ul>
* And, you don't need to release any {@link ByteBuffer} which is passed as a parameter
* of {@link IoHandler#messageReceived(IoSession, Object)} method. They are released
* automatically when the method returns.
* <p>
* You have to release buffers manually by calling {@link #release()} when:
* <ul>
* <li>You allocated a buffer, but didn't pass the buffer to any of two methods above.</li>
* <li>You called {@link #acquire()} to prevent the buffer from being released.</li>
* </ul>
* </p>
*
* <h2>Wrapping existing NIO buffers and arrays</h2>
* <p>
* This class provides a few <tt>wrap(...)</tt> methods that wraps
* any NIO buffers and byte arrays. Wrapped MINA buffers are not returned
* to the buffer pool by default to prevent unexpected memory leakage by default.
* In case you want to make it pooled, you can call {@link #setPooled(boolean)}
* with <tt>true</tt> flag to enable pooling.
*
* <h2>AutoExpand</h2>
* <p>
* Writing variable-length data using NIO <tt>ByteBuffers</tt> is not really
* easy, and it is because its size is fixed. MINA <tt>ByteBuffer</tt>
* introduces <tt>autoExpand</tt> property. If <tt>autoExpand</tt> property
* is true, you never get {@link BufferOverflowException} or
* {@link IndexOutOfBoundsException} (except when index is negative).
* It automatically expands its capacity and limit value. For example:
* <pre>
* String greeting = messageBundle.getMessage( "hello" );
* ByteBuffer buf = ByteBuffer.allocate( 16 );
* // Turn on autoExpand (it is off by default)
* buf.setAutoExpand( true );
* buf.putString( greeting, utf8encoder );
* </pre>
* NIO <tt>ByteBuffer</tt> is reallocated by MINA <tt>ByteBuffer</tt> behind
* the scene if the encoded data is larger than 16 bytes. Its capacity and
* its limit will increase to the last position the string is written.
* </p>
*
* <h2>Derived Buffers</h2>
* <p>
* Derived buffers are the buffers which were created by
* {@link #duplicate()}, {@link #slice()}, or {@link #asReadOnlyBuffer()}.
* They are useful especially when you broadcast the same messages to
* multiple {@link IoSession}s. Please note that the derived buffers are
* neither pooled nor auto-expandable. Trying to expand a derived buffer will
* raise {@link IllegalStateException}.
* </p>
*
* <h2>Changing Buffer Allocation and Management Policy</h2>
* <p>
* MINA provides a {@link ByteBufferAllocator} interface to let you override
* the default buffer management behavior. There are two allocators provided
* out-of-the-box:
* <ul>
* <li>{@link PooledByteBufferAllocator} (Default)</li>
* <li>{@link SimpleByteBufferAllocator}</li>
* </ul>
* You can change the allocator by calling {@link #setAllocator(ByteBufferAllocator)}.
* </p>
*
* @author The Apache Directory Project (mina-dev@directory.apache.org)
* @version $Rev: 637706 $, $Date: 2008-03-17 12:18:43 +0900 (Mon, 17 Mar 2008) $
* @noinspection StaticNonFinalField
* @see ByteBufferAllocator
*/
public abstract class ByteBuffer implements Comparable<ByteBuffer> {
private static ByteBufferAllocator allocator = new PooledByteBufferAllocator();
private static boolean useDirectBuffers = true;
/**
* Returns the current allocator which manages the allocated buffers.
*/
public static ByteBufferAllocator getAllocator() {
return allocator;
}
/**
* Changes the current allocator with the specified one to manage
* the allocated buffers from now.
*/
public static void setAllocator(ByteBufferAllocator newAllocator) {
if (newAllocator == null) {
throw new NullPointerException("allocator");
}
ByteBufferAllocator oldAllocator = allocator;
allocator = newAllocator;
if (null != oldAllocator) {
oldAllocator.dispose();
}
}
public static boolean isUseDirectBuffers() {
return useDirectBuffers;
}
public static void setUseDirectBuffers(boolean useDirectBuffers) {
ByteBuffer.useDirectBuffers = useDirectBuffers;
}
/**
* Returns the direct or heap buffer which is capable of the specified
* size. This method tries to allocate direct buffer first, and then
* tries heap buffer if direct buffer memory is exhausted. Please use
* {@link #allocate(int, boolean)} to allocate buffers of specific type.
*
* @param capacity the capacity of the buffer
*/
public static ByteBuffer allocate(int capacity) {
if (useDirectBuffers) {
try {
// first try to allocate direct buffer
return allocate(capacity, true);
} catch (OutOfMemoryError e) {
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
mina框架介绍以及JAR包 (1202个子文件)
535f854c-f081-48c9-acd2-6a8a689bcecf.bmp 16KB
bogus.cert 937B
keystore.cert 937B
blog.css 40KB
SyntaxHighlighter.css 2KB
blue.css 2KB
stylesheet.css 2KB
stylesheet.css 2KB
stylesheet.css 1KB
8d386387-6e82-461e-af37-1369abbb6c6a-thumb.gif 3KB
2829efcb-9f70-4891-bfa5-000e11c1e42a-thumb.gif 3KB
reply_twitter.gif 2KB
user-logo-thumb.gif 863B
rss_zhuaxia.gif 723B
rss_google.gif 701B
offline.gif 682B
chat.gif 649B
icon_minigender_1.gif 143B
inherit.gif 57B
nio socket 及其开源框架MINA学习总结(一) - 李哥在javaeye - JavaEye技术网站.htm 81KB
index-all.html 554KB
ByteBuffer.html 173KB
IoSession.html 172KB
ByteBufferProxy.html 160KB
ByteBuffer.html 146KB
ByteBuffer.html 94KB
IoFilter.NextFilter.html 89KB
AbstractIoFilterChain.html 73KB
SSLHandler.html 72KB
SSLFilter.html 70KB
SSLFilter.html 67KB
package-use.html 64KB
DatagramAcceptorDelegate.html 64KB
ByteBufferTest.html 62KB
SSLContextFactoryBean.html 60KB
DatagramConnectorDelegate.html 58KB
IoFilterAdapter.html 57KB
ReferenceCountingIoFilter.html 56KB
IoSession.html 55KB
StreamWriteFilterTest.html 53KB
PooledByteBufferAllocator.html 51KB
overview-tree.html 51KB
ByteBufferProxy.html 50KB
SocketIoProcessor.html 49KB
IoFilter.html 49KB
DemuxingProtocolCodecFactory.html 48KB
IoFilterChain.html 46KB
ExecutorFilter.html 46KB
SocketAcceptor.html 46KB
SocketConnector.html 45KB
BaseIoSession.html 44KB
ObjectSerializationInputStream.html 43KB
LoggingFilter.html 43KB
ObjectSerializationOutputStream.html 42KB
ProtocolCodecFilter.html 42KB
ProtocolCodecFilter.html 42KB
ExpiringMap.html 42KB
IoFilter.html 42KB
CompressionFilter.html 41KB
IoSessionManager.html 41KB
IoFilterChainTest.html 41KB
IoHandler.html 39KB
BlacklistFilter.html 39KB
SSLContextFactoryBean.html 38KB
SocketSessionImpl.html 38KB
IoServiceConfig.html 38KB
IoHandlerChain.html 36KB
DefaultIoFilterChainBuilder.html 36KB
SocketSessionConfigImpl.html 35KB
allclasses-frame.html 35KB
TextLineDecoderTest.html 35KB
IoFilter.html 35KB
IoHandlerChain.html 35KB
ExpiringMap.html 34KB
BaseByteBuffer.html 33KB
IoSession.html 33KB
SingleSessionIoHandlerDelegate.html 32KB
StatCollector.html 32KB
TextLineDecoder.html 32KB
ExecutorFilter.html 32KB
VmPipeFilterChain.html 31KB
DatagramSessionImpl.html 31KB
IdentityHashSet.html 31KB
IoFilterChain.html 31KB
DefaultIoFilterChainBuilder.html 30KB
StreamWriteFilter.html 30KB
IoServiceListenerSupportTest.html 30KB
IdleStatus.html 30KB
ThreadPoolExecutorFactoryBean.html 30KB
SocketSessionConfigImpl.html 30KB
DemuxingIoHandler.html 29KB
SingleSessionIoHandlerAdapter.html 29KB
CompressionFilter.html 29KB
KeyStoreFactoryBean.html 29KB
SwingChatClient.html 29KB
IoHandlerAdapter.html 28KB
SessionLog.html 28KB
IoServiceManager.html 28KB
DefaultIoFilterChainBuilderFactoryBean.html 27KB
package-use.html 27KB
共 1202 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
liwei5365
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Wireshark-4.2.4-x64.zip
- 2022年11月软件设计师上
- 基于VB+ACCESS教学管理系统(参考文献+源代码).zip
- 一个工具的流程图 demo
- EMC3080的用于连接FogCloud的固件
- 基于VB+Access酒店客房管理系统(源代码+参考文献+报告).zip
- 应用笔记LAT1244+奇怪的NRST+管脚异常复位问题
- SEMI标准的解释说明
- tensorflow-2.8.2-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-rocm-2.12.0.560-cp311-cp311-manylinux2014-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论16