socketbuffer.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 655 行 · 第 1/2 页
JAVA
655 行
}
/**
* Gets a 16-bit word from the buffer
* @param index
*/
public int get16(int index) {
if (index >= size) {
// Index is beyond my data
if (next != null) {
return next.get16(index - size);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
} else if (index + 1 < size) {
// Both bytes are within my data
final int b0 = data[start + index + 0] & 0xFF;
final int b1 = data[start + index + 1] & 0xFF;
return (b0 << 8) | b1;
} else {
// First byte is within my data, second is not
final int b0 = get(index + 0);
final int b1 = get(index + 1);
return (b0 << 8) | b1;
}
}
/**
* Gets a 32-bit word from the buffer
* @param index
*/
public int get32(int index) {
if (index >= size) {
// Index is beyond my data
if (next != null) {
return next.get32(index - size);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
} else if (index + 3 < size) {
// Both bytes are within my data
final int b0 = data[start + index + 0] & 0xFF;
final int b1 = data[start + index + 1] & 0xFF;
final int b2 = data[start + index + 2] & 0xFF;
final int b3 = data[start + index + 3] & 0xFF;
return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
} else {
// First byte is within my data, second is not
final int b0 = get(index + 0);
final int b1 = get(index + 1);
final int b2 = get(index + 1);
final int b3 = get(index + 1);
return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
}
}
/**
* Sets a byte in the buffer
* @param index
*/
public void set(int index, int value) {
if (index >= size) {
if (next != null) {
next.set(index - size, value);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
} else {
data[start + index] = (byte) value;
}
}
/**
* Sets a 16-bit word in the buffer
* @param index
*/
public void set16(int index, int value) {
if (index >= size) {
// Index is beyond my data
if (next != null) {
next.set16(index - size, value);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
} else if (index + 1 < size) {
// Both bytes are within my data
data[start + index + 0] = (byte) ((value >> 8) & 0xFF);
data[start + index + 1] = (byte) (value & 0xFF);
} else {
// First byte is within my data, second is not
set(index + 0, ((value >> 8) & 0xFF));
set(index + 1, (value & 0xFF));
}
}
/**
* Sets a 32-bit word in the buffer
* @param index
*/
public void set32(int index, int value) {
if (index >= size) {
// Index is beyond my data
if (next != null) {
next.set32(index - size, value);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
} else if (index + 3 < size) {
// All bytes are within my data
data[start + index + 0] = (byte) ((value >> 24) & 0xFF);
data[start + index + 1] = (byte) ((value >> 16) & 0xFF);
data[start + index + 2] = (byte) ((value >> 8) & 0xFF);
data[start + index + 3] = (byte) (value & 0xFF);
} else {
// First byte is within my data, last is not
set(index + 0, ((value >> 24) & 0xFF));
set(index + 1, ((value >> 16) & 0xFF));
set(index + 2, ((value >> 8) & 0xFF));
set(index + 3, (value & 0xFF));
}
}
/**
* Sets a byte-array in the buffer
* @param index
*/
public void set(int index, byte[] src, int srcOffset, int length) {
if (index >= size) {
// Index is beyond my data
if (next != null) {
next.set(index - size, src, srcOffset, length);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
} else if (index + length <= size) {
// All bytes are within my data
System.arraycopy(src, srcOffset, data, start + index, length);
} else {
// First byte is within my data, last is not
if (next != null) {
final int myLength = size - index;
System.arraycopy(src, srcOffset, data, start + index, myLength);
next.set(index - myLength, src, srcOffset + myLength, length - myLength);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
}
}
/**
* Gets a byte-array in the buffer
* @param index
*/
public void get(byte[] dst, int dstOffset, int index, int length) {
try {
if (index >= size) {
// Index is beyond my data
if (next != null) {
next.get(dst, dstOffset, index - size, length);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
} else if (index + length <= size) {
// All bytes are within my data
System.arraycopy(data, start + index, dst, dstOffset, length);
} else {
// First byte is within my data, last is not
if (next != null) {
final int myLength = size - index;
System.arraycopy(data, start + index, dst, dstOffset, myLength);
next.get(dst, dstOffset + myLength, Math.max(0, index - myLength), length - myLength);
} else {
throw new IndexOutOfBoundsException("at index " + index);
}
}
} catch (IndexOutOfBoundsException ex) {
log.debug("get(dst, " + dstOffset + ", " + index + ", " + length + ") start=" + start + ", size=" + size);
throw new IndexOutOfBoundsException(ex.getMessage());
}
}
/**
* Gets the contents of this buffer as a single bytearray.
* Please note that on concatenated buffers, this can be an expensive
* function!
* @return The contents of this buffer
*/
public byte[] toByteArray() {
final byte[] result = new byte[getSize()];
int ofs = 0;
SocketBuffer skbuf = this;
do {
System.arraycopy(skbuf.data, skbuf.start, result, ofs, skbuf.size);
ofs += skbuf.size;
skbuf = skbuf.next;
} while (skbuf != null);
return result;
}
/**
* Gets the used number of bytes in the buffer (and any appended buffers)
*/
public int getSize() {
if (next != null) {
return size + next.getSize();
} else {
return size;
}
}
/**
* Set the new buffer size
* @param newSize
*/
private void setSize(int newSize) {
if (data == null) {
if (newSize > 0) {
// There is no buffer, create one
data = new byte[alignSize(Math.max(newSize, INITIAL_SIZE))];
size = newSize;
}
} else if (data.length < start + newSize) {
// Enlarge the buffer
final byte[] newData = new byte[alignSize(start + newSize)];
System.arraycopy(data, start, newData, start, size);
this.data = newData;
this.size = newSize;
} else {
// The buffer is large enough, update size
this.size = newSize;
}
testBuffer();
}
private final int alignSize(int size) {
return (size + (INITIAL_SIZE - 1)) & ~(INITIAL_SIZE - 1);
}
/**
* Test the parameters of this buffer for illegal combinations.
*/
private final void testBuffer() {
if (data == null) {
if (size != 0) {
throw new RuntimeException("size(" + size + ") must be 0 when data is null");
}
if (start != 0) {
throw new RuntimeException("start(" + start + ") must be 0 when data is null");
}
} else {
if (size < 0) {
throw new RuntimeException("size(" + size + ") must be >= 0");
}
if (start < 0) {
throw new RuntimeException("start(" + start + ") must be >= 0");
}
if (start + size > data.length) {
throw new RuntimeException("start(" + start + ")+size(" + size + ") must be <= data.length(" + data.length + ")");
}
}
}
/**
* Gets the header of the linklayer
*/
public LinkLayerHeader getLinkLayerHeader() {
if (linkLayerHeader != null) {
return linkLayerHeader;
} else if (next != null) {
return next.getLinkLayerHeader();
} else {
return null;
}
}
/**
* Gets the header of the networklayer
*/
public NetworkLayerHeader getNetworkLayerHeader() {
if (networkLayerHeader != null) {
return networkLayerHeader;
} else if (next != null) {
return next.getNetworkLayerHeader();
} else {
return null;
}
}
/**
* Gets the header of the transportlayer
*/
public TransportLayerHeader getTransportLayerHeader() {
if (transportLayerHeader != null) {
return transportLayerHeader;
} else if (next != null) {
return next.getTransportLayerHeader();
} else {
return null;
}
}
/**
* Sets the header of the linklayer
* @param header
*/
public void setLinkLayerHeader(LinkLayerHeader header) {
linkLayerHeader = header;
}
/**
* Sets the header of the networklayer
* @param header
*/
public void setNetworkLayerHeader(NetworkLayerHeader header) {
networkLayerHeader = header;
}
/**
* Sets the header of the transportlayer
* @param header
*/
public void setTransportLayerHeader(TransportLayerHeader header) {
transportLayerHeader = header;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?