/*
* 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 <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) {
newCapacity = IoBuffer.normaliz
没有合适的资源?快使用搜索试试~ 我知道了~
mina源代码学习提供下载
需积分: 9 50 下载量 132 浏览量
2010-03-03
21:11:52
上传
评论 1
收藏 7.3MB GZ 举报
温馨提示
共2000个文件
html:1841个
java:555个
jar:20个
mina源代码学习mina源代码学习mina源代码学习mina源代码学习
资源推荐
资源详情
资源评论
收起资源包目录
mina源代码学习提供下载 (2000个子文件)
stylesheet.css 2KB
stylesheet.css 2KB
stylesheet.css 1KB
index-all.html 1.64MB
IoSession.html 331KB
IoBufferWrapper.html 255KB
IoBuffer.html 250KB
AbstractIoBuffer.html 242KB
AbstractIoBuffer.html 226KB
IoBuffer.html 217KB
IoFilter.NextFilter.html 194KB
IoBuffer.html 152KB
AbstractIoSession.html 128KB
AbstractIoSession.html 124KB
overview-tree.html 115KB
CompositeByteArray.html 104KB
WriteRequest.html 104KB
IoFilter.html 103KB
ObjectMBean.html 103KB
AbstractPollingIoProcessor.html 99KB
DefaultIoFilterChain.html 94KB
IoBufferTest.html 88KB
DefaultIoFilterChain.html 87KB
ObjectMBean.html 87KB
ProfilerTimerFilter.html 87KB
IoSession.html 85KB
ExecutorFilter.html 82KB
NioSocketConnector.html 82KB
SslFilter.html 81KB
NioDatagramConnector.html 80KB
IoBufferWrapper.html 77KB
package-use.html 76KB
AprSocketConnector.html 76KB
OrderedThreadPoolExecutor.html 76KB
SslFilter.html 75KB
ExecutorFilter.html 75KB
NioSocketAcceptor.html 74KB
AprSocketAcceptor.html 74KB
NioDatagramAcceptor.html 73KB
allclasses-frame.html 72KB
AbstractPollingIoConnector.html 71KB
SslHandler.html 68KB
ProtocolDecoderOutput.html 68KB
AbstractPollingIoAcceptor.html 68KB
ReferenceCountingFilter.html 67KB
KeepAliveFilter.html 65KB
package-use.html 65KB
ConnectFuture.html 64KB
AbstractIoAcceptor.html 64KB
IoFilterChain.html 64KB
IoProcessor.html 63KB
allclasses-frame.html 63KB
ProtocolCodecFilter.html 63KB
AbstractPollingIoAcceptor.html 62KB
constant-values.html 61KB
AbstractPollingIoConnector.html 61KB
NioProcessor.html 60KB
AbstractPollingConnectionlessIoAcceptor.html 60KB
OrderedThreadPoolExecutor.html 60KB
IoFilterAdapter.html 59KB
AbstractIoService.html 59KB
AbstractPollingIoProcessor.html 59KB
AbstractPollingConnectionlessIoAcceptor.html 59KB
IoSession.html 58KB
IdleStatus.html 58KB
AbstractIoConnector.html 58KB
SslContextFactory.html 57KB
LoggingFilter.html 57KB
AbstractIoService.html 56KB
UnorderedThreadPoolExecutor.html 56KB
AprIoProcessor.html 56KB
DefaultIoFilterChainBuilder.html 56KB
allclasses-noframe.html 55KB
ProfilerTimerFilter.html 54KB
IoFilter.html 54KB
IoFilterChain.html 53KB
DecodingState.html 53KB
AbstractStreamWriteFilterTest.html 52KB
CompositeByteArray.html 52KB
DemuxingIoHandler.html 52KB
DefaultIoFilterChainBuilder.html 51KB
NTLMUtilities.html 51KB
ProxyFilter.html 51KB
package-use.html 49KB
RequestResponseFilter.html 49KB
DefaultSocketSessionConfig.html 49KB
MdcInjectionFilterTest.html 49KB
Socks5LogicHandler.html 48KB
Base64.html 48KB
IoSessionConfig.html 48KB
KeepAliveFilter.html 48KB
AbstractIoSessionConfig.html 47KB
CommonEventFilter.html 47KB
IoFuture.html 46KB
ByteAccessTest.html 46KB
UnorderedThreadPoolExecutor.html 45KB
CircularQueue.html 45KB
SslContextFactory.html 45KB
AbstractIoAcceptor.html 44KB
VmPipeAcceptor.html 44KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
java_开发人员1
- 粉丝: 5
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 第12章spring-mvc自定义类型转换器
- 基于PHP图书管理系统实验报告.docx
- Python爬取淘宝热卖商品并可视化分析
- 5152单片机proteus仿真和源码将按键次数写入AT24C02再读出并用1602LCD显示
- SE-SSD复现过程(Det3D的安装教程)
- 基于Python的在线学习与推荐系统设计与实现(论文+源码)-kaic
- 串口通过 YMODEM 协议进行文件传输
- 蓝桥杯2024年第十五届省赛真题-前缀总分
- com.qihoo.appstore_300101305-1.apk
- tensorflow-gpu-2.7.1-cp37-cp37m-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功