Example #1
0
        /// <summary>
        /// Writes MAT-file header into <c>Stream</c>
        /// </summary>
        /// <param name="stream">The output stream</param>
        private void WriteHeader(BinaryWriter stream)
        {
            // write descriptive text
            MatFileHeader header = MatFileHeader.CreateHeader();

            char[] dest = new char[116];
            char[] src  = header.Description.ToCharArray();
            Array.Copy(src, 0, dest, 0, src.Length);

            byte[] endianIndicator = header.EndianIndicator;

            // ByteBuffer buf = new ByteBuffer( dest.Length * 2 + /* char size */ + 2 + endianIndicator.Length );
            ByteBuffer buf = new ByteBuffer(128); // The header is always a 128-byte header

            for (int i = 0; i < dest.Length; i++)
            {
                buf.Put((byte)dest[i]);
            }
            // write subsyst data offset
            buf.Position(buf.Position() + 8);

            // write version
            int version = header.Version;

            buf.PutShort((short)version);

            buf.Put(endianIndicator);

            stream.Write(buf.Array());
        }
Example #2
0
        /// <summary>
        /// Read Mat-file header.
        /// </summary>
        /// <param name="buf"><c>BinaryReader</c> input stream</param>
        private void ReadHeader(Stream buf)
        {
            string       description;
            int          version;
            BinaryReader br = new BinaryReader(buf);

            byte[] endianIndicator = new byte[2];

            // descriptive text 116 bytes
            byte[] descriptionBuffer = new byte[116];
            br.Read(descriptionBuffer, 0, descriptionBuffer.Length);

            description = ZeroEndByteArrayToString(descriptionBuffer);

            if (!description.StartsWith("MATLAB 5.0 MAT-file"))
            {
                throw new MatlabIOException("This is not a valid MATLAB 5.0 MAT-file.");
            }

            // subsyst data offset 8 bytes
            br.ReadBytes(8);

            byte[] bversion = new byte[2];
            // version 2 bytes
            br.Read(bversion, 0, bversion.Length);

            //endian indicator 2 bytes
            br.Read(endianIndicator, 0, endianIndicator.Length);

            // Program reading the MAT-file must perform byte swapping to interpret the data
            // in the MAT-file correctly
            if ((char)endianIndicator[0] == 'I' && (char)endianIndicator[1] == 'M')
            {
                // We have a Little Endian MAT-file
                version = bversion[1] & 0xff | bversion[0] << 8;
            }
            else
            {
                // right now, this version of MatNETIO does not support Big Endian
                throw new MatlabIOException("This version of MatNETIO does not support Big Endian.");
            }

            _matFileHeader = new MatFileHeader(description, version, endianIndicator);
        }