Writing bitset to file in java

I have a BitSet and you want to write it to a file - I came across a decision to use ObjectOutputStream using the writeObject method.

I looked at ObjectOutputStream in the java API and saw that you can write other things (bytes, int, short, etc.)

I tried to check the class, so I tried to write a byte to a file using the following code, but the result gives me a file with 7 bytes instead of 1 byte

My question is: what are the first 6 bytes in the file? why are they there?

my question relates to BitSet, because I do not want to start writing a large amount of data to a file and understand that I have random bytes inserted into the file, not knowing what they are.

here is the code:

byte[] bt = new byte[]{'A'}; File outFile = new File("testOut.txt"); FileOutputStream fos = new FileOutputStream(outFile); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.write(bt); oos.close(); 

thanks for any help

Avner

+6
java objectoutputstream bitset
source share
4 answers

Other bytes will have type information.

Basically, an ObjectOutputStream is a class used to write Serializable objects to some destination (usually a file). This makes sense if you are thinking of an InputObjectStream. It has a readObject () method on it. How does Java know which object to create? Easy: there is type information.

+2
source share

You can write any objects to an ObjectOutputStream , so the stream contains information about the written types, as well as data necessary to restore the object.

If you know that a stream will always contain a BitSet, do not use an ObjectOutputStream - and if the space is a premium class, then convert the BitSet to a byte set where each bit corresponds to a bit in a BitSet , then write it directly to the underlying stream (e.g. FileOutputStream as in your example).

+1
source share

The serialization format, like many others, includes a header with a magic number and version information. When you use the DataOutput / OutputStream on an ObjectOutputStream , they are placed in the middle of serialized data ( without type information ). This is usually done only in writeObject implementations after calling defaultWriteObject or using putFields .

0
source share

If you use only saved BitSet in Java, serialization works fine. However, this is annoying if you want to share your beta on multiple platforms. In addition to the overhead of serializing Java, BitSet is stored in units of 8 bytes. This can be too costly if your bitset is small.

We wrote this little class so that we can output byte arrays from BitSet. Depending on your use, it may work better than Java serialization.

 public class ExportableBitSet extends BitSet { private static final long serialVersionUID = 1L; public ExportableBitSet() { super(); } public ExportableBitSet(int nbits) { super(nbits); } public ExportableBitSet(byte[] bytes) { this(bytes == null? 0 : bytes.length*8); for (int i = 0; i < size(); i++) { if (isBitOn(i, bytes)) set(i); } } public byte[] toByteArray() { if (size() == 0) return new byte[0]; // Find highest bit int hiBit = -1; for (int i = 0; i < size(); i++) { if (get(i)) hiBit = i; } int n = (hiBit + 8) / 8; byte[] bytes = new byte[n]; if (n == 0) return bytes; Arrays.fill(bytes, (byte)0); for (int i=0; i<n*8; i++) { if (get(i)) setBit(i, bytes); } return bytes; } protected static int BIT_MASK[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; protected static boolean isBitOn(int bit, byte[] bytes) { int size = bytes == null ? 0 : bytes.length*8; if (bit >= size) return false; return (bytes[bit/8] & BIT_MASK[bit%8]) != 0; } protected static void setBit(int bit, byte[] bytes) { int size = bytes == null ? 0 : bytes.length*8; if (bit >= size) throw new ArrayIndexOutOfBoundsException("Byte array too small"); bytes[bit/8] |= BIT_MASK[bit%8]; } } 
0
source share

All Articles