Anton Banchev
Anton Banchev

Reputation: 539

What does "zero bit padding in the end" mean

I am trying to debug a 3d file format exporter which sometimes exports files that aren't working everywhere(some programs open them some don't). While I was reading the documentation there seems to be a line that I cannot wrap my head around:

6.4 Objects

The objects in each section are serialized as an array of bytes, one after the other. This array of bytes will either be compressed (if CompressionScheme is 1) or it will be uncompressed. If it is compressed, it is compressed as a single chunk of data, not as separate objects. Zero bits must be padded in the end to make the Objects field byte aligned.

The documentation can be found at http://www.j2megame.org/j2meapi/JSR_184_Mobile_3D_Graphics_API_1_1/file-format.html

And this is the current code I am using:

protected void write(M3GOutputStream os,ArrayList table) throws IOException
{
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    writeObjects(new M3GOutputStream(baos),table);
    uncompressedLength=baos.size();
    byte data[]=baos.toByteArray();
            if(!M3GToolkit.useZlibCompression)
            compressionScheme = UNCOMPRESSED;
    if (compressionScheme==ZLIB)
    {
        Deflater deflater=new Deflater(Deflater.BEST_COMPRESSION,false);
        deflater.setInput(data);
        deflater.finish();
        byte compressed[]=new byte[data.length<<1];
        int length=deflater.deflate(compressed);
        data=new byte[length];
        System.arraycopy(compressed,0,data,0,length);
        deflater.end();
    }
    os.resetAdler32();
    os.writeByte(compressionScheme);
    os.writeUInt32(totalSectionLength=data.length+13);
    os.writeUInt32(uncompressedLength);
    os.write(data);
    os.writeUInt32(checksum=(int)os.getAdler32Value());
}

Upvotes: 0

Views: 2452

Answers (1)

Hampton Terry
Hampton Terry

Reputation: 354

If you have the binary value of "101", it must be padded to "00000101" assuming an 8 bit value.

Adding the leading zeros will not change the value, however it is needed so the number takes up the entire 8 bits. This is needed to ensure the value stored next to it will be aligned at the start of a byte.

Upvotes: 2

Related Questions