0

Im develop my own game, And i use google play games services to add multiplayer features.

I have a problem with the data messages transfer. In default the message object is byte[], And its easy to transfer object like int, char, and cast they back.

My problem is to transfer objects i created.

Worked example:

When sending data:

 int a = 0;
 mMsgBuf[0] = (byte) a;

When reciving:

int a = (int) mMsgBuf[0];

Now i want to send my object, But i need to transfer him to a byte (not byte array), and put him in mMsgBuf[X].

How can i do that?

Edit Its easy to convert object to byte[], but The messages transfer is a byte[]. I'm consulting about how to transfer the message (As we know - byte[]) with my objectbyte[], Maybe merge the two arrays?

Idon89
  • 141
  • 4
  • 16
  • Possible duplicate of [Java: object to byte\[\] and byte\[\] to object converter (for Tokyo Cabinet)](https://stackoverflow.com/questions/3736058/java-object-to-byte-and-byte-to-object-converter-for-tokyo-cabinet) – Vyacheslav May 25 '17 at 07:00
  • @Vyacheslav - Its not duplicate my friend, Please look at the edit update. – Idon89 May 25 '17 at 07:52
  • `easy to convert object to byte[], but The messages transfer is a byte[]. ` ??? If you have to send a byte array and you can convert your object to a byte array then what is the problem? Unclear what your problem is. – greenapps May 25 '17 at 07:57
  • The way you compress an integer to a byte will only work for values < 256. Integers have four bytes. – greenapps May 25 '17 at 07:58
  • `In default the message object is byte[], `. So that would be a byte array. How many bytes each message? Trying to understand your post it looks to me that you can only send messages which are one byte long. Please elaborate. – greenapps May 25 '17 at 08:03
  • `want to send my object, But i need to transfer him to a byte` ? Compressing a complete object in one byte? How would that be possible? – greenapps May 25 '17 at 08:08
  • Its not possible. I needs to think about a creative solution to transfer my objects in the message. The maximum size of the messae is 1400 bytes. I needs to check if the message with my objects is less then 1400 bytes? Maybe there is a different way to transfer data in Google Play Game Services? – Idon89 May 25 '17 at 08:15

1 Answers1

0

Make your custom Object serializable if it's not. Implement Serializable Interface. Then convert between object and byte array to transfer or receive the data.

Object to byte[]:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
byte[] yourBytes = bos.toByteArray();

byte[] to Object:

ByteArrayIntputSream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = new ObjectInputStream(bis);
Object o = in.readObject();
viz
  • 1,247
  • 16
  • 27
  • Thanks! But the message is byte[] and i cannot enter byte[] into the message. Please look at my edited question. – Idon89 May 25 '17 at 07:51
  • @Idon89 Copy the byte array to your message buffer, which is a byte array, too. You can just do `mMsgBuf = convertedBytes.ToArray()`. No? – viz May 25 '17 at 07:53
  • @Idon89 I think you are misunderstanding basic java byte and byte array concepts. Have a look on some tutorials to understand how to manipulate the array. – viz May 25 '17 at 08:13
  • I understand, My english is not so good so i have some spelling mistakes. The byte convertor you gave me worked only with java objects (int, string, string[]....). But i created a new object of mine and its not work with him. – Idon89 May 25 '17 at 09:13
  • Your object should be serializable in order to process marshalling. See the edited answer above. – viz May 25 '17 at 14:08