Concept of "Serialization" - java

What are methods to convert data (ints, strings) to bytes in Java? I am looking for methods other than using the Serializable class. I researched and found things like ByteOutputStream.
Can I just parse strings and ints to a byte data type?
Any suggestions?

Have a look at DataInputStream and DataOutputStream, they convert all Java data types to bytes and read/write to an underlying Input/OutputStream.
If you need to read or write ints, longs etc.. to a file, then these are the classes for you.
If instead you are just interested in how to convert then to bytes for other purposes, have a look at the source code of those classes, they convert to big-endian.

Classes supporting the DataOutput interface will do what you want. Use DataInput to read the stream back to data.

The standard encoding method used by Java when serializing is, just as your own thoughts, a simple translation of the fields into a byte stream.
Primitives as well as non-transient, non-static referenced objects are encoded into the stream. Each object that is referenced by the serialized object and must also be serialized.
Other languages, such as PHP for example, serializes to a pretty much human readable format and some implementations serialize to JSON or XML.
In my own mind though, true serialization should be binary byte-per-byte representation of the data. That way it's possible to quickly read all the data up into memory again and it can be executed as is.

Related

Parsing data from untrusted Java serialized object

I need to parse untrusted Java serialized objects. The data is given to me as a byte array (written at some point by ObjectOutputStream).
I do not want to simply call ObjectInputStream.readObject() and/or load the actual object. I am looking for a way to safely parse the bytes and grab field names & values.
--
Here's a little summary of my attempt so far, after taking a look at the ObjectInputStream procedure for deserializing objects.
I have tried to extract field types/names (as unicode strings) recursively based on expected stream constants. I end up with a list of field names whose values should appear in the byte array in order. I am uneasy about this approach because it is probably buggy. Especially accommodating for what seems to be individual serialization protocols followed by HashMap, ArrayList, etc. But it might work, if I can figure out a way to read the bytes that represent field values:
I can try to read and store primitives based on size/offset, but when I encounter my first object, it gets a bit more complicated -- there is no clear way to distinguish between which bytes are associated with which values anymore (without actually loading the object in the way that ObjectInputStream probably does?).
--
Can anyone suggest either a potential solution that I'm obviously looking past, or a trusted library that can help parse the serialized data without loading objects?
Thank you for reading, and for all comments/suggestions!!! I apologize if something is unclear and I would be happy to clarify if you bear with me.
You can't do this in principle. Any Java class can take over its own Serialization and write arbitrary data to the stream that only it knows how to parse and reconstruct, via code that is only invoked during deserialization.

Serialization, for converting Object to Binary?

Someone told me that we use serialization only to convert object to binary? How far is that correct?
It is correct 😁
Class to be serialized needs to implement Serializable or Externalizable
You may then use ObjectOutputStream to serialize and ObjectInputStream to deserialize. As these are IO streams they could be abstractions over a file, socket etc.
I don't think the point is converting an object to binary. The only reason why anyone uses serialization is object storage - either in a file or in a database. The object can then be unserialized for later usage.

Java Creating a packet byte array to send to server

How would one go about making a byte[] that is a packet that needs to be run through an RC4 encryption class and then sent to a server?
So let's say I need the packet to stsrt with a string, followed with an int, byte, int, string. How would I create that as a byte array? (Byte[])
Thanks!
OK, so you have a data structure containing strings, integers and bytes, that you want to serialize to a byte array. There are several options:
create a Serializable class containing all this information as fields, and use an ObjectOutputStream to write it. Beware: the result will only be easily readable by a Java program using the exact same class.
create a class containing all this information as fields, and use a JSON object mapper (like Jackson) to write it.
create a class containing all this information as fields, and use an XML object mapper (like JAXB) to write it.
design a binary representation of this structure, that can be transformed back into the individual parts, and use a DataOutputStream to write it.
Use protocol buffers
...

Java Object Array IO

I have an Array of Objects called "person" and i need to save that data, Is there a way to output an array and then input it back into the program. I have tried simply outputting the raw String and int data that makes up each "person" and the program never writes that data to the specified txt file. Is there a method or function that can write raw object data and then retrieve it?
You should have a look to ObjectOutputStream and ObjectOutputStream and the Serializable interface. What you are trying to do is to serialize your data to a file and load (deserialize) it back again.
You can use standard Java serialisation. Another trivial solution is to use XStream, which will serialise your objects into/from XML. You don't need to implement specific interfaces or annotate your objects (by default) and you'll get a readable (in XML terms) serialisation, which can be useful.
One approach would be to use Serialization provided by Java. You need to implement the java.io.Serializable interface and write and read to and from the ObjectOutputStream and ObjectInputStream classes to store and retrieve data respectively.
You need serialization. Read this.

Java: Serializing String[] Array to store in a MySQL Database?

Yes, I know it's bad practice and I should instead normalize my tables. That put aside, is it possible to serialize a String [] array and store it in the database?
I am from the lenient and forgiving world of PHP, where invoking the serialize() function and would convert the array into a string.
Is there an equivalent of doing such heresy in Java?
Apart from normalization, are there more elegant ways of storing String Arrays in the database?
In case it's applicable, I am using the jdbc driver for my MySQL connections.
Yes. You can serialize any Java objects and store the serialized data into MySQL.
If you use the regular serialization (ObjectOutputStream), the output is always binary. Even String is serialized into binary data. So you have to Base64 encode the stream or use a binary column like BLOB.
This is different from PHP, whose serialize() converts everything into text.
You can also use the XML serialization in Java (XMLEncoder) but it's very verbose.
If you're thinking in terms of raw arrays, you're still writing PHP in Java.
Java's an object-oriented language. An array of Strings really isn't much of an abstraction.
You'll get perfectly good advice here telling you that it's possible to serialize that array of Strings into a BLOB that you can readily store in MySQL, and you can tell yourself that leniency is a virtue.
But I'll going to remind you that you're losing something by not thinking in terms of objects. They're really about abstraction and encapsulation and dealing with things at a higher level than bare metal ints, Strings, and arrays.
It'd be a good exercise to try and design an object that might encapsulate an array or another more sophisticated data structure of child objects that were more than Strings. There'd be a 1:m relationship between parent and child that would better reflect the problem you were really trying to solve. That would be a far more object-oriented design than the one you're proposing here.
There are various good serialization/deserialization libraries that automatically convert JavaBean objects to/from XML and JSON strings. One I've had good experience with is XStream.
Java's built-in support for serialization can do the same thing, and you can write custom serialization/deserialization methods for Java to call.
You can roll your own serialization methods too, eg converting to and from a comma-separated value (CSV) format.
I'd opt for a library like XStream first, assuming there's a very compelling reason not to normalize the data.
You don't want to serialize the array. I'm not sure why you'd serialize it in PHP either, because implode() and explode() would be more appropriate. You really should normalize your data, but aside from that, you could very easily Google a solution for converting an array to a string.
But surely the more logical thing to do would be to save each string as its own record with a suitable identifier. That would probably be less coding than serializing -- a simple loop through the elements of the array -- and would result in a clean database design, rather than some gooey mess.
If you really don't want to normalize this values into a separate table where each string would be in its own row, then just convert your array to a list of comma separated values (possibly escaping commas somehow). Maybe quoting each string so that "str1","str2".
Google for CSV RFC for spec on how this should be properly escaped.

Categories

Resources