?? bufferstream.java
字號:
/*
* ChannelBuffer.java
*
* Created on 2003年6月17日, 下午1:22
*/
package collector.communication;
import java.io.*;
/**
*
* @author WangJun
*/
public class BufferStream {
private byte[] m_Buffer = null;
private int m_Index, m_Limit, m_Mark;
// is the stream m_Closed?
private boolean m_Closed;
private boolean m_Shared;
private int m_Capacity;
private static final int DEFAULT_INITIAL_BUFFER_SIZE = 8192;
/** Creates a new instance of ChannelBuffer */
public BufferStream() {
m_Buffer = new byte[DEFAULT_INITIAL_BUFFER_SIZE];
m_Index = 0;
m_Limit = 0;
m_Mark = 0;
m_Closed = false;
m_Shared = true;
m_Capacity = DEFAULT_INITIAL_BUFFER_SIZE;
}
public BufferStream(int initialBufferSize) {
m_Capacity = initialBufferSize;
m_Buffer = new byte[m_Capacity];
m_Index = 0;
m_Limit = 0;
m_Mark = 0;
m_Closed = false;
m_Shared = true;
}
public BufferStream(byte[] m_pBuffer, int m_pOffset, int m_pLength) {
if (m_pBuffer == null) {
throw new NullPointerException();
}
else if ( (m_pOffset < 0) || (m_pOffset + m_pLength > m_pBuffer.length) ||
(m_pLength < 0)) {
throw new IndexOutOfBoundsException();
}
else {
m_Buffer = m_pBuffer;
m_Index = m_pOffset;
m_Limit = m_pOffset + m_pLength;
m_Mark = m_pOffset;
m_Closed = false;
m_Shared = true;
m_Capacity = DEFAULT_INITIAL_BUFFER_SIZE;
}
}
public int clear() {
m_Index = 0;
m_Limit = 0;
m_Mark = 0;
m_Closed = false;
m_Shared = true;
m_Capacity = DEFAULT_INITIAL_BUFFER_SIZE;
return 1;
}
public int read() throws IOException {
if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else if (m_Index >= m_Limit) {
return -1; // EOF
}
else {
return m_Buffer[m_Index++] & 0xff;
}
}
public int read(byte m_pBuffer[], int m_pOffset, int m_pLength) throws
IOException {
if (m_pBuffer == null) {
throw new NullPointerException();
}
else if ( (m_pOffset < 0) || (m_pOffset + m_pLength > m_pBuffer.length) ||
(m_pLength < 0)) {
throw new IndexOutOfBoundsException();
}
else if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else if (m_Index >= m_Limit) {
return -1; // EOF
}
else {
// restrict m_pLength to available m_pBuffer
if (m_pLength > m_Limit - m_Index) {
m_pLength = m_Limit - m_Index;
// copy out the subarray
}
System.arraycopy(m_Buffer, m_Index, m_pBuffer, m_pOffset, m_pLength);
m_Index += m_pLength;
if (m_Index == m_Limit) {
m_Index = 0;
m_Limit = 0;
}
return m_pLength;
}
}
public int preRead(byte m_pBuffer[], int m_pOffset, int m_pLength) throws
IOException {
if (m_pBuffer == null) {
throw new NullPointerException();
}
else if ( (m_pOffset < 0) || (m_pOffset + m_pLength > m_pBuffer.length) ||
(m_pLength < 0)) {
/*
if (m_pOffset < 0)
m_CFunction.putHintString (1,"preRead in BufferStream Error #1" + "m_pOffset= "+ m_pOffset + "m_pLength"+ m_pLength + "m_pBuffer.length" + m_pBuffer.length );
if (m_pOffset + m_pLength > m_pBuffer.length)
m_CFunction.putHintString (1,"preRead in BufferStream Error #2" + "m_pOffset= "+ m_pOffset + "m_pLength"+ m_pLength + "m_pBuffer.length" + m_pBuffer.length);
if (m_pLength < 0)
m_CFunction.putHintString (1,"preRead in BufferStream Error #3" + "m_pOffset= "+ m_pOffset + "m_pLength"+ m_pLength + "m_pBuffer.length" + m_pBuffer.length);
*/
throw new IndexOutOfBoundsException();
}
else if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else if (m_Index >= m_Limit) {
return -1; // EOF
}
else {
// restrict m_pLength to available m_pBuffer
if (m_pLength > m_Limit - m_Mark) {
m_pLength = m_Limit - m_Mark;
// copy out the subarray
}
System.arraycopy(m_Buffer, m_Mark, m_pBuffer, m_pOffset, m_pLength);
// m_Index += m_pLength;
return m_pLength;
}
}
public long skip(long amount) throws IOException {
if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else if (amount <= 0) {
return 0;
}
else {
// restrict amount to available m_pBuffer
if (amount > m_Limit - m_Index) {
amount = m_Limit - m_Index;
}
m_Index += (int) amount;
return amount;
}
}
public int available() throws IOException {
if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else {
return m_Limit - m_Index;
}
}
public void close() {
m_Closed = true;
this.m_Buffer = null;
}
// if the report is adjusted by procotol , call the function
public void mark() {
m_Mark = m_Index;
}
public void reset() throws IOException {
if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else {
// reset m_Index
m_Index = m_Mark;
}
}
public boolean markSupported() {
return true;
}
public int write(int datum) throws IOException {
if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else {
if (m_Limit >= m_Capacity) {
return -1;
}
// store the byte
m_Limit++;
m_Buffer[m_Limit] = (byte) datum;
}
return m_Limit;
}
public int write(byte[] m_pBuffer, int m_pOffset, int m_pLength) throws
IOException {
if (m_pBuffer == null) {
throw new NullPointerException();
}
else if ( (m_pOffset < 0) || (m_pOffset + m_pLength > m_pBuffer.length) ||
(m_pLength < 0)) {
throw new IndexOutOfBoundsException();
}
else if (m_Closed) {
throw new IOException("Stream m_Closed");
}
else {
if (m_Limit + m_pLength > m_Capacity) {
return -1;
}
// copy in the subarray
System.arraycopy(m_pBuffer, m_pOffset, m_Buffer, m_Limit, m_pLength);
m_Limit += m_pLength;
}
return m_Limit;
}
/*public static void main(String args[]) {
String dbHost = "localhost";
try {
if (args.length > 0) {
dbHost = args[0];
}
BufferStream m_BufferStream = new BufferStream();
int i = 0;
byte[] m_pBuffer = new byte[512];
byte[] recdata = new byte[512];
while (true) {
for (int k = 0; k < 20; k++) {
m_pBuffer[k] = (byte) i;
i++;
if (i >= 256) {
i = 0;
}
}
m_BufferStream.write(m_pBuffer, 0, 20);
int num = m_BufferStream.available();
//CollectorDefine.SystemPrintln("num = " + num );
m_BufferStream.preRead(recdata, 0, num);
//m_BufferStream.m_Mark(1);
// CollectorDefine.SystemPrintln(m_CFunction.translateBytesToString(1, recdata,num));
for (int k = 0; k < 255; k++) {
m_pBuffer[k] = (byte) i;
i++;
if (i >= 256) {
i = 0;
}
}
m_BufferStream.write(m_pBuffer, 0, 20);
num = m_BufferStream.available();
//CollectorDefine.SystemPrintln("num = " + num );
m_BufferStream.preRead(recdata, 0, num);
m_BufferStream.read(recdata, 0, num);
m_BufferStream.mark();
//CollectorDefine.SystemPrintln(m_CFunction.translateBytesToString(1, recdata,num));
num = m_BufferStream.available();
//CollectorDefine.SystemPrintln("num = " + num );
break;
}
}
catch (Exception e) {
CollectorDefine.SystemPrintln("getLocalHostName In CFunction "+ m_Exception.getMessage());
}
}
*/
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -