Protobuf streaming (lazy serialization) API

前端 未结 3 2031
星月不相逢
星月不相逢 2021-02-10 00:57

We have an Android app that uses Protocol Buffers to store application data. The data format (roughly) is a single protobuf (\"container\") that contains a list of protobufs (\"

3条回答
  •  囚心锁ツ
    2021-02-10 01:23

    In the normal java version of Protocol buffers there is Delimited files where you write Protocol-Buffers one at a time. I am not sure if it is in the Android version

     aLocation.writeDelimitedTo(out);
    

    As Marc has indicated it easily implemented; just write a length followed the serialised bytes. In normal (non android) java version of prortocol-buffers you can also do (you have to serialise to a byte array or something similar)

    private CodedOutputStream codedStream = null;
    
    
    public void write(byte[] bytes) throws IOException {
        if (bytes != ConstClass.EMPTY_BYTE_ARRAY) {
            codedStream.writeRawVarint32(bytes.length);
            codedStream.writeRawBytes(bytes);
            codedStream.flush();
        }
    }
    

    and

        private CodedInputStream coded;
    
    public byte[] read() throws IOException {
        if (coded == null) {
            throw new IOException("Reader has not been opened !!!");
        }
        if (coded.isAtEnd()) {
            return null;
        }
        return coded.readBytes().toByteArray();
    

    Something may be possible in other Protocol-Buffers versions

提交回复
热议问题