Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从poco到pojo以及从pojo到poco的协议缓冲区_Java_C#_Binary_Protocol Buffers - Fatal编程技术网

Java 从poco到pojo以及从pojo到poco的协议缓冲区

Java 从poco到pojo以及从pojo到poco的协议缓冲区,java,c#,binary,protocol-buffers,Java,C#,Binary,Protocol Buffers,我希望这篇文章可以帮助其他同样情况的人。 我一直在尝试使用协议缓冲区将普通对象保存和读取到二进制文件中,并且与平台/语言无关 Protobuf net是.net端采用的序列化程序,并且 Protostuff是java端使用的序列化程序 在一个平台中进行序列化/反序列化时,一切都可以正常工作,但如果我试图从java平台读取在.net中创建的二进制文件,则无法正常工作,反之亦然 在C#中,我有以下poco: [ProtoContract] public class PocoDto { [

我希望这篇文章可以帮助其他同样情况的人。 我一直在尝试使用协议缓冲区将普通对象保存和读取到二进制文件中,并且与平台/语言无关

  • Protobuf net是.net端采用的序列化程序,并且
  • Protostuff是java端使用的序列化程序
在一个平台中进行序列化/反序列化时,一切都可以正常工作,但如果我试图从java平台读取在.net中创建的二进制文件,则无法正常工作,反之亦然

在C#中,我有以下poco:

[ProtoContract]
public class PocoDto
{
    [ProtoMember(1)]
    public string name { get; set; }
    [ProtoMember(2)]
    public int timestamp { get; set; }
}
在java中,pojo是:

import io.protostuff.Tag;

public final class PocoDto {
    @Tag(1)
    public String name;
    @Tag(2)
    public int timestamp;
}
使用protobuf net在.net中序列化的方法是:

    internal static void AppendToBin(PocoDto poco, string path)
    {
        using (var fs = new FileStream(path, FileMode.Append))
        {
            Serializer.SerializeWithLengthPrefix(fs, poco, PrefixStyle.Base128, 1);
        }         
    }
而我用来反序列化的方法是

    public static List<PocoDto> FromBin(this string path)
    {
        using (var fs = new FileStream(path, FileMode.Open))
        {
           return Serializer.DeserializeItems<T>(fs, PrefixStyle.Base128, 1).ToList();
        }
    }
公共静态列表FromBin(此字符串路径)
{
使用(var fs=new FileStream(路径,FileMode.Open))
{
返回Serializer.DeserializeItems(fs,PrefixStyle.Base128,1).ToList();
}
}
使用protostuff在java中进行序列化时使用的方法是:

public static void append(PocoDto pojo, String filePath) throws IOException
{
    Schema<PocoDto> schema = RuntimeSchema.getSchema(PocoDto.Class);
    File file = new File(filePath);
    file.createNewFile();       
    FileOutputStream out = new FileOutputStream(file, true);    
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);

    ProtostuffIOUtil.writeDelimitedTo(out, pojo, schema, buffer);
    buffer.clear();
    out.flush();
    out.close();
}
publicstaticvoidappend(pocodtopojo,stringfilepath)抛出IOException
{
Schema Schema=RuntimeSchema.getSchema(PocoDto.Class);
文件文件=新文件(文件路径);
createNewFile();
FileOutputStream out=新的FileOutputStream(文件,true);
LinkedBuffer buffer=LinkedBuffer.allocate(LinkedBuffer.DEFAULT\u buffer\u SIZE);
ProtostuffIOUtil.writedLimitedTo(out、pojo、schema、buffer);
buffer.clear();
out.flush();
out.close();
}
对于反序列化:

public static List<PocoDto> fromBin(String filePath) throws IOException
{
    Schema<PocoDto> schema = RuntimeSchema.getSchema(pojoType);
    File file = new File(filePath);
    if(!file.exists())
    {
        return null;
    }
    FileInputStream inStream = new FileInputStream(new File(filePath)); 
    return ProtobufIOUtil.parseListFrom(inStream, schema);  
}
公共静态列表fromBin(字符串文件路径)引发IOException
{
Schema Schema=RuntimeSchema.getSchema(pojoType);
文件文件=新文件(文件路径);
如果(!file.exists())
{
返回null;
}
FileInputStream inStream=newfileinputstream(新文件(filePath));
返回ProtobufIOUtil.parseListFrom(流内,模式);
}
通过比较在每种情况下创建的两个二进制文件,C#中的文件似乎总是在每个条目后附加一个换行符,而在java中生成的文件则不会出现这种情况。 此外,使用protostuff创建一个列表会添加不同的分隔符,并且它似乎不会以相同的方式处理长度前缀。 我只是想知道在设置缓冲区时是否选择了错误的选项,或者这两个输出是否真的不兼容。 谢谢你的帮助