out.writeShort(state.getModCount());
// values
InternalValue[] values = state.getValues();
out.writeInt(values.length); // count
for (int i = 0; i < values.length; i++) {
InternalValue val = values[i];
if (state.getType() == PropertyType.BINARY) {
// special handling required for binary value:
// put binary value in BLOB store
InputStream in = val.getStream();
String blobId = blobStore.createId(state.getPropertyId(), i);
try {
blobStore.put(blobId, in, val.getLength());
} finally {
IOUtils.closeQuietly(in);
}
// store id of BLOB as property value
out.writeUTF(blobId); // value
// replace value instance with value backed by resource
// in BLOB store and discard old value instance (e.g. temp file)
if (blobStore instanceof ResourceBasedBLOBStore) {
// optimization: if the BLOB store is resource-based
// retrieve the resource directly rather than having
// to read the BLOB from an input stream
FileSystemResource fsRes =
((ResourceBasedBLOBStore) blobStore).getResource(blobId);
values[i] = InternalValue.create(fsRes);
} else {
in = blobStore.get(blobId);
try {
values[i] = InternalValue.create(in);
} finally {
IOUtils.closeQuietly(in);
}
}
val.discard();
} else {
/**
* because writeUTF(String) has a size limit of 65k,
* Strings are serialized as <length><byte[]>
*/
//out.writeUTF(val.toString()); // value
byte[] bytes = val.toString().getBytes(ENCODING);
out.writeInt(bytes.length); // lenght of byte[]
out.write(bytes); // byte[]
}
}
}