CameraStreamAdapter extends TypeAdapter<CameraServerData> { @Override public CameraServerData deserialize(byte[] buffer, int bufferPosition) { int cursor = bufferPosition; final String name = readString(buffer, cursor); cursor += name.length() + SIZE_OF_INT; final byte fileNum = buffer[cursor]; cursor++; final short frameNum = readShort(buffer, cursor); cursor += SIZE_OF_SHORT; final int bandwidth = readInt(buffer, cursor); cursor += SIZE_OF_INT; final double fps = readShort(buffer, cursor) / 100.0; CameraStreamReader reader = readers.computeIfAbsent(name, __ -> new CameraStreamReader(__, getCurrentFile())); return new LazyCameraServerData(name, fileNum, frameNum, () -> { try { reader.setFileNumber(fileNum); return reader.readFrame(frameNum); } catch (IOException e) { log.log(Level.WARNING, "Could not read frame " + frameNum, e); return null; } }, fps, bandwidth); } CameraStreamAdapter(); @Override void flush(); @Override void cleanUp(); @Override CameraServerData deserialize(byte[] buffer, int bufferPosition); @Override int getSerializedSize(CameraServerData value); @Override byte[] serialize(CameraServerData data); static String videoFilePath(File rootRecordingFile, String cameraName, int fileIndex);  }
@Test public void testDeserialize() throws IOException { File file = File.createTempFile("test-recording", ".sbr"); adapter.setCurrentFile(file); CameraServerData data = new CameraServerData("name", null, (1 << 15) / 100, Integer.MAX_VALUE); byte[] bytes = adapter.serialize(data); adapter.cleanUp(); CameraServerData deserialize = adapter.deserialize(bytes, 0); deleteTempFiles(file); assertAll( () -> assertEquals(data.getName(), deserialize.getName(), "Wrong name"), () -> assertEquals(data.getFps(), deserialize.getFps(), "Wrong FPS"), () -> assertEquals(data.getBandwidth(), deserialize.getBandwidth(), "Wrong bandwidth"), () -> assertNull(deserialize.getImage(), "Image should be null (video file does not exist)") ); }