Search code examples
javaarraysserializationlibgdxserver

2D Array to int (seed) and back


I have got a question....

Here we got an 2d byte array :

byte[][] duengonMap = new byte[500][500];

Because i wanna send it from the client to the server or the other way round, i need to put it into a int/long. From the server it will be send to the other connected clients and there it is gonna convert back to an 2d array. It sounds so simple ... but how do i do that ?

I tried something like that :

int[][] twoD = new int[][] { new int[] { 1, 2 },
        new int[] { 3, 4 } };

int[][] newTwoD = null; // will deserialize to this

System.out.println("Before serialization");
for (int[] arr : twoD) {
    for (int val : arr) {
        System.out.println(val);
    }
}

try {
    FileOutputStream fos = new FileOutputStream("test.dat");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(twoD);

    FileInputStream fis = new FileInputStream("test.dat");
    ObjectInputStream iis = new ObjectInputStream(fis);
    newTwoD = (int[][]) iis.readObject();

} catch (Exception e) {

}

System.out.println("After serialization");
for (int[] arr : newTwoD) {
    for (int val : arr) {
        System.out.println(val);
    }
}
}

It only is converted into an "File" maybe i did that wrong, but i have no idea how to convert it to an int or long ... Any ideas ? Or examples ?


Solution

  • You can send all the values One by One using a loop and read them simultaneously on the server/client and arrange them back into an array