At the moment I’m at a most painful taskt of converting different Java types into byte-array for sending over the network.
I need to remember that Java stores everything in a 2complement notation.
But to handle all that stuff I’ve written my own Byte-O-Rama Class in Java. It’s a shit that Java isn’t even able to encode
its basic data-types into byte[] in a simple and convenient manner. I don’t talk ByteBuffers/writers and stupid stuff i talk
byte[] barr = (byte[]) intValue; Plain and simple Type-Conversion. Shouldn’t even be explicit. But since XML is overhyped in Java
no one seems to be concerned that anybody out there might use playn byte[] ’s as a fast, overhead-free Method to serialize his objects.
Python conveniently offers this: http://www.python.org/doc/current/lib/module-struct.html
If I’ve got time and anybody bothers I will go and publish my Byte-O-Rama.
one comment so far...
Die Begründung ist relativ einfach. Bei Java wurde sehr stark darauf geachtet, dass die VM von der Hardware-Architektur abgekapselt ist. Ein cast nach byte[] wurde die Objektdarstellung von der Hardware (Big-Endian, Little-Endian, 64bit/32bit, Byte Alignment, etc.) abhängig machen.
Außerdem ist bei der Entwicklung von Java auf die Typsicherheit in dem Sinne geachtet worden, dass keine ungültigen Objekte (Wild Pointer etc) erzeugt werden. Bei dem Weg Object x = (Object)byteArray könnte dies aber leicht passieren. Deshalb sind in Java nur Casts zwischen möglicherweise kompatiblen Typen erlaubt. Aber so einfach ist es ja auch bei Python’s struct nicht.
Der Defaultweg um Java-Objekte zu serialisien (für ein Flat-File, oder Netzwerklink) ist das java.io.Serializable Interface. Aber damit wird die On-the-Wire-Repräsentierung automatisch festgelegt. Mit java.io. Externalizable hast du schon eine ziemlich genau kontrolle über die Repräsentierung. Overhead für den Programmierer hat man so kaum (bei Serializable kann man einfach die Default-Repräsentierung nehmen), Overhead auf dem Wire ist definitiv vorhanden (z.B. schreibt Java automatisch bestimmte Klasseninformationen, um sicher zustellen, dass die originale Klasse kompatibel ist mit der Version der Klasse am anderen Ende der Leitung), aber i.d.R. wird es als effizient genug betrachtet.
leave a reply