FlatBuffers索引超出绑定异常

时间:2016-02-15 21:09:29

标签: java flatbuffers

现在我试图使用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()); 完整错误:线程中的异常" main" java.lang.IndexOutOfBoundsException

2 个答案:

答案 0 :(得分:0)

只使用一个FlatBufferBuilder。您无法在另一个缓冲区中引用数据。

答案 1 :(得分:0)

而不是:

Login saveSchema = Login.getRootAsLogin(lg.getByteBuffer());
                System.out.println(saveSchema.email());

这样:

Login saveLogin = new Login();
                saveLogin = (Login) lg.parameters(saveLogin);

                System.out.println(saveLogin.password());