Re: size of things
Tom A. wrote:
I'm trying to use a RandomAccessFile, and was having trouble figuring
out what size things are, so I can look past them on the file.
If you look at the Java doc for RandomAccessFile, it tells you what the
sizes of things are. Example:
void writeByte(int v)
Writes a byte to the file as a one-byte value.
void writeChar(int v)
Writes a char to the file as a two-byte value, high byte first.
void writeDouble(double v)
Converts the double argument to a long using the
doubleToLongBits method in class Double, and then writes that long value
to the file as an eight-byte quantity, high byte first.
So bytes are one byte, chars are two, and double is 8. Reading FTW.
So, after I build the file, I was wondering how to get the offset to
the "Offsets" area. I couldn't find any sort of "size" function that
will return the size in bytes of "Names".
AFAIK, there's no way to get the internal size of JVM primitives or
classes, and their external behavior (bits of precision, for example) is
defined by the spec. Their size in the RandomAccessFile is documented
in that class's Java doc, as I mentioned above.
Is there something that will do that? Or could I cast an arbitrary
array (of chars, longs, etc) into a byte[] and take the .length
attribute?
This seems to be a lot of trouble for nothing. Check out
java.nio.ByteBuffer for better "casting" options, imo. ByteBuffer
handles both big endian and little endian too.
2nd question
If I have an array of say boolean, barray, would I be able to
initialize it using the new for syntax with:
for(boolean b : barray ) b=false;
I think new arrays of primitives are initialized to 0/null/false just
like instance fields in new classes, but I didn't double check that.
However I think b just represents the value of your array elements, not
a reference to them, so no dice on the loop. Use indexes and access the
elements as normal for an array.