Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java serialization causes utfdataformatexception

I'm trying to serialize a number of objects to a file. In particular, when i try to write

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);

This happen ok, all the writeObject succeed and I close the corresponding streams faithfully. However, when i try to reconstruct back all the objects by calling readObject(), I encountered the 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

After commenting out the readObject and writeObject one by one, it seems that the leaving P2PAuditTrailGenerator out of this allows me to read in all the objects successfully. Looking at this class, it seems that there aren't String related objects.

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;
    }
}

Am id-ing the wrong class or if not, what's wrong with my implementation?

like image 838
goh Avatar asked Nov 13 '22 20:11

goh


1 Answers

I'm guessing that you've declared P2PAuditTrailGenerator to be serializable but it doesn't have a no-argument constructor. Everything else looks OK.

like image 89
Chris Gerken Avatar answered Nov 15 '22 11:11

Chris Gerken