我正在尝试将多个对象序列化到一个文件中。特别是,当我尝试写
public void execute(PipelineContext context) throws Exception {
FileOutputStream fos = new FileOutputStream("test_audit_trail2.objects");
ObjectOutputStream oos = new ObjectOutputStream(fos);
BigInteger rho = (BigInteger) context.get("rho");
BigInteger p = (BigInteger) context.get("p");
BigInteger xS = (BigInteger) context.get("xs");
BigInteger zSBar = (BigInteger)context.get("zsbar");
int nS = (Integer) context.get("ns");
P2PShuffler ownShuff = (P2PShuffler) context.get("shuffler");
HexCryptor cryptor = (HexCryptor) context.get("cryptor");
String[] PRPrimePrimeBar = (String []) context.get("pr_prime_prime_bar");
P2PAuditTrailGenerator ownATG = (P2PAuditTrailGenerator) context.get("p2p_audit_trail_generator");
int kS = (Integer) context.get("ks");
oos.writeObject(rho);
oos.writeObject(p);
oos.writeObject(xS);
oos.writeObject(zSBar);
oos.writeObject(ownShuff);
oos.writeObject(cryptor);
oos.writeObject(PRPrimePrimeBar);
oos.writeObject(ownATG);
oos.writeObject(nS);
oos.writeObject(kS);
这发生了,所有的 writeObject 都成功了,我忠实地关闭了相应的流。但是,当我尝试通过调用 readObject() 来重建所有对象时,我遇到了UTFDataFormatException
java.io.UTFDataFormatException
at java.io.ObjectInputStream$BlockDataInputStream.readUTFSpan(ObjectInputStream.java:3081)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(ObjectInputStream.java:3006)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2819)
at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1050)
at java.io.ObjectStreamClass.readNonProxy(ObjectStreamClass.java:614)
at java.io.ObjectInputStream.readClassDescriptor(ObjectInputStream.java:808)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1564)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1495)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:1911)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1873)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at edu.foo.peer.pipeline.AuditTrailCheckStageTest.setUp
在逐一注释掉readObject
and之后,似乎省略了 this 可以让我成功读取所有对象。查看这个类,似乎没有 String 相关的对象。writeObject
P2PAuditTrailGenerator
public class P2PAuditTrailGenerator implements Serializable {
private int num;
private BigInteger phi;
private BigInteger rho;
/**
* @param auditTrailSecretKeys The secret keys used for audit trail
*/
private BigInteger[] auditTrailSecretKeys;
private BigInteger[] encAuditTrails;
private Random rnd;
public BigInteger [] getAuditTrailSecretKeys(){
return auditTrailSecretKeys;
}
public P2PAuditTrailGenerator(int num, BigInteger rho, BigInteger phi) {
this.num = num;
this.phi = phi;
this.rho = rho;
auditTrailSecretKeys = new BigInteger[num];
encAuditTrails = new BigInteger[num];
rnd = new Random();
}
public BigInteger[] generateATs() {
for (int i = 0; i < num; i++) {
//assuming the security parameter is always 512 or 1024;
auditTrailSecretKeys[i] = new BigInteger(200, rnd).mod(phi);
encAuditTrails[i] = rho.modPow(auditTrailSecretKeys[i], phi);
}
return encAuditTrails;
}
}
id-ing 错误的类,或者如果不是,我的实现有什么问题?