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

When parsing the MAT file header, store the byte order used as well.

For applications parsing the MAT file later, they may need to know the byte
order of the original file.  Thus store that information in the header.  Note
for writing it is possible to change the endian indicator without changing the
byte order returned.  This should eventually throw an exception.
parent e0ac0db9
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
package ca.mjdsystems.jmatio.io;

import java.nio.ByteOrder;
import java.util.Date;

/**
@@ -17,6 +18,7 @@ public class MatFileHeader
                                                   + ", CREATED on: ";
    private static int DEFAULT_VERSION = 0x0100;
    private static byte[] DEFAULT_ENDIAN_INDICATOR = new byte[] {(byte)'M', (byte)'I'};
    private final ByteOrder byteOrder;

    private int version;
    private String description;
@@ -29,11 +31,12 @@ public class MatFileHeader
     * @param version - by default is set to 0x0100
     * @param endianIndicator - byte array size of 2 indicating byte-swapping requirement
     */
    public MatFileHeader(String description, int version, byte[] endianIndicator)
    public MatFileHeader(String description, int version, byte[] endianIndicator, ByteOrder byteOrder)
    {
        this.description = description;
        this.version = version;
        this.endianIndicator = endianIndicator;
        this.byteOrder = byteOrder;
    }
    
    /**
@@ -80,7 +83,8 @@ public class MatFileHeader
    {
        return new MatFileHeader( DEFAULT_DESCRIPTIVE_TEXT + (new Date()).toString(), 
                                    DEFAULT_VERSION, 
                                    DEFAULT_ENDIAN_INDICATOR);
                                    DEFAULT_ENDIAN_INDICATOR,
                                    ByteOrder.BIG_ENDIAN );
    }
    
    /* (non-Javadoc)
@@ -98,4 +102,9 @@ public class MatFileHeader
        return sb.toString();
    }

    public ByteOrder getByteOrder()
    {
        assert( (byteOrder != ByteOrder.LITTLE_ENDIAN || endianIndicator[0] == 'I') && (byteOrder != ByteOrder.BIG_ENDIAN || endianIndicator[0] == 'M') );
        return byteOrder;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -1225,7 +1225,7 @@ public class MatFileReader
        
        buf.order( byteOrder );
        
        matFileHeader = new MatFileHeader(description, version, endianIndicator);
        matFileHeader = new MatFileHeader(description, version, endianIndicator, byteOrder);

        // After the header, the next read must be aligned.  Thus force the alignment.  Only matters with reduced header data,
        // but apply it regardless for safety.