(MLUInt8 mcosData, Set<MLObjectPlaceholder> mcosPlaceholders)
| 428 | } |
| 429 | |
| 430 | private static void parseMCOS(MLUInt8 mcosData, Set<MLObjectPlaceholder> mcosPlaceholders) throws IOException { |
| 431 | // First, parse back out the mcosData. |
| 432 | ByteBuffer buffer = mcosData.getRealByteBuffer(); |
| 433 | ByteBufferInputStream dataStream = new ByteBufferInputStream(buffer, buffer.limit()); |
| 434 | |
| 435 | MatFileReader matFile = new MatFileReader(dataStream, MatFileType.ReducedHeader); |
| 436 | Map<String, MLArray> mcosContent = matFile.getContent(); |
| 437 | MLCell mcosInfo = (MLCell) ((MLStructure) mcosContent.get("@0")).getField("MCOS"); |
| 438 | ByteBuffer mcosDataBuf = ((MLUInt8) mcosInfo.get(0)).getRealByteBuffer(); |
| 439 | // This bytebuffer needs to be read in the byte order of the MAT file order. Thus fix. |
| 440 | mcosDataBuf.order(matFile.getMatFileHeader().getByteOrder()); |
| 441 | |
| 442 | // Parse out the data buffer. First get version information. Should always equal 2. |
| 443 | int version = mcosDataBuf.getInt(); |
| 444 | if (version != 2) { |
| 445 | throw new IllegalStateException("MAT file's MCOS data has a different version(?). Got: " + version + ", wanted 2."); |
| 446 | } |
| 447 | |
| 448 | // Get the string count + define the string array. |
| 449 | int strCount = mcosDataBuf.getInt(); |
| 450 | String[] strs = new String[strCount]; |
| 451 | |
| 452 | // Get the segment indexes. |
| 453 | int segmentIndexes[] = new int[6]; |
| 454 | for (int i = 0; i < segmentIndexes.length; ++i) { |
| 455 | segmentIndexes[i] = mcosDataBuf.getInt(); |
| 456 | } |
| 457 | |
| 458 | // There should now be 8 0 bytes. Make sure this is true to avoid object format changes. |
| 459 | if (mcosDataBuf.getLong() != 0) { |
| 460 | throw new IllegalStateException("MAT file's MCOS data has different byte values for unknown fields! Aborting!"); |
| 461 | } |
| 462 | |
| 463 | // Finally, read in each string. Java doesn't provide an easy way to do this in bulk, so just use a stupid formula for now. |
| 464 | StringBuilder sb = new StringBuilder(); |
| 465 | for (int i = 0; i < strCount; ++i) { |
| 466 | sb.setLength(0); |
| 467 | char next = (char) mcosDataBuf.get(); |
| 468 | while (next != '\0') { |
| 469 | sb.append(next); |
| 470 | next = (char) mcosDataBuf.get(); |
| 471 | } |
| 472 | strs[i] = sb.toString(); |
| 473 | } |
| 474 | |
| 475 | // Sanity check, next 8 byte aligned position in the buffer should equal the start of the first segment! |
| 476 | if (((mcosDataBuf.position() + 0x07) & ~0x07) != segmentIndexes[0]) { |
| 477 | throw new IllegalStateException("Data from the strings section was not all read!"); |
| 478 | } |
| 479 | |
| 480 | // First segment, class information. Really just need the class names. |
| 481 | List<String> classNamesList = new ArrayList<String>(); |
| 482 | mcosDataBuf.position(segmentIndexes[0]); |
| 483 | // There are 16 unknown bytes. Ensure they are 0. |
| 484 | if (mcosDataBuf.getLong() != 0 || mcosDataBuf.getLong() != 0) { |
| 485 | throw new IllegalStateException("MAT file's MCOS data has different byte values for unknown fields! Aborting!"); |
| 486 | } |
| 487 | while (mcosDataBuf.position() < segmentIndexes[1]) { |
no test coverage detected