* @return The size of the Strings Entry that was written, if any.
*/
int writeInterval(ByteBuffer buffer, int endPosOfStringEntry) {
- int sizeOfStringEntry;
- byte[] byteArrayToWrite;
-
buffer.putLong(start);
buffer.putLong(end);
buffer.putInt(attribute);
*/
e.printStackTrace();
}
- return 0; /* we didn't use a Strings section entry */
+ break;
case TYPE_STRING:
- byteArrayToWrite = sv.toByteArray();
- /*
- * Size to write (+2 = +1 for size at the start, +1 for the 0 at the
- * end)
- */
- sizeOfStringEntry = byteArrayToWrite.length + 2;
+ byte[] byteArrayToWrite;
+ try {
+ byteArrayToWrite = sv.unboxStr().getBytes();
+ } catch (StateValueTypeException e1) {
+ /* Should not happen, we're in a switch/case for string type */
+ throw new RuntimeException();
+ }
/* we use the valueOffset as an offset. */
- buffer.putInt(endPosOfStringEntry - sizeOfStringEntry);
+ buffer.putInt(endPosOfStringEntry - stringsEntrySize);
buffer.mark();
- buffer.position(endPosOfStringEntry - sizeOfStringEntry);
+ buffer.position(endPosOfStringEntry - stringsEntrySize);
/*
* write the Strings entry (1st byte = size, then the bytes, then the 0)
*/
- buffer.put((byte) sizeOfStringEntry);
+ buffer.put((byte) stringsEntrySize);
buffer.put(byteArrayToWrite);
buffer.put((byte) 0);
assert (buffer.position() == endPosOfStringEntry);
buffer.reset();
- return sizeOfStringEntry;
+ break;
case TYPE_LONG:
- /*
- * Size to write is the number of bytes in a Long
- */
- sizeOfStringEntry = 8;
-
/* we use the valueOffset as an offset. */
- buffer.putInt(endPosOfStringEntry - sizeOfStringEntry);
+ buffer.putInt(endPosOfStringEntry - stringsEntrySize);
buffer.mark();
- buffer.position(endPosOfStringEntry - sizeOfStringEntry);
+ buffer.position(endPosOfStringEntry - stringsEntrySize);
/*
* write the Long in the Strings section
}
assert (buffer.position() == endPosOfStringEntry);
buffer.reset();
- return sizeOfStringEntry;
+ break;
default:
- return 0;
+ break;
}
+ return stringsEntrySize;
}
@Override
}
private int computeStringsEntrySize() {
- if (sv.toByteArray() == null) {
+ switch(sv.getType()) {
+ case NULL:
+ case INTEGER:
+ /* Those don't use the strings section at all */
return 0;
+ case LONG:
+ /* The value's bytes are written directly into the strings section */
+ return 8;
+ case STRING:
+ try {
+ /* String's length + 2 (1 byte for size, 1 byte for \0 at the end */
+ return sv.unboxStr().getBytes().length + 2;
+ } catch (StateValueTypeException e) {
+ /* We're inside a switch/case for the string type, can't happen */
+ throw new RuntimeException();
+ }
+ default:
+ /* It's very important that we know how to write the state value in
+ * the file!! */
+ throw new RuntimeException();
}
- return sv.toByteArray().length + 2;
- /* (+1 for the first byte indicating the size, +1 for the 0'ed byte) */
}
/**
*/
protected abstract Object getValue();
- /**
- * Specify how to "serialize" this value when writing it to a file.
- * Alternatively you can return "null" here if you do not need a byte-array
- * indirection (the getValue will get written as-in instead of the offset in
- * the file block)
- *
- * @return The state value in byte array form
- */
- public abstract byte[] toByteArray();
-
@Override
public boolean equals(Object other) {
if (this == other) {
return this.getValue().hashCode();
}
- /**
- * Return the max size that a variable-length state value can have when
- * serialized.
- *
- * @return The maximum size in bytes
- */
- public static int getStateValueMaxSize() {
- return Byte.MAX_VALUE;
- }
-
/*
* Since all "null state values" are the same, we only need one copy in
* memory.