Following is a sample class showing how I put String into ByteBuffer. I am able to write String to a file like this, but I am not sure how can I know the size of byte array to read the title back again when deserializing.
public class TestClass {
private Long id;
private String title;
public void write (final ByteBuffer byteBuffer) {
byteBuffer.putInt(title.length());
byteBuffer.put(title.getBytes());
}
public static UpdateFeed read (final ByteBuffer byteBuffer) {
final long id = byteBuffer.getLong();
final int titleLength = byteBuffer.getInt();
byte[] titleArr = new byte[titleLength];
byteBuffer.get(titleArr);
String title = new String(titleArr);
System.out.println("Title :"+title);
????????????????
return new TestClass(id,title);
}
}