@@ -126,7 +126,14 @@ private static FractalHeapHeader readHeader(SeekableByteChannel channel, HdfData
126126 }
127127 channel .read (headerBuffer );
128128 h .checksum = Integer .toUnsignedLong (headerBuffer .getInt ());
129- h .offsetBytes = (h .maximumHeapSize + 7 ) / 8 ;
129+ int offsetBytesSize = (h .maximumHeapSize + 7 ) / 8 ;
130+ h .offsetBytes = new FixedPointDatatype (
131+ FixedPointDatatype .createClassAndVersion (),
132+ FixedPointDatatype .createClassBitField (false , false , false , false ),
133+ offsetBytesSize ,
134+ 0 , 8 * offsetBytesSize , hdfDataFile );
135+ ;
136+
130137 double logVal = Math .log ((double ) h .maximumDirectBlockSize .getInstance (Long .class ) / h .startingBlockSize .getInstance (Long .class )) / Math .log (2 );
131138 h .maxDblockRows = (int ) Math .floor (logVal ) + 1 ;
132139 return h ;
@@ -234,34 +241,40 @@ private static FilterPipeline parseFilterPipeline(byte[] data) throws IOExceptio
234241 return fp ;
235242 }
236243
237- private static Block readDirectBlock (HdfReader reader , FractalHeapHeader header , long address , long expectedBlockOffset , long filteredSize , int filterMask ) throws IOException {
244+ private static Block readDirectBlock (SeekableByteChannel channel , HdfDataFile hdfDataFile , FractalHeapHeader header , long address , HdfFixedPoint expectedBlockOffset , long filteredSize , int filterMask ) throws IOException , InvocationTargetException , InstantiationException , IllegalAccessException {
245+ FixedPointDatatype sizeOfOffset = hdfDataFile .getSuperblock ().getFixedPointDatatypeForOffset ();
238246 long undefined = -1L ;
239247 if (address == undefined ) {
240248 throw new IOException ("Invalid direct block address" );
241249 }
242- reader .seek (address );
243- String sig = new String (reader .readBytes (4 ));
250+ channel .position (address );
251+ int directBlockSize = getDirectBlockSize (header , expectedBlockOffset .getInstance (Long .class ), 8 , filteredSize , filterMask );
252+ ByteBuffer bb = ByteBuffer .allocateDirect (directBlockSize ).order (ByteOrder .LITTLE_ENDIAN );
253+ channel .read (bb );
254+
255+ String sig = new String (Arrays .copyOfRange (bb .array (), 0 , 4 ), StandardCharsets .US_ASCII );
244256 if (!Objects .equals (sig , "FHDB" )) {
245257 throw new IOException ("Invalid direct block signature" );
246258 }
247- byte version = reader .readByte ();
259+ bb .position (4 );
260+ byte version = bb .get ();
248261 if (version != 0 ) {
249262 throw new IOException ("Unsupported version" );
250263 }
251- long heapHeader = reader . readVariableLong ( header . sizeOfOffsets );
252- if (heapHeader == undefined ) {
264+ HdfFixedPoint heapHeader = HdfReadUtils . readHdfFixedPointFromBuffer ( sizeOfOffset , bb );
265+ if (heapHeader . isUndefined () ) {
253266 throw new IOException ("Invalid heap header address in direct block" );
254267 }
255- long blockOffset = reader . readVariableLong (header .offsetBytes );
256- if (blockOffset != expectedBlockOffset ) {
268+ HdfFixedPoint blockOffset = HdfReadUtils . readHdfFixedPointFromBuffer (header .offsetBytes , bb );
269+ if (blockOffset . compareTo ( expectedBlockOffset ) != 0 ) {
257270 throw new IOException ("Block offset mismatch" );
258271 }
259- int checksum = 0 ;
272+ long checksum = 0 ;
260273 if (header .checksumDirect ) {
261- checksum = reader . readInt ( ); // checksum, not verifying
274+ checksum = Integer . toUnsignedLong ( bb . getInt () ); // checksum, not verifying
262275 }
263- int headerSize = 4 + 1 + header . sizeOfOffsets + header .offsetBytes + (header .checksumDirect ? 4 : 0 );
264- long blockSize = getBlockSize (header , expectedBlockOffset );
276+ int headerSize = 4 + 1 + sizeOfOffset . getSize () + header .offsetBytes . getSize () + (header .checksumDirect ? 4 : 0 );
277+ long blockSize = getBlockSize (header , expectedBlockOffset . getInstance ( Long . class ) );
265278 long dataSize ;
266279// if (filteredSize != -1) {
267280// dataSize = filteredSize - headerSize;
@@ -276,23 +289,55 @@ private static Block readDirectBlock(HdfReader reader, FractalHeapHeader header,
276289 if (dataSize < 0 ) {
277290 throw new IOException ("Invalid data size in direct block" );
278291 }
279- byte [] data = reader .readBytes ((int ) dataSize );
292+ ByteBuffer dbBlockBuffer = ByteBuffer .allocate ((int ) dataSize );
293+ channel .read (dbBlockBuffer );
280294 DirectBlock db = new DirectBlock ();
281295 db .blockOffset = blockOffset ;
282296 db .blockSize = blockSize ;
283- db .data = data ;
297+ db .data = dbBlockBuffer . array () ;
284298 db .filterMask = filterMask ;
285299 db .checksum = checksum ;
286300 db .headerSize = headerSize ;
287301 return db ;
288302 }
289303
290- private static Block readIndirectBlock (HdfReader reader , FractalHeapHeader header , long address , long expectedBlockOffset , long iblockSize , short passedNrows ) throws IOException {
304+ private static int getDirectBlockSize (FractalHeapHeader header , long expectedBlockOffset , int sizeOfOffset , long filteredSize , int filterMask ) throws IOException {
305+ int size = 0 ;
306+ size += 4 ; // sig
307+ size += 1 ; // version
308+ size += sizeOfOffset ; // heapHeader
309+ size += sizeOfOffset ; // blockOffset
310+ if (header .checksumDirect ) {
311+ size += 4 ; // checksum
312+ }
313+ int headerSize = 4 + 1 + sizeOfOffset + header .offsetBytes .getSize () + (header .checksumDirect ? 4 : 0 );
314+ long blockSize = getBlockSize (header , expectedBlockOffset );
315+ long dataSize ;
316+ // if (filteredSize != -1) {
317+ // dataSize = filteredSize - headerSize;
318+ // } else {
319+ // dataSize = blockSize - headerSize;
320+ // }
321+ if (filteredSize != -1 ) {
322+ dataSize = filteredSize ;
323+ } else {
324+ dataSize = blockSize ;
325+ }
326+ if (dataSize < 0 ) {
327+ throw new IOException ("Invalid data size in direct block" );
328+ }
329+ size += ((int ) dataSize );
330+ return size ;
331+ }
332+
333+ private static Block readIndirectBlock (SeekableByteChannel channel , FractalHeapHeader header , long address , long expectedBlockOffset , long iblockSize , short passedNrows ) throws IOException {
291334 long undefined = -1L ;
292335 if (address == undefined ) {
293336 throw new IOException ("Invalid indirect block address" );
294337 }
295- reader .seek (address );
338+ channel .position (address );
339+
340+
296341 String sig = new String (reader .readBytes (4 ));
297342 if (!Objects .equals (sig , "FHIB" )) {
298343 throw new IOException ("Invalid indirect block signature" );
@@ -426,7 +471,7 @@ public static class FractalHeapHeader {
426471 long filterMaskRoot ;
427472 FilterPipeline filterPipeline ;
428473 long checksum ;
429- int offsetBytes ;
474+ FixedPointDatatype offsetBytes ;
430475 int maxDblockRows ;
431476 boolean hasFilters ;
432477 boolean checksumDirect ;
@@ -445,15 +490,15 @@ public static class FilterPipeline {
445490 }
446491
447492 public static abstract class Block {
448- long blockOffset ;
493+ HdfFixedPoint blockOffset ;
449494 }
450495
451496 public static class DirectBlock extends Block {
452497 public int headerSize ;
453498 long blockSize ;
454499 byte [] data ;
455500 int filterMask ;
456- int checksum ;
501+ long checksum ;
457502 }
458503
459504 public static class IndirectBlock extends Block {
0 commit comments