package com.sibilantsolutions.grison.net.netty.codec;

import java.util.function.Function;

import com.sibilantsolutions.grison.driver.foscam.dto.VideoDataTextDto;
import com.sibilantsolutions.grison.driver.foscam.type.FosInt32;
import com.sibilantsolutions.grison.driver.foscam.type.FosInt8;
import com.sibilantsolutions.grison.net.netty.codec.parse.NettyFosTypeReader;
import io.netty.buffer.ByteBuf;

public class VideoDataTextDtoParser implements Function<ByteBuf, VideoDataTextDto> {
    @Override
    public VideoDataTextDto apply(ByteBuf buf) {
        final FosInt32 timestamp = NettyFosTypeReader.fosInt32(buf);
        final FosInt32 framePerSec = NettyFosTypeReader.fosInt32(buf);
        final FosInt8 reserve = NettyFosTypeReader.fosInt8(buf);
        final FosInt32 videoLength = NettyFosTypeReader.fosInt32(buf);
        byte[] videoData = new byte[videoLength.value()];
        buf.readBytes(videoData);

        return VideoDataTextDto.builder()
                .timestamp(timestamp)
                .framePerSec(framePerSec)
                .reserve(reserve)
                .videoLength(videoLength)
                .videoData(videoData)
                .build();
    }
}

@@ -286,16 +286,94 @@
 osInt8;%0A
+import com.sibilantsolutions.grison.net.netty.codec.parse.NettyByteBufHelper;%0A
 import c
@@ -911,89 +911,269 @@
     
-byte%5B%5D
+//Copy the
  video
-D
+ d
 ata 
-= new byte%5BvideoLength.value()%5D;%0A        buf.readBytes(videoData
+out of the buffer into a new array.  Two reasons: we don't want downstream to depend%0A        //on Netty, and we can't count on downstream to release a buffer.%0A        byte%5B%5D videoData = NettyByteBufHelper.readBytes(videoLength.value(), buf
 );%0A%0A

Use the readBytes helper to create the byte[] array for video data.