### Question:
MessageFormatInputStream extends InputStream { public long getSize() { return messageLength; }  @Override int read(); @Override int read(byte b[], int off, int len); @Override int available(); long getSize();  }### Answer:
@Test public void messageFormatPutNoArgReadTest() throws Exception, MessageFormatException { StoreKey key = new MockId("id1"); StoreKeyFactory keyFactory = new MockIdFactory(); short accountId = Utils.getRandomShort(TestUtils.RANDOM); short containerId = Utils.getRandomShort(TestUtils.RANDOM); BlobProperties prop = new BlobProperties(10, "servid", accountId, containerId, false); byte[] encryptionKey = new byte[100]; new Random().nextBytes(encryptionKey); byte[] usermetadata = new byte[1000]; new Random().nextBytes(usermetadata); int blobContentSize = 2000; byte[] data = new byte[blobContentSize]; new Random().nextBytes(data); ByteBufferInputStream stream = new ByteBufferInputStream(ByteBuffer.wrap(data)); MessageFormatInputStream messageFormatStream = new PutMessageFormatInputStream(key, ByteBuffer.wrap(encryptionKey), prop, ByteBuffer.wrap(usermetadata), stream, blobContentSize, BlobType.DataBlob); TestUtils.validateInputStreamContract(messageFormatStream); TestUtils.readInputStreamAndValidateSize(messageFormatStream, messageFormatStream.getSize()); }
@Test public void messageFormatTtlUpdateRecordTest() throws IOException, MessageFormatException { StoreKey key = new MockId("id1"); short accountId = Utils.getRandomShort(TestUtils.RANDOM); short containerId = Utils.getRandomShort(TestUtils.RANDOM); long ttlUpdateTimeMs = SystemTime.getInstance().milliseconds() + TestUtils.RANDOM.nextInt(); long updatedExpiryMs = ttlUpdateTimeMs + TestUtils.RANDOM.nextInt(); short lifeVersion = 1; short oldMessageFormatHeaderVersion = MessageFormatRecord.headerVersionToUse; for (short messageFormatHeaderVersion : new short[]{MessageFormatRecord.Message_Header_Version_V2, MessageFormatRecord.Message_Header_Version_V3}) { MessageFormatRecord.headerVersionToUse = messageFormatHeaderVersion; MessageFormatInputStream messageFormatStream = new TtlUpdateMessageFormatInputStream(key, accountId, containerId, updatedExpiryMs, ttlUpdateTimeMs, lifeVersion); long ttlUpdateRecordSize = MessageFormatRecord.Update_Format_V3.getRecordSize(SubRecord.Type.TTL_UPDATE); int headerSize = MessageFormatRecord.getHeaderSizeForVersion(MessageFormatRecord.headerVersionToUse); Assert.assertEquals(headerSize + ttlUpdateRecordSize + key.sizeInBytes(), messageFormatStream.getSize()); checkTtlUpdateMessage(messageFormatStream, ttlUpdateRecordSize, key, accountId, containerId, updatedExpiryMs, ttlUpdateTimeMs, lifeVersion); } MessageFormatRecord.headerVersionToUse = oldMessageFormatHeaderVersion; }
@Test public void messageFormatUndeleteUpdateRecordTest() throws Exception { MessageFormatRecord.headerVersionToUse = MessageFormatRecord.Message_Header_Version_V3; StoreKey key = new MockId("id1"); short accountId = Utils.getRandomShort(TestUtils.RANDOM); short containerId = Utils.getRandomShort(TestUtils.RANDOM); long updateTimeMs = SystemTime.getInstance().milliseconds() + TestUtils.RANDOM.nextInt(); short lifeVersion = Utils.getRandomShort(TestUtils.RANDOM); MessageFormatInputStream messageFormatInputStream = new UndeleteMessageFormatInputStream(key, accountId, containerId, updateTimeMs, lifeVersion); long undeleteRecordSize = MessageFormatRecord.Update_Format_V3.getRecordSize(SubRecord.Type.UNDELETE); int headerSize = MessageFormatRecord.getHeaderSizeForVersion(MessageFormatRecord.Message_Header_Version_V3); Assert.assertEquals(headerSize + undeleteRecordSize + key.sizeInBytes(), messageFormatInputStream.getSize()); checkUndeleteMessage(messageFormatInputStream, undeleteRecordSize, key, accountId, containerId, updateTimeMs, lifeVersion); }