tcpdatabuffer.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 132 行
JAVA
132 行
/*
* $Id: TCPDataBuffer.java,v 1.1 2003/11/25 11:52:23 epr Exp $
*/
package org.jnode.net.ipv4.tcp;
import org.jnode.net.SocketBuffer;
/**
* @author Ewout Prangsma (epr@users.sourceforge.net)
*/
public class TCPDataBuffer {
private final byte[] data;
private final int length;
private int used;
/**
* Create a new instance
*
* @param length
*/
public TCPDataBuffer(int length) {
this.data = new byte[length];
this.length = length;
this.used = 0;
}
/**
* Create a socketbuffer for a given portion of this databuffer.
*
* @param offset
* @param length
* @return The created buffer
*/
public SocketBuffer createSocketBuffer(int offset, int length) {
if ((offset < 0) || (offset >= used)) {
throw new IndexOutOfBoundsException("offset " + offset);
}
if ((length < 0) || (offset + length > used)) {
throw new IndexOutOfBoundsException("offset + length" + offset + "+" + length);
}
return new SocketBuffer(data, offset, length);
}
/**
* Remove the first count byte from this buffer
*
* @param count
*/
public void pull(int count) {
if ((count < 0) || (count > used)) {
// Avoid errors in SYN & FIN situations
// TODO create a better solution for this
if (count > 2) {
throw new IllegalArgumentException("count " + count);
}
} else {
used -= count;
System.arraycopy(data, count, data, 0, used);
}
}
/**
* Add the given data to this buffer.
*
* @param src
* @param srcOffset
* @param length
* @return The offset of the added data within the buffer.
*/
public int add(byte[] src, int srcOffset, int length) {
if (length > getFreeSize()) {
throw new IllegalArgumentException("Not enough free space");
}
final int dstOffset = this.used;
System.arraycopy(src, srcOffset, this.data, dstOffset, length);
this.used += length;
return dstOffset;
}
/**
* Add the given data to this buffer.
*
* @return The offset of the added data within the buffer.
*/
public int add(SocketBuffer skbuf, int index, int length) {
if (length > getFreeSize()) {
throw new IllegalArgumentException("Not enough free space");
}
final int dstOffset = this.used;
skbuf.get(this.data, dstOffset, index, length);
this.used += length;
return dstOffset;
}
/**
* @return Returns the length.
*/
public final int getLength() {
return this.length;
}
/**
* @return Returns the used.
*/
public final int getUsed() {
return this.used;
}
/**
* Gets the number of free bytes in this buffer
*/
public final int getFreeSize() {
return length - used;
}
/**
* Read data from the start of this buffer and remove it.
*
* @param b
* @param off
* @param len
* @return The number of bytes read
*/
public int read(byte[] b, int off, int len) {
len = Math.min(used, len);
System.arraycopy(data, 0, b, off, len);
pull(len);
return len;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?