1

For example:

  • I send a byte[] over Socket outputStream using ObjectInput/OutputStream:

    ObjectOutputStream myOutput = new ObjectOutputStream(mySocket.getOutputStream());
    myOutput.write(myByteArray);
    
  • At the other end, he use DataInputStream (and may use other InputStream as well) to read:

    DataInputStream hisInput = new DataInputStream(hisSocket.getInputStream());
    hisInput.read(hisByteArray);
    

So my question is:

  • Will the receiver read the byte[] correctly?
  • What about String using writeObject and readUTF?

Info:

  • I can only use Java library (JRE8) and is new to Java's Stream.
  • I can not expect or force the other side to use what Stream other than knowing that they are not using ObjectInput/OutputStream
  • I asked this question because using ObjectInput/OutputStream MAY save me some work, I took advice from my other question.
3
  • Just FYI, both the Data and Object Input/Output streams date back to an age when communication/serialization protocols were not nearly as well developed as they are now. These days we have XML and JSON, both of which are technology neutral and big / little endian neutral. Commented Oct 24, 2014 at 15:35
  • Is it a Byte[] or a byte[]? Commented Oct 24, 2014 at 15:46
  • @DavidConrad Oh! I'm sorry, typo, I edited the question. Commented Oct 24, 2014 at 15:51

1 Answer 1

1

Will the receiver read the Byte[] correctly?

No.

What about String using writeObject and readUTF?

No.

If you use ObjectOutputStream.writeObject to write, the only practical way to read the stream is using ObjectOutputStream.readObject.

In theory, you could write code to implement the serialization protocol yourself (using byte or "data" input streams), but it is not a sensible / practical approach.


I can not expect or force the other side to use what Stream other than knowing that they are not using ObjectInput/OutputStream

Whatever you use as the format when you write, you will be forcing the other end to use something that is capable of reading it. Forcing the other end (if it is Java) to use ObjectInputStream should not be a concern. It is another matter if the other end could be implemented in another language, but then you should probably be considering something like JSON or XML as your encoding scheme.

Sign up to request clarification or add additional context in comments.

10 Comments

That's a harsh fact, so in short, ObjectOutputStream can only work with ObjectInputStream????
I didn't say that. I said it is the only practical way. You could implement code that unpicked the Java object serialization format (it is specified!) ... but that is hardly a practical solution.
Java Serialize primitive type too? I thought because it is primitive, it doesn't have to be Serialized.
A Byte[] is not primitive.
@DavidConrad Well, it's related to my other question about forcing or not. But I've edited my question, sorry for my typo, it's actually byte[] not Byte[].
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.