2.1 As discussed with Jun, since any use of javaapi/ByteBufferMessageSet must happen through the iterator, I have removed the getSerialized() method and made the buffer field private. The buffer field in non javaapi version of ByteBufferMessageSet is a public val. The getSerialized() method is removed from non javaapi version of ByteBufferMessageSet, MessageSet and FileMessageSet as well (where it merely threw an UnsupportedOperationException). This introduces a need for a typecast in ProducerRequest.writeTo(buffer) and it perhaps could look like another hack introduced to get rid of the UnsupportedOperationException hack in FileMessageSet. This could be taken care of with KAFKA-289.
2.2. Removed unused imports.
2.3 The hashCode method is now overridden. Jay, thanks for catching that!
2.4 Just to be sure, did you mean that the behaviour achieved by Either should be achieved by throwing an exception in KafkaApis.readMessageSet and catching it in KafkaApis.readMessageSets? That's what I have done in this patch.
2.5 Removed testValidBytes() in javaapi/ByteBufferMessageSetTest because it was merely running the test on underlying.validBytes and there is already another test that does the same thing on non javaapi version of ByteBufferMessageSet. Moved testValidBytesWithCompression() from javaapi/ByteBufferMessageSetTest to the other ByteBufferMessageSetTest.
There was a method to calculate hashcode in ByteBufferMessageSet, but it was never used. So I have removed it. Also removed the method hasError in KafkaETLContext.java as it was not used anymore.