0
votes

Please I am quite new to java programming.I am trying to write a class to a bytearrayoutputstream, then pass this data to a simple array.

Then I read and reconstruct the class out of the byte array.

I am doing this using ObjectOutputStream and ObjectInputStream. but I don't understand why I am still getting java.io.InvalidClassException: that my class has no valid constructor. the best help I could find is at java.io.InvalidClassException: no valid constructor.

My code:

// this is the class I am writing to the ByteArrayOutputStream
public class CrtB extends X509v3CertificateBuilder implements Serializable {
    private static final long serialVersionUID = 1234509876;

    public CrtB(X500nEx arg0, BigInteger arg1, Date arg2, Date arg3,
            X500nEx arg4, SubjectPublicKeyInfo arg5) {
        super(arg0, arg1, arg2, arg3, arg4, arg5);
    }
}
    //this how I pass the class to the ByteArrayOutputStream and then reconstruct it

public class Testclass implements Serializable {  

    public  CrtB getCrt(){
        subpubInfo=SubjectPubInfo.getInstance(getPublicKey.getEncoded());
        certbld=new CrtB(name1, serialNr, start, end,name2, subpubInfo);
        return certbld; // certbld is a global variable
    }

    //writes to the ByteArrayOutputStream
    private  ByteArrayOutputStream writetoBAS(){
        ByteArrayOutputStream bout=null;
        try{
            bout=new ByteArrayOutputStream();
            ObjectOutputStream obj=new ObjectOutputStream(bout);
            obj.writeObject(certbld);
            obj.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return bout;

    }
    //read from byte array
    private  CrtB getbui(byte[] ar){
    CrtB b=null;
    try {
        ByteArrayInputStream bin=new ByteArrayInputStream(ar);
        ObjectInputStream oin=new ObjectInputStream(bin);
        b=(CrtB)oin.readObject();
        System.out.println("successfully transferred");
    }catch (Exception e){
        e.printStackTrace();
    }
    return b;
    }

    public static void main(String[] args) {

        Testclass rqR=new Testclass();
        rqR.CrtB();
    byte[] br=rqR.writeBAS().toByteArray();
      CrtB b=rqR.getbui(br);
    }
}

Could any one help me or give me a hint.

1
Post stacktrace please. - takendarkk
@csmckelvey It doesn't need one. The no-arg constructor is created automatically. - Kayaman
It may have to do with X509v3CertificateBuilder constructor not taking the parameters you passed in. @Kayaman Yes, sorry, I'm still waking up. - takendarkk
Can you make sure that the super(arg0, arg1, arg2, arg3, arg4, arg5); matches with the super constructor in "number", "type" and order of argyuments. - Nu2Overflow
It looks like you are using a lot of different classes (X509v3CertificateBuilder, X500nEx, etc.) -- are these classes ones that you created in the same package, or did you import them from a library? If the later, could you include the import statements in your code? Also, you have two public classes together -- are these in the same file in your program? If they are then it won't compile. - James Dunn

1 Answers

1
votes

From Serializable: To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

And your case is that X509v3CertificateBuilder doesn't implements Serializable and X509v3CertificateBuilder has no accessible (public or protected) no-arg constructor.

Does it answer your question "I don't understand why I am still getting java.io.InvalidClassException"?

And I found a definition of X509v3CertificateBuilder here. Did you use this.