From 4126e3d7e0d307ac6f4b3970f20529d4227e79ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Mon, 29 Mar 2021 10:02:23 +0200 Subject: [PATCH] LocalReader solution --- .../java/koz01/java2/lab08/IndexReader.java | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/koz01/java2/lab08/IndexReader.java b/src/main/java/koz01/java2/lab08/IndexReader.java index 8ae3d52..252eefd 100644 --- a/src/main/java/koz01/java2/lab08/IndexReader.java +++ b/src/main/java/koz01/java2/lab08/IndexReader.java @@ -55,7 +55,48 @@ public class IndexReader { return LocalFile; } - private byte[] getData(FileChannel fc, int index) throws IOException { - return new byte[0]; + private byte[] getData(FileChannel fc, int i) throws IOException { + // 0...3 - number of images + // 4 ... 7 - Position of 1. image data in file + // 8 ... 11 - Lenght of 1. image + + // position_in_index = 4 + i*8 + + // position_of_image = readInt(position_in_index) + // size_of_image = readInt(position_in_index + 4) + + // data = readData(position_of_image, size_of_image) + // return data + + // go to position ... fc.position(position) + // read data .... fc.read(bb) - bb is ByteBuffer + + // create ByteBuffer 8 bytes long + ByteBuffer bb = ByteBuffer.allocate(8); + fc.position(0); + fc.read(bb); + bb.flip(); + int numberOfImages = bb.getInt(); + if (i < 0 || numberOfImages <= i) { + return new byte[0]; + } + bb.clear(); + fc.position(4 + i * 8); + fc.read(bb); + bb.flip(); + int position = bb.getInt(); + int size = bb.getInt(); + + bb = ByteBuffer.allocate(size); + fc.position(position); + fc.read(bb); + bb.flip(); + return bb.array(); + + // read data .... fc.read(bb) - bb is ByteBuffer + // bb.flip() + // bb.getInt(), bb.getInt() + + } } -- GitLab