Can I inform a remote host of the original datatype I'm sending in my byte[]?

Suppose I want to create a generic container to send throughout a local topology of nodes:

public class StreamReceiver{
    byte[] payload;
}

On the receiving end, I want to be able to cast this byte[] to a specific datatype, likely passed along with the payload as part of the same object. So something like:

public class StreamPackage{
    byte[] payload;
    Object/String/Something: [some identifier of what payload type is]
}

So that I can check this variable on the receiving side to know how to properly cast the data?

Answers


import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        MyClass myClass = new MyClass("An object");
        StreamObject so = new StreamObject(myClass, "MyClass");

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(".test")));
        oos.writeObject(so);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(".test")));
        so = (StreamObject) ois.readObject();
        Class clz = Class.forName(so.name);
        System.out.println(clz.getName());
        Object o = clz.cast(so.o);
        System.out.println(o.getClass());
    }
}

class StreamObject implements Serializable{
    public StreamObject(Serializable o, String name) {
        this.o = o;
        this.name = name;
    }

    Serializable o;
    String name;
}

class MyClass implements Serializable{
    String id;

    public MyClass(String id) {
        this.id = id;
    }
}

The output of this sample is

MyClass class MyClass

But it seams you must get the filed and method information only by reflection.

I can not think of any other ways.


Need Your Help

Kimsufi API setup

php ovh

Has anyone had any experience in setting up a Kimsufi API using PHP?

Safari web inspector next match?

search safari find web-inspector

I am searching my Javascript file for certain text. I entered that text into the search box, and it helpfully tells me that there are four matches. So far, so good. But how can I go to the next/