diff --git a/src/main/java/koz01/java2/lab08/IndexReader.java b/src/main/java/koz01/java2/lab08/IndexReader.java index 8ae3d5279801f840144d6c162792bc8cf9a2866e..252eefd640f10ce86822c8a1bc48c594744ee1d8 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() + + } }