/*
* 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.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 The Apache MINA Project (dev@mina.apache.org)
* @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
* @see IoBufferAllocator
*/
public abstract class AbstractIoBuffer extends IoBuffer {
private final IoBufferAllocator allocator;
private final boolean derived;
private boolean autoExpand;
private boolean autoShrink;
private boolean recapacityAllowed = true;
private int minimumCapacity;
/**
* 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.
*/
protected AbstractIoBuffer(
IoBufferAllocator allocator, int initialCapacity) {
this.allocator = allocator;
this.recapacityAllowed = true;
this.derived = false;
this.minimumCapacity = initialCapacity;
}
/**
* Creates a new derived buffer.
*/
protected AbstractIoBuffer(AbstractIoBuffer parent) {
this.allocator = parent.allocator;
this.recapacityAllowed = false;
this.derived = true;
this.minimumCapacity = parent.minimumCapacity;
}
@Override
public final boolean isDirect() {
return buf().isDirect();
}
@Override
public final boolean isReadOnly() {
return buf().isReadOnly();
}
/**
* Sets the underlying NIO buffer instance.
*/
protected abstract void buf(ByteBuffer newBuf);
@Override
public final int minimumCapacity() {
return minimumCapacity;
}
@Override
public final IoBuffer minimumCapacity(int minimumCapacity) {
if (minimumCapacity < 0) {
throw new IllegalArgumentException(
"minimumCapacity: " + minimumCapacity);
}
this.minimumCapacity = minimumCapacity;
return this;
}
@Override
public final int capacity() {
return buf().capacity();
}
@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 =
allocator.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;
}
@Override
public final boolean isAutoExpand() {
return autoExpand && recapacityAllowed;
}
@Override
public final boolean isAutoShrink() {
return autoShrink && recapacityAllowed;
}
@Override
public final boolean isDerived() {
return derived;
}
@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;
}
@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;
}
@Override
public final IoBuffer expand(int expectedRemaining) {
return expand(position(), expectedRemaining, false);
}
private IoBuffer expand(int expectedRemaining, boolean autoExpand) {
return expand(position(), expectedRemaining, autoExpand);
}
@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) {
newCapacity = IoBuffer.normalizeCapacity(end);
} else {
newCapacity = end;
}
if (newCapacity > capacity()) {
// The buffer needs expansion.
capacity(newCapacity);
}
if (end > limit()) {
// We call limit() directly to prevent StackOverflowError
buf().limit(end);
}
return this;
}
@Override
public final IoBuffer shrink() {
if (!recapacityAllowed) {
throw new IllegalStateException(
"Derived buffers and their parent can't be expanded.");
}
int position = position();
int capacity = capacity();
int limit = limit();
if (capacity == limit) {
return this;
}
int newCapacity = capacity;
int minCapacity = Math.max(minimumCapacity, limit);
for (;;) {
if (newCapacity >>> 1 < minCapacity) {
break;
}
newCapacity >>>= 1;
}
newCapacity = Math.max(minCapacity, newCapacity);
if (newCapacity == capacity) {
return this;
}
// Shrink and compact:
//// Save the state.
ByteOrder bo = order();
//// Reallocate.
ByteBuffer oldBuf = buf();
没有合适的资源?快使用搜索试试~ 我知道了~
mina-2.0.0-M3.zip
共2115个文件
html:1575个
java:484个
jar:20个
5星 · 超过95%的资源 需积分: 4 94 下载量 22 浏览量
2008-10-29
13:09:34
上传
评论
收藏 6.56MB ZIP 举报
温馨提示
mina-2.0.0-M3.zip 源码,文档,jar包 全部包括
资源推荐
资源详情
资源评论
收起资源包目录
mina-2.0.0-M3.zip (2115个子文件)
bogus.cert 937B
keystore.cert 937B
stylesheet.css 2KB
stylesheet.css 2KB
stylesheet.css 1KB
inherit.gif 57B
index-all.html 1.41MB
IoSession.html 313KB
IoBufferWrapper.html 256KB
AbstractIoBuffer.html 256KB
IoBuffer.html 219KB
IoBuffer.html 216KB
AbstractIoBuffer.html 190KB
IoFilter.NextFilter.html 154KB
IoBuffer.html 154KB
AbstractIoSession.html 125KB
AbstractIoSession.html 115KB
ObjectMBean.html 103KB
IoFilter.html 101KB
overview-tree.html 99KB
WriteRequest.html 96KB
DefaultIoFilterChain.html 95KB
DefaultIoFilterChain.html 90KB
AbstractPollingIoProcessor.html 89KB
ObjectMBean.html 87KB
IoSession.html 86KB
IoBufferTest.html 85KB
ExecutorFilter.html 83KB
SslFilter.html 81KB
IoBufferWrapper.html 77KB
NioSocketConnector.html 77KB
NioSocketAcceptor.html 75KB
NioDatagramConnector.html 75KB
SslFilter.html 74KB
NioDatagramAcceptor.html 72KB
AprSocketAcceptor.html 72KB
AprSocketConnector.html 72KB
package-use.html 71KB
AbstractPollingIoConnector.html 71KB
ProtocolDecoderOutput.html 70KB
LoggingFilter.html 68KB
AbstractPollingIoAcceptor.html 68KB
SslHandler.html 66KB
IoFilterChain.html 66KB
KeepAliveFilter.html 65KB
IoFilterAdapter.html 65KB
AbstractIoAcceptor.html 65KB
allclasses-frame.html 64KB
ReferenceCountingFilter.html 63KB
AbstractIoService.html 62KB
AbstractPollingConnectionlessIoAcceptor.html 60KB
NioProcessor.html 59KB
AbstractPollingIoConnector.html 59KB
AbstractIoService.html 59KB
ProfilerTimerFilter.html 59KB
AprIoProcessor.html 58KB
AbstractIoConnector.html 58KB
AbstractPollingIoProcessor.html 58KB
SslContextFactory.html 57KB
ConnectFuture.html 57KB
AbstractPollingIoAcceptor.html 57KB
IdleStatus.html 57KB
OrderedThreadPoolExecutor.html 57KB
IoSession.html 57KB
UnorderedThreadPoolExecutor.html 56KB
IoFilter.html 56KB
AbstractPollingConnectionlessIoAcceptor.html 56KB
DefaultIoFilterChainBuilder.html 56KB
IoProcessor.html 55KB
allclasses-frame.html 54KB
DecodingState.html 53KB
package-use.html 53KB
ProfilerTimerFilter.html 53KB
DemuxingIoHandler.html 53KB
OrderedThreadPoolExecutor.html 52KB
DefaultIoFilterChainBuilder.html 51KB
DefaultSocketSessionConfig.html 51KB
AbstractStreamWriteFilterTest.html 51KB
ProtocolCodecFilter.html 51KB
IoFilterChain.html 50KB
RequestResponseFilter.html 49KB
KeepAliveFilter.html 48KB
allclasses-noframe.html 48KB
ExecutorFilter.html 47KB
CommonEventFilter.html 47KB
AbstractIoSessionConfig.html 47KB
package-use.html 46KB
MdcInjectionFilterTest.html 45KB
IoSessionConfig.html 45KB
VmPipeAcceptor.html 45KB
UnorderedThreadPoolExecutor.html 45KB
SslContextFactory.html 45KB
IoFuture.html 45KB
BlacklistFilter.html 44KB
ProtocolCodecFilter.html 44KB
CircularQueue.html 44KB
ExpiringMap.html 44KB
CompressionFilter.html 43KB
ObjectSerializationInputStream.html 43KB
IoAcceptor.html 43KB
共 2115 条
- 1
- 2
- 3
- 4
- 5
- 6
- 22
资源评论
- 西日草2013-08-24非常不错的框架,正在学习,感谢分享
- 石头0_02014-05-26非常齐全,谢谢
hgd250
- 粉丝: 43
- 资源: 39
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功