一个比较复杂的情况是流中的数据类型各不相同,可能是A+B+C,对A,B,C的读写处理是A,B,C的类型来决定。
1>在往流中写入数据之前要写入数据的类型,
2>从流中读数据的时候也要先判断数据类型
1>在往流中写入数据之前要写入数据的类型,
2>从流中读数据的时候也要先判断数据类型
private int readInt(InputStream in) throws IOException
{
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}
private byte readByte(InputStream in) throws IOException
{
int b = in.read();
if(b < 0)
throw new EOFException();
return (byte)b;
}
private String readString(InputStream in) throws IOException
{
int len = readInt(in);
byte b[] = new byte[len];
int l = 0;
while(l < len)
l += in.read(b, l, len - l);
return new String(b);
}
private void writeInt(OutputStream out, int v) throws IOException
{
out.write((v >>> 24) & 0xFF);
out.write((v >>> 16) & 0xFF);
out.write((v >>> 8) & 0xFF);
out.write((v >>> 0) & 0xFF);
}
private void writeString(OutputStream out, String v) throws IOException
{
byte[] b = v.getBytes();
writeInt(out, b.length);
out.write(b);
}