📄 sockdata.java
字号:
import java.net.*;
import java.io.*;
import java.util.*;
class SockData
{
static final byte[] Read(BufferedInputStream inStream) throws Exception
{
// read the length of the data into 4 byte
// this is to make it platform independent
byte[] pSize = new byte[4];
int nTotal = 0;
int nRead = 0;
while(nTotal<4)
{
nRead = inStream.read(pSize,nTotal,4-nTotal);
if(nRead<0) break;
nTotal += nRead;
}
int nSize = (pSize[0]&0x000000ff)|((pSize[1]&0x000000ff)<<8)|((pSize[2]&0x000000ff)<<16)|((pSize[3]&0x000000ff)<<24);
if(nRead<0||nSize<0) throw new Exception("Invalid input data");
// then read the actual data into a byte array
if(nSize==0) return "".getBytes();
byte[] pData = new byte[nSize];
nTotal = 0;
nRead = 0;
while(nTotal<nSize)
{
nRead = inStream.read(pData,nTotal,nSize-nTotal);
if(nRead<0) break;
nTotal += nRead;
}
if(nRead<0) throw new Exception("Invalid input data");
return pData;
}
static final void Write(BufferedOutputStream outStream, byte[] pData, int nSize) throws Exception
{
if(nSize>=0)
{
// write the length of the data as a 4-byte array
// this is to make it platform-independent
byte[] pSize=
{
(byte)(0x000000ff&nSize),
(byte)(0x000000ff&(nSize>>8)),
(byte)(0x000000ff&(nSize>>16)),
(byte)(0x000000ff&(nSize>>24))
};
// then write the actual data in the given byte array
outStream.write(pSize,0,4);
if(nSize>0)
{
if(pData!=null&&pData.length>=nSize) outStream.write(pData,0,nSize);
else throw new Exception("Invalid output data");
}
outStream.flush();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -