我正在学习Java中的Protocol Buffer。在基本序列化/反序列化之后,我试图进行压缩。 在提供的代码中,我尝试使用GZIP,但在反序列化时,它会读取一条额外的记录。可以在System.out中看到一个额外的","最后。 使用相同代码时不会发生此问题,但使用Snappy(在下面的代码中注释)。如果我做错了什么建议?或者,如果我可以绕过这个问题。
使用GZIP输出:
id0,NAME0
,
使用Snappy输出:
id0,NAME0
代码:
package testPkg;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.xerial.snappy.SnappyInputStream;
import org.xerial.snappy.SnappyOutputStream;
public class TestProtoBuf {
public static String fileNm = "C:/testFile";
public static void main(String[] args) throws IOException {
// creating data
List<DataClass> list = new ArrayList<>();
for (int i = 0; i < 1; i++) {
list.add(new DataClass("id" + i, "NAME" + i));
}
// writing data
try (OutputStream os = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(fileNm)))
/*OutputStream os = new SnappyOutputStream(new BufferedOutputStream(new FileOutputStream(fileNm)))*/) {
for (DataClass dc : list) {
DataClassProtoBufProxy.DataClass.Builder bld = DataClassProtoBufProxy.DataClass.newBuilder();
bld.setId(dc.id);
bld.setName(dc.name);
bld.build().writeDelimitedTo(os);
}
os.flush();
}
// reading data
try (InputStream is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(fileNm)))
/*InputStream is = new SnappyInputStream(new BufferedInputStream(new FileInputStream(fileNm)))*/) {
while (is.available() > 0) {
DataClassProtoBufProxy.DataClass.Builder bld = DataClassProtoBufProxy.DataClass.newBuilder();
bld.mergeDelimitedFrom(is);
DataClassProtoBufProxy.DataClass dc = bld.build();
System.out.println(dc.getId() + "," + dc.getName());
}
}
}
static class DataClass {
private String id;
private String name;
DataClass() {
id = null;
name = null;
}
DataClass(String id, String name) {
this.id = id;
this.name = name;
}
}
}
原型文件:
// [START declaration]
syntax = "proto3";
package testPkg;
// [END declaration]
// [START java_declaration]
option java_package = "testPkg";
option java_outer_classname = "DataClassProtoBufProxy";
// [END java_declaration]
// [START messages]
message DataClass {
string id = 1;
string name = 20;
}
// [END messages]
Maven依赖项:
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial.snappy/snappy-java -->
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.2.6</version>
</dependency>