objects.add(object);
return this;
}
public Buffer toBuffer() {
Buffer buffer = Buffer.create(length);
int offset = 0;
for (Object object : objects) {
if (object.getClass() == Boolean.class || "boolean".equals(object.getClass().getName())) {
buffer.set(offset, (byte) (((Boolean) object) ? 1 : 0));
offset++;
} else if (object.getClass() == Byte.class || "byte".equals(object.getClass().getName())) {
buffer.set(offset, (Byte) object);
offset++;
} else if (object.getClass() == Character.class || "char".equals(object.getClass().getName())) {
buffer.writeInt16BE((short) ((Character) object).charValue(), offset);
offset += 2;
} else if (object.getClass() == Short.class || "short".equals(object.getClass().getName())) {
buffer.writeInt16BE((Short) object, offset);
offset += 2;
} else if (object.getClass() == Integer.class || "int".equals(object.getClass().getName())) {
buffer.writeInt32BE((Integer) object, offset);
offset += 4;
} else if (object.getClass() == Float.class || "float".equals(object.getClass().getName())) {
buffer.writeFloatBE((Float) object, offset);
offset += 4;
} else if (object.getClass() == Double.class || "double".equals(object.getClass().getName())) {
buffer.writeDoubleBE((Double) object, offset);
offset += 8;
} else if (object.getClass() == String.class) {
buffer.writeInt32BE(Buffer.byteLength((String) object), offset);
buffer.write((String) object, offset + 4);
offset += BufferStream.getStringByteLength((String) object);
} else if (object instanceof Enum) {
buffer.set(offset, (byte) ((Enum<?>) object).ordinal());
offset++;
} else {
throw new IllegalArgumentException("Unknown object type: " + object.getClass());
}
}