几天来,我正在尝试使用 flatbuffers 通过套接字发送数据
架构:
namespace Objects;
union Parameters { Login, Register }
table Message {
parameters:Parameters;
}
table Login{
email:string;
password:string;
}
table Register{
email:string;
password:string;
}
root_type Message;
file_identifier "MESG";
我的建设者:
FlatBufferBuilder fbb = new FlatBufferBuilder(1);
FlatBufferBuilder ftb = new FlatBufferBuilder(1);
String buffer = "EMAILCHE";
int id = ftb.createString(buffer);
Login.startLogin(ftb);
Login.addEmail(ftb, id);
Login.addPassword(ftb, id);
int offset = Login.endLogin(ftb);
Message.startMessage(fbb);
Message.addParametersType(fbb, Parameters.Login);
Message.addParameters(fbb, offset);
int par = Message.endMessage(fbb);
Message.finishMessageBuffer(fbb, par);
反序列化:
ByteBuffer bff = fbb.dataBuffer();
Message.MessageBufferHasIdentifier(bff);
Message lg = Message.getRootAsMessage(bff);
switch (lg.parametersType()){
case Parameters.Login:
Login saveSchema = Login.getRootAsLogin(lg.getByteBuffer());
System.out.println(saveSchema.email());
break;
case Parameters.Register:
System.out.println("reg");
break;
}
在这一行抛出索引越界异常: System.out.println(saveSchema.email()); 完全错误:线程“主”java.lang.IndexOutOfBoundsException 中的异常