/*
* Copyright 2004-2006 the original author or authors.
*
* Licensed 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 net.sf.cindy.buffer;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.InvalidMarkException;
import java.nio.ReadOnlyBufferException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import net.sf.cindy.Buffer;
import net.sf.cindy.util.Charset;
import net.sf.cindy.util.Configuration;
/**
* Abstract buffer.
*
* @author <a href="chenrui@gmail.com">Roger Chen</a>
* @version $id$
*/
public abstract class AbstractBuffer implements Buffer {
protected static final int WRITE_PACKET_SIZE = Configuration
.getWritePacketSize();
private boolean readonly;
private boolean bigEndian = true;
private boolean permanent;
private boolean released;
private int mark = -1;
private int position = 0;
private int limit;
private int capacity;
private final int offset;
protected AbstractBuffer(int offset, int capacity) {
this.offset = offset;
this.capacity = capacity;
limit(capacity);
}
public boolean isPermanent() {
return permanent;
}
public void setPermanent(boolean b) {
checkReleased();
this.permanent = b;
}
public boolean isReleased() {
return released;
}
public synchronized void release() {
if (!(permanent || released)) {
try {
_release();
} finally {
released = true;
}
}
}
protected abstract void _release();
public boolean isReadonly() {
return readonly;
}
protected Buffer setReadonly(boolean readonly) {
this.readonly = readonly;
return this;
}
public boolean isBigEndian() {
return bigEndian;
}
public Buffer setBigEndian(boolean bigEndian) {
this.bigEndian = bigEndian;
return this;
}
public Buffer skip(int size) {
if (size != 0)
position(position + size);
return this;
}
public int capacity() {
return capacity;
}
protected Buffer capacity(int newCapacity) {
if (newCapacity < 0)
throw new IllegalArgumentException();
capacity = newCapacity;
return limit(Math.min(limit, capacity));
}
public int limit() {
return limit;
}
public Buffer limit(int newLimit) {
if ((newLimit > capacity) || (newLimit < 0))
throw new IllegalArgumentException();
limit = newLimit;
return position(Math.min(position, limit));
}
public int position() {
return position;
}
public Buffer position(int newPosition) {
if ((newPosition > limit) || (newPosition < 0))
throw new IllegalArgumentException();
position = newPosition;
if (mark > position)
mark = -1;
return this;
}
public Buffer mark() {
return mark(position);
}
protected Buffer mark(int mark) {
if (mark < 0)
this.mark = -1;
else {
if (mark > position)
throw new IndexOutOfBoundsException();
this.mark = mark;
}
return this;
}
protected int getMark() {
return mark;
}
protected final int getIndex(int i, int len) {
checkReleased();
if ((len < 0) || (i < 0) || (len > limit - i))
throw new IndexOutOfBoundsException();
return i + offset;
}
protected final int getIndex(int len) {
checkReleased();
if (limit - position < len)
throw new BufferUnderflowException();
int p = position + offset;
position += len;
return p;
}
protected final int putIndex(int i, int len) {
checkReleased();
checkReadonly();
if ((len < 0) || (i < 0) || (len > limit - i))
throw new IndexOutOfBoundsException();
return i + offset;
}
protected final int putIndex(int len) {
checkReleased();
checkReadonly();
if (limit - position < len)
throw new BufferOverflowException();
int p = position + offset;
position += len;
return p;
}
protected final static void checkBounds(int off, int len, int size) {
// all less than 0
if ((off | len | (off + len) | (size - (off + len))) < 0)
throw new IndexOutOfBoundsException();
}
protected final void checkReadonly() {
if (isReadonly())
throw new ReadOnlyBufferException();
}
protected final void checkReleased() {
if (isReleased())
throw new ReleasedBufferException();
}
public Buffer reset() {
if (mark < 0)
throw new InvalidMarkException();
position = mark;
return this;
}
public Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
public Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
public Buffer rewind() {
position = 0;
mark = -1;
return this;
}
public int remaining() {
return limit - position;
}
public boolean hasRemaining() {
return position < limit;
}
public int indexOf(byte[] b) {
if (b.length == 0)
return position;
int startIndex = getIndex(0);
int endIndex = startIndex + remaining() - b.length;
byte first = b[0];
Label: for (int i = startIndex; i <= endIndex; i++) {
// look for the first character
if (_get(i) == first) {
for (int j = 1; j < b.length; j++) {
if (_get(i + j) != b[j])
continue Label;
}
return i - startIndex + position;
}
}
return -1;
}
public int write(WritableByteChannel channel) throws IOException {
int writeCount = 0;
ByteBuffer buffer = asByteBuffer();
if (buffer.remaining() > WRITE_PACKET_SIZE)
buffer.limit(buffer.position() + WRITE_PACKET_SIZE);
try {
writeCount = channel.write(buffer);
} finally {
skip(writeCount);
}
return writeCount;
}
public int read(ReadableByteChannel channel) throws IOException {
int readCount = 0;
try {
readCount = channel.read(asByteBuffer());
} finally {
if (readCount > 0)
skip(readCount);
}
return readCount;
}
public Buffer asReadOnlyBuffer() {
return ((AbstractBuffer) duplicate()).setReadonly(true);
}
public boolean equals(Object obj) {
if (!(obj instanceof Buffer))
return false;
Buffer buffer = (Buffer) obj;
if (this.remaining() != buffer.remaining())
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
导入到myelipse中可以直接查看源文件。Netty、Mina、Cindy都是不错的NIO开源框架,后两者都是在Netty的基础上演化出来的。所以要学习好Java的异步通信框架,这三个都是不可多得的好材料。 本资源仅供学习和参考使用,若要进行开发,请下载相应的Jar包
资源推荐
资源详情
资源评论
收起资源包目录
Cindy 3.0b1 的源码--- Mina的兄弟 (219个子文件)
AbstractBuffer.class 17KB
DelegateBuffer.class 11KB
LinkedBuffer.class 10KB
DefaultReactor.class 10KB
ByteBufferBuffer.class 9KB
AbstractSession.class 8KB
SSLFilter.class 8KB
JmxSession.class 7KB
AbstractChannelSession$ChannelReactorHandler.class 6KB
DefaultBufferPool.class 6KB
DefaultDispatcher.class 6KB
Charset.class 6KB
Configuration.class 6KB
ByteArrayBuffer.class 5KB
SessionFactory.class 5KB
AbstractChannelSession.class 5KB
BlockingSessionAcceptor.class 5KB
DefaultDispatcher$Worker.class 4KB
AbstractSessionAcceptor.class 4KB
SocketChannelSession.class 4KB
ServerSocketChannelSession.class 4KB
DefaultFuture.class 4KB
NonBlockingSessionAcceptor.class 4KB
SocketChannelSession$1.class 4KB
DispatcherFilter.class 3KB
DatagramChannelSession$1.class 3KB
StatisticFilter.class 3KB
DatagramChannelSession.class 3KB
Bits.class 3KB
BufferBuilder.class 3KB
AbstractSessionFilterChain.class 3KB
LogFilter.class 3KB
Buffer.class 3KB
PipeSession$1.class 3KB
AbstractChannelSession$1.class 3KB
DefaultBufferPool$DirectBuffer.class 3KB
BufferFactory.class 2KB
DefaultBufferPool$HeapBuffer.class 2KB
PacketDecoderFilter.class 2KB
Speed.class 2KB
PipeSession.class 2KB
PacketDecoderFilter$CopyPacketDecoderFilter.class 2KB
AbstractSession$DefaultSessionFilterChain.class 2KB
JmxSessionAcceptor.class 2KB
NamedThreadFactory.class 2KB
ServerSocketChannelSession$1.class 2KB
SessionHandlerFilter.class 2KB
PacketDecoderFilter$LinkPacketDecoderFilter.class 2KB
PacketDecoderFilter$LinkPacketDecoderFilter$LinkedPacketDecoderBuffer.class 2KB
AbstractChannelSession$ChannelReactorHandler$3.class 2KB
SessionFilterAdapter.class 2KB
DirectDispatcher.class 2KB
DefaultPacket.class 2KB
ElapsedTime.class 2KB
PacketDecoderChain.class 2KB
NonBlockingSessionAcceptor$1.class 2KB
Session.class 2KB
NullFilter.class 2KB
AbstractChannelSession$FuturePacket.class 2KB
SerialDecoder.class 1KB
PacketEncoderChain.class 1KB
AbstractChannelSession$ChannelReactorHandler$2.class 1KB
PriorityPacket.class 1KB
ReactorFactory.class 1KB
AbstractChannelSession$ChannelReactorHandler$1.class 1KB
DefaultFuture$1.class 1KB
DispatcherFactory.class 1KB
DefaultReactor$1.class 1KB
DefaultReactor$Attachment.class 1KB
SerialEncoder.class 1KB
PacketDecoderFilter$DiscardPacketDecoderFilter.class 1KB
DefaultBufferPool$Entry.class 1KB
LogThreadGroup.class 1KB
SerialEncoder$1.class 1KB
BlockingSessionAcceptor$1.class 1KB
SessionHandlerAdapter.class 1KB
LinkedBuffer$DelegateReleaseBuffer.class 1KB
SerialDecoder$1.class 1KB
SimplePacketDecoder.class 1KB
ByteBufferBuffer$DelegateReleaseBuffer.class 1KB
ByteArrayBuffer$DelegateReleaseBuffer.class 1KB
ByteBufferDecoder.class 1KB
DefaultReactor$ReactorHandlerInterest.class 1022B
ElapsedTime$PerfProvider.class 1008B
DispatcherFilter$2.class 992B
DispatcherFilter$1.class 992B
DispatcherFilter$5.class 988B
DispatcherFilter$4.class 988B
DispatcherFilter$3.class 977B
DispatcherFilter$6.class 973B
NonBlockingSessionAcceptor$2.class 966B
BufferDecoder.class 956B
SessionMBean.class 920B
ByteArrayDecoder.class 896B
SSLFilter$QueuedPacket.class 890B
DelegatePacket.class 888B
DispatcherFilter$9.class 859B
DispatcherFilter$8.class 859B
DispatcherFilter$7.class 858B
SessionAcceptorHandlerAdapter.class 845B
共 219 条
- 1
- 2
- 3
资源评论
- yanshitao2013-05-18真的很实用!
RedStoneGames
- 粉丝: 62
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功