/*
* 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.core.buffer;
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.Serializable;
import java.io.StreamCorruptedException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
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.EnumSet;
import java.util.Set;
/**
* A base implementation of {@link IoBuffer}. This implementation
* assumes that {@link IoBuffer#buf()} always returns a correct NIO
* {@link ByteBuffer} instance. Most implementations could
* extend this class and implement their own buffer management mechanism.
*
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
* @see IoBufferAllocator
*/
public abstract class AbstractIoBuffer extends IoBuffer {
/** Tells if a buffer has been created from an existing buffer */
private final boolean derived;
/** A flag set to true if the buffer can extend automatically */
private boolean autoExpand;
/** A flag set to true if the buffer can shrink automatically */
private boolean autoShrink;
/** Tells if a buffer can be expanded */
private boolean recapacityAllowed = true;
/** The minimum number of bytes the IoBuffer can hold */
private int minimumCapacity;
/** A mask for a byte */
private static final long BYTE_MASK = 0xFFL;
/** A mask for a short */
private static final long SHORT_MASK = 0xFFFFL;
/** A mask for an int */
private static final long INT_MASK = 0xFFFFFFFFL;
/**
* We don't have any access to Buffer.markValue(), so we need to track it down,
* which will cause small extra overhead.
*/
private int mark = -1;
/**
* Creates a new parent buffer.
*
* @param allocator The allocator to use to create new buffers
* @param initialCapacity The initial buffer capacity when created
*/
protected AbstractIoBuffer(IoBufferAllocator allocator, int initialCapacity) {
setAllocator(allocator);
this.recapacityAllowed = true;
this.derived = false;
this.minimumCapacity = initialCapacity;
}
/**
* Creates a new derived buffer. A derived buffer uses an existing
* buffer properties - the allocator and capacity -.
*
* @param parent The buffer we get the properties from
*/
protected AbstractIoBuffer(AbstractIoBuffer parent) {
setAllocator(parent.getAllocator());
this.recapacityAllowed = false;
this.derived = true;
this.minimumCapacity = parent.minimumCapacity;
}
/**
* {@inheritDoc}
*/
@Override
public final boolean isDirect() {
return buf().isDirect();
}
/**
* {@inheritDoc}
*/
@Override
public final boolean isReadOnly() {
return buf().isReadOnly();
}
/**
* Sets the underlying NIO buffer instance.
*
* @param newBuf The buffer to store within this IoBuffer
*/
protected abstract void buf(ByteBuffer newBuf);
/**
* {@inheritDoc}
*/
@Override
public final int minimumCapacity() {
return minimumCapacity;
}
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer minimumCapacity(int minimumCapacity) {
if (minimumCapacity < 0) {
throw new IllegalArgumentException("minimumCapacity: "
+ minimumCapacity);
}
this.minimumCapacity = minimumCapacity;
return this;
}
/**
* {@inheritDoc}
*/
@Override
public final int capacity() {
return buf().capacity();
}
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer capacity(int newCapacity) {
if (!recapacityAllowed) {
throw new IllegalStateException(
"Derived buffers and their parent can't be expanded.");
}
// Allocate a new buffer and transfer all settings to it.
if (newCapacity > capacity()) {
// Expand:
//// Save the state.
int pos = position();
int limit = limit();
ByteOrder bo = order();
//// Reallocate.
ByteBuffer oldBuf = buf();
ByteBuffer newBuf = getAllocator().allocateNioBuffer(newCapacity,
isDirect());
oldBuf.clear();
newBuf.put(oldBuf);
buf(newBuf);
//// Restore the state.
buf().limit(limit);
if (mark >= 0) {
buf().position(mark);
buf().mark();
}
buf().position(pos);
buf().order(bo);
}
return this;
}
/**
* {@inheritDoc}
*/
@Override
public final boolean isAutoExpand() {
return autoExpand && recapacityAllowed;
}
/**
* {@inheritDoc}
*/
@Override
public final boolean isAutoShrink() {
return autoShrink && recapacityAllowed;
}
/**
* {@inheritDoc}
*/
@Override
public final boolean isDerived() {
return derived;
}
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer setAutoExpand(boolean autoExpand) {
if (!recapacityAllowed) {
throw new IllegalStateException(
"Derived buffers and their parent can't be expanded.");
}
this.autoExpand = autoExpand;
return this;
}
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer setAutoShrink(boolean autoShrink) {
if (!recapacityAllowed) {
throw new IllegalStateException(
"Derived buffers and their parent can't be shrinked.");
}
this.autoShrink = autoShrink;
return this;
}
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer expand(int expectedRemaining) {
return expand(position(), expectedRemaining, false);
}
private IoBuffer expand(int expectedRemaining, boolean autoExpand) {
return expand(position(), expectedRemaining, autoExpand);
}
/**
* {@inheritDoc}
*/
@Override
public final IoBuffer expand(int pos, int expectedRemaining) {
return expand(pos, expectedRemaining, false);
}
private IoBuffer expand(int pos, int expectedRemaining, boolean autoExpand) {
if (!recapacityAllowed) {
throw new IllegalStateException(
"Derived buffers and their parent can't be expanded.");
}
int end = pos + expectedRemaining;
int newCapacity;
if (autoExpand) {
ne
没有合适的资源?快使用搜索试试~ 我知道了~
apache下的mina框架的源码
共2000个文件
html:2127个
java:565个
xml:16个
4星 · 超过85%的资源 需积分: 10 12 下载量 81 浏览量
2012-11-29
13:41:05
上传
评论
收藏 6.84MB ZIP 举报
温馨提示
可以使用 TCP/IP、UDP/IP、串口和虚拟机内部的管道等传输方式,是一个开发高性能和高可伸缩性网络应用程序的网络应用框架的源码.
资源推荐
资源详情
资源评论
收起资源包目录
apache下的mina框架的源码 (2000个子文件)
stylesheet.css 2KB
stylesheet.css 2KB
stylesheet.css 1KB
index-all.html 1.84MB
IoSession.html 440KB
AbstractIoBuffer.html 312KB
IoBuffer.html 306KB
IoBufferWrapper.html 294KB
AbstractIoBuffer.html 277KB
IoBuffer.html 246KB
IoBuffer.html 213KB
IoFilter.NextFilter.html 207KB
AbstractIoSession.html 167KB
IoBufferTest.html 146KB
overview-tree.html 143KB
AbstractPollingIoProcessor.html 142KB
AbstractIoSession.html 132KB
CompositeByteArray.html 132KB
ObjectMBean.html 125KB
DefaultIoFilterChain.html 115KB
IoBufferWrapper.html 114KB
WriteRequest.html 108KB
ProfilerTimerFilter.html 108KB
IoFilter.html 105KB
GraphicalCharGenClient.html 103KB
SwingChatClient.html 102KB
SslFilter.html 101KB
SslHandler.html 97KB
package-use.html 96KB
OrderedThreadPoolExecutor.html 94KB
ConnectDialog.html 92KB
ImagePanel.html 92KB
ExecutorFilter.html 91KB
ClientPanel.html 90KB
ObjectMBean.html 89KB
constant-values.html 88KB
DefaultIoFilterChain.html 87KB
IoSession.html 86KB
AbstractPollingIoAcceptor.html 85KB
SslFilter.html 84KB
NioSocketConnector.html 83KB
ExecutorFilter.html 83KB
AbstractPollingConnectionlessIoAcceptor.html 81KB
NioDatagramConnector.html 80KB
ProtocolDecoderOutput.html 78KB
allclasses-frame.html 77KB
AprSocketConnector.html 77KB
ProtocolCodecFilter.html 76KB
AbstractPollingIoConnector.html 76KB
NioDatagramAcceptor.html 74KB
NioSocketAcceptor.html 74KB
AprSocketAcceptor.html 72KB
allclasses-frame.html 72KB
AbstractPollingIoAcceptor.html 72KB
IoSession.html 71KB
AbstractPollingIoConnector.html 71KB
IdleStatus.html 70KB
package-use.html 70KB
package-use.html 69KB
AbstractIoService.html 69KB
DefaultIoFilterChainBuilder.html 69KB
allclasses-noframe.html 68KB
AbstractStreamWriteFilterTest.html 68KB
IoProcessor.html 67KB
ReferenceCountingFilter.html 67KB
MdcInjectionFilterTest.html 67KB
NioProcessor.html 65KB
KeepAliveFilter.html 65KB
ConnectFuture.html 65KB
AbstractIoAcceptor.html 64KB
AbstractPollingIoProcessor.html 64KB
NTLMUtilities.html 62KB
OrderedThreadPoolExecutor.html 61KB
IoFilterChain.html 61KB
AbstractIoService.html 61KB
AbstractPollingConnectionlessIoAcceptor.html 61KB
AprIoProcessor.html 61KB
IoFilterAdapter.html 60KB
Base64.html 60KB
Socks5LogicHandler.html 60KB
ByteAccessTest.html 60KB
serialized-form.html 59KB
SslContextFactory.html 59KB
StateMachineFactory.html 58KB
AbstractIoConnector.html 58KB
KeepAliveFilter.html 57KB
UnorderedThreadPoolExecutor.html 57KB
LoggingFilter.html 57KB
AprIoProcessor.html 57KB
BufferByteArray.html 56KB
UnorderedThreadPoolExecutor.html 56KB
IoFilterChain.html 56KB
AbstractIoAcceptor.html 55KB
TextLineDecoder.html 54KB
SslContextFactory.html 54KB
ProfilerTimerFilter.html 54KB
IoFilter.html 54KB
DecodingState.html 53KB
ExpiringMap.html 53KB
AbstractHttpLogicHandler.html 53KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- BowenSun2013-01-28谢谢楼主源码,有木有中文帮助文档也分享一下吧
changzhen1990
- 粉丝: 1
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功