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

When multiple empty keys are found, make sure they are all stored in the output.

Even though most users won't run against this, in case multiple empty keys are
found, store them all under @? where ? is a 0-starting index.  Also include a
modified MATLAB file to test these conditions.  Note that MATLAB just ignores
all three inputs.
parent 0b202b02
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -617,12 +617,16 @@ public class MatFileReader
                
                MLArray element = readMatrix( buf, true );
       
                if ( element != null && !data.containsKey( element.getName() ) )
                {
                if ( element != null ) {
                    if ( !data.containsKey( element.getName() ) ) {
                        data.put(element.getName(), element);
                    }
                else
                {
                    if ( element.getName() == "@" ) {
                        int nextIndex = 0;
                        for( ; data.containsKey("@" + nextIndex); nextIndex++ ) { }
                        data.put( "@" + nextIndex, element );
                    }
                } else {
                    int red = buf.position() - pos;
                    int toread = tag.size - red;
                    buf.position( buf.position() + toread );
+19 −0
Original line number Diff line number Diff line
package com.jmatio.test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.BufferedOutputStream;
@@ -1073,4 +1075,21 @@ public class MatIOTest
        assertEquals( expected[2], mlchar.getString(2) );
        assertEquals( expected[3], mlchar.getString(3) );
    }

    @Test
    public void testMultipleEmptyNames() throws IOException
    {
        File f = fileFromStream("/emptyname.mat");
        MatFileReader r = new MatFileReader(f);
        Map<String, MLArray> content = r.getContent();

        // There are 3 elements in that file, should have 4 in the array (the first value under @, and the three values under @*)
        assertThat(content.size(), is(4));

        // Check the three values came through alright.  Order is fixed as they are read in in file order.  Also check @.
        assertThat(((MLDouble)content.get("@")).get(0), is(1.0));
        assertThat(((MLDouble)content.get("@0")).get(0), is(1.0));
        assertThat(((MLDouble)content.get("@1")).get(0), is(2.0));
        assertThat(((MLDouble)content.get("@2")).get(0), is(3.0));
    }
}
+296 B

File added.

No diff preview for this file type.