com.gitee.yanfanvip.model.Frame Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rocksdb Show documentation
Show all versions of rocksdb Show documentation
RocksDB Cluster and Replication cluster
The newest version!
package com.gitee.yanfanvip.model;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.Serializable;
import com.gitee.yanfanvip.util.ByteUtil;
public class Frame implements Serializable{
private static final long serialVersionUID = -2051607561398807413L;
FrameEnum type;
Row data;
public Frame() { }
public Frame(byte[] bytes) {
byte[] typeBytes = ByteUtil.sub(bytes, 0, 4);
byte[] dataLength = ByteUtil.sub(bytes, 4, 4);
byte[] dataBytes = ByteUtil.sub(bytes, 8, ByteUtil.byteArrayToInt(dataLength));
this.type = FrameEnum.get(ByteUtil.byteArrayToInt(typeBytes));
this.data = ByteUtil.getObject(dataBytes);
}
public Frame(DataInputStream in) throws IOException {
this.type = FrameEnum.get(in.readInt());
int dataLength = in.readInt();
if(dataLength > 0){
byte[] dataBytes = new byte[dataLength];
in.read(dataBytes);
this.data = ByteUtil.getObject(dataBytes);
}
}
public Frame(FrameEnum type) {
this.type = type;
}
public Frame(FrameEnum type, Row data) {
this.type = type;
this.data = data;
}
public FrameEnum getType() {
return type;
}
public void setType(FrameEnum type) {
this.type = type;
}
public Row getData() {
return data;
}
public void setData(Row data) {
this.data = data;
}
public byte[] getByte() {
byte[] typeBytes = ByteUtil.intToByteArray(type.getType());
if(data != null){
byte[] dataBytes = ByteUtil.toBytes(data);
byte[] dataLength = ByteUtil.intToByteArray(dataBytes.length);
return ByteUtil.merges(typeBytes, dataLength, dataBytes);
}else{
byte[] dataLength = ByteUtil.intToByteArray(0);
return ByteUtil.merges(typeBytes, dataLength);
}
}
@Override
public String toString() {
if(data == null){
return type.name();
}else{
return type.name() + ":" + data.toString();
}
}
}