this.setFieldsAsAccessible = setFieldsAsAccessible;
rebuildCachedFields();
}
public void writeObjectData (ByteBuffer buffer, Object object) {
Context context = Kryo.getContext();
if (context.getTemp(this, "schemaWritten") == null) {
context.putTemp(this, "schemaWritten", Boolean.TRUE);
if (TRACE) trace("kryo", "Writing " + fields.length + " field names.");
IntSerializer.put(buffer, fields.length, true);
for (int i = 0, n = fields.length; i < n; i++)
StringSerializer.put(buffer, fields[i].field.getName());
}
for (int i = 0, n = fields.length; i < n; i++) {
CachedField cachedField = fields[i];
try {
if (TRACE) trace("kryo", "Writing field: " + cachedField + " (" + object.getClass().getName() + ")");
Object value = cachedField.get(object);
if (value == null) {
kryo.writeClass(buffer, null);
continue;
}
int start = buffer.position();
try {
buffer.position(start + 1);
} catch (IllegalArgumentException ex) {
new BufferOverflowException();
}
Serializer serializer = cachedField.serializer;
if (cachedField.fieldClass == null) {
RegisteredClass registeredClass = kryo.writeClass(buffer, value.getClass());
if (serializer == null) serializer = registeredClass.getSerializer();
serializer.writeObjectData(buffer, value);
} else {
if (serializer == null)
cachedField.serializer = serializer = kryo.getRegisteredClass(cachedField.fieldClass).getSerializer();
if (!cachedField.canBeNull)
serializer.writeObjectData(buffer, value);
else
serializer.writeObject(buffer, value);
}
int dataLength = buffer.position() - start - 1;
if (dataLength <= 127) {
// Ideally it fits in one byte.
buffer.put(start, (byte)dataLength);
} else {
// Shift the data over to make room for the length.
byte[] temp = context.getByteArray(dataLength);
buffer.position(start + 1);
buffer.get(temp);
buffer.position(start);
IntSerializer.put(buffer, dataLength, true);
buffer.put(temp);