Commit aff2d006 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Handle MCOS objects in arrays as non-MCOS object properties.

Since MLObject has been refactored to support this properly, now handle arrays
of MCOS.  Note that currently only 2d arrays are supported for MCOS objects
right now.
parent f7e67fdc
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -5,23 +5,28 @@ package ca.mjdsystems.jmatio.io;

import ca.mjdsystems.jmatio.types.MLArray;

import java.util.Arrays;

/**
 *
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
class MLObjectPlaceholder extends MLArray {
    MLObjectPlaceholder( String name, String className, int[][] information )
    MLObjectPlaceholder(String name, String className, int[] dims, int[][] information)
    {
        super( name, new int[] {1, 1}, -1, 0 );
        super( name, dims, -1, 0 );
        this.className = className;
        this.information = information;

        this.objectId = information[4][0];
        this.classId = information[5][0];
        this.objectIds = new int[information.length - 5];
        for (int i = 0; i < objectIds.length; ++i) {
            objectIds[i] = information[i + 4][0];
        }
        this.classId = information[information.length - 1][0];
    }

    final String className;
    final int[][] information;
    final int objectId;
    final int[] objectIds;
    final int classId;
}
+13 −5
Original line number Diff line number Diff line
@@ -604,9 +604,17 @@ public class MatFileReader

        for (Map.Entry<String, MLArray> it : data.entrySet()) {
            if ( it.getValue() instanceof MLObjectPlaceholder ) {
                MLObjectPlaceholder obj = (MLObjectPlaceholder) it.getValue();
                MatMCOSObjectInformation objectInformation = objectInfoList.get(obj.objectId - 1);
                it.setValue(new MLObject(obj.name, classNamesList.get(objectInformation.classId - 1), new int[]{1, 1}, 0).setFields(0, objectInformation.structure));
                MLObjectPlaceholder objHolder = (MLObjectPlaceholder) it.getValue();
                int classId = objHolder.classId;
                MLObject obj = new MLObject(objHolder.name, classNamesList.get(classId - 1), objHolder.getDimensions(), 0);
                it.setValue(obj);
                for (int i = 0; i < obj.getSize(); ++i) {
                    MatMCOSObjectInformation objectInformation = objectInfoList.get(objHolder.objectIds[i] - 1);
                    if (classId != objectInformation.classId) {
                        throw new IllegalStateException("Found an object in array with a different class id! Actual: " + objectInformation.classId + ", expected: " + classId + "!");
                    }
                    obj.setFields(i, objectInformation.structure);
                }
            }
        }
    }
@@ -1197,11 +1205,11 @@ public class MatFileReader
                            int[][] t = content.getArray();

                            // Check that the first four numbers are the same, as expected.
                            if (t[0][0] != 0xdd000000 || t[1][0] != 2 || t[2][0] != 1 || t[3][0] != 1) {
                            if (t[0][0] != 0xdd000000 || t[1][0] != 2) {
                                throw new IOException("MCOS per-object header was different then expected!  Got: " + content.contentToString());
                            }

                            mlArray = new MLObjectPlaceholder(arrName, className, t);
                            mlArray = new MLObjectPlaceholder(arrName, className, new int[]{t[2][0], t[3][0]}, t);
                            haveMCOS = true;
                        } else { // This is where we get the useful MCOS data.  Only used on FileWrapper__ classes.
                            mlArray = readMatrix(buf, false);
+21 −4
Original line number Diff line number Diff line
@@ -4,10 +4,7 @@
package ca.mjdsystems.jmatio.test;

import ca.mjdsystems.jmatio.io.MatFileReader;
import ca.mjdsystems.jmatio.types.MLArray;
import ca.mjdsystems.jmatio.types.MLChar;
import ca.mjdsystems.jmatio.types.MLInt8;
import ca.mjdsystems.jmatio.types.MLObject;
import ca.mjdsystems.jmatio.types.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -204,6 +201,26 @@ public class MatlabMCOSTest {
        assertThat(charField.getString(0), is("testing"));
    }

    @Test
    public void testMultipleMCOSInArray() throws IOException
    {
        File file = fileFromStream("/mcos/simplesingletext_multiplearray.mat");
        MatFileReader reader = new MatFileReader(file);
        Map<String, MLArray> content = reader.getContent();

        assertThat(content.size(), is(1));

        MLObject obj = (MLObject) content.get("a");
        assertThat(obj, is(notNullValue()));

        assertThat(obj.getName(), is("a"));
        assertThat(obj.getClassName(), is("SimpleSingleText"));
        assertThat(((MLDouble) obj.getFields(0).get("test_text")).get(0), is(1.0));
        assertThat(((MLDouble) obj.getFields(1).get("test_text")).get(0), is(2.0));
        assertThat(((MLDouble) obj.getFields(2).get("test_text")).get(0), is(3.0));
        assertThat(((MLDouble) obj.getFields(3).get("test_text")).get(0), is(4.0));
    }

    private File fileFromStream(String location) throws IOException
    {
        String outname = location.replace("/", "_");
+1.57 KiB

File added.

No diff preview for this file type.