Die Umwandlung von Objekt zu byte array und zurück in java

Ich habe zwei Klassen: TcpPacket und IpPacket. Die tcpPacket gespeichert werden soll in das Feld des ip-Pakets. Wie kann ich zuverlässig konvertieren tcpPacket-Objekt ein byte-array, so dass ich speichern Sie es in der IpPacket Datenfeld? Und nach Erhalt das Paket auf der anderen Seite, wie zuverlässig konvertiert es zurück in ein Paket-Objekt?

public static final class TcpPacket {
    int source;
    int destination;

    public Packet( int source, int destination ) {
        this.source = source;
        this.destination = destination;
    }

    public String toString() {
        return "Source: " + source + ", Dest: " + destination;
    }
}

public static final class IpPacket {
    byte[] data;

    IpPacket( byte[] data ){
        this.data = data;
    }
}

TcpPacket tcpPacket = new TcpPacket( <someint>, <someint> );

//the following doesn't work because tcppacket is not a byte array.
IpPacket ipPacket = new IpPacket( tcpPacket );

Wie wird das normalerweise gemacht?

Dank Maricruzz

InformationsquelleAutor Maricruzz | 2014-04-29

Schreibe einen Kommentar