帮忙看下这段代码是什么意思?
public class LengthFramer implements Framer {
public static final int MAXMESSAGELENGTH = 65535;
public static final int BYTEMASK = 0xff;
public static final int SHORTMASK = 0xffff;
public static final int BYTESHIFT = 8;
private DataInputStream in; // wrapper for data I/O
public LengthFramer(InputStream in) throws IOException {
this.in = new DataInputStream(in);
}
public void frameMsg(byte[] message, OutputStream out) throws IOException {
if (message.length > MAXMESSAGELENGTH) {
throw new IOException("message too long");
}
// write length prefix
out.write((message.length >> BYTESHIFT) & BYTEMASK);//OutPutStream.write(int b)只写b的低位8bit到流中,高24bit忽略
out.write(message.length & BYTEMASK);
// write message
out.write(message);
out.flush();
}
public byte[] nextMsg() throws IOException {
int length;
try {
length = in.readUnsignedShort(); // read 2 bytes
} catch (EOFException e) { // no (or 1 byte) message
return null;
}
// 0 <= length <= 65535
byte[] msg = new byte[length];
in.readFully(msg); // if exception, it's a framing error.
return msg;
}
}
请问下标红的那段代码是什么意思、,可以解释下吗
[解决办法]
>>移位运算
>>8就等于除以2的8次方
得到的数与SHORTMASK 进行与运算,与运算就是转为二进制之后
一:0011
二:1001
三结果:0001
同为1时结果为1。
[解决办法]
write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。
1个int32位,message.length右移了8位,所以这里写出去的应该是message.length第8到15位,得到应该就是前缀长度
&0xffff在这没什么意义吧
