Example #1
0
        /// <summary>
        /// Reads the data into a <c>ByteBuffer</c>.
        /// </summary>
        /// <param name="dest">The destination <c>ByteBuffer</c></param>
        /// <param name="elements">The number of elements to read into a buffer</param>
        /// <param name="storage">The backing <c>ByteStorageSupport</c> that
        /// gives information on how data should be interpreted</param>
        /// <returns>Reference to the destination <c>ByteBuffer</c></returns>
        /// <exception cref="NotSupportedException">When attempting to read an unsupported
        /// class type from the buffer</exception>
        public ByteBuffer ReadToByteBuffer(ByteBuffer dest, int elements,
                                           ByteStorageSupport storage)
        {
            int bytesAllocated = storage.GetBytesAllocated;
            int size           = elements * bytesAllocated;

            // direct buffer copy
            if (MatDataTypes.SizeOf(_type) == bytesAllocated)
            {
                int bufMaxSize = 1024;
                int bufSize    = Math.Min((int)(_buf.BaseStream.Length - _buf.BaseStream.Position), bufMaxSize);
                int bufPos     = (int)_buf.BaseStream.Position;

                byte[] tmp = new byte[bufSize];

                while (dest.Remaining() > 0)
                {
                    int length = Math.Min(dest.Remaining(), tmp.Length);
                    _buf.Read(tmp, 0, length);
                    dest.Put(tmp, 0, length);
                }
                _buf.BaseStream.Position = bufPos + size;
            }
            else
            {
                // Because Matlab writes data not respectively to the declared
                // matrix type, the reading is not straight forward (as above)
                Type clazz = storage.GetStorageType;
                while (dest.Remaining() > 0)
                {
                    if (clazz.Equals(typeof(double)))
                    {
                        dest.PutDouble(ReadDouble());
                        continue;
                    }
                    if (clazz.Equals(typeof(byte)))
                    {
                        dest.PutDouble(ReadByte());
                        continue;
                    }
                    if (clazz.Equals(typeof(int)))
                    {
                        dest.PutDouble(ReadInt());
                        continue;
                    }
                    if (clazz.Equals(typeof(long)))
                    {
                        dest.PutDouble(ReadLong());
                        continue;
                    }
                    throw new NotSupportedException("Not supported buffer reader for " + clazz);
                }
            }
            dest.Rewind();
            return(dest);
        }
            ///// <summary>
            ///// Gets the size of the <c>ISMatTag</c>
            ///// </summary>
            //public int Size
            //{
            //    get { return (int)Buf.BaseStream.Length; }
            //}

            /// <summary>
            /// Read MAT-file tag to a byte buffer.
            /// </summary>
            /// <param name="buff"><c>ByteBuffer</c></param>
            /// <param name="storage"><c>ByteStorageSupport</c></param>
            public void ReadToByteBuffer(ByteBuffer buff, ByteStorageSupport storage)
            {
                MatFileInputStream mfis = new MatFileInputStream(Buf, _type);
                int elements            = _size / SizeOf();

                mfis.ReadToByteBuffer(buff, elements, storage);
                //skip padding
                if (padding > 0)
                {
                    Buf.ReadBytes(padding);
                }
            }
 ///// <summary>
 ///// Gets the size of the <c>ISMatTag</c>
 ///// </summary>
 //public int Size
 //{
 //    get { return (int)Buf.BaseStream.Length; }
 //}
 /// <summary>
 /// Read MAT-file tag to a byte buffer.
 /// </summary>
 /// <param name="buff"><c>ByteBuffer</c></param>
 /// <param name="storage"><c>ByteStorageSupport</c></param>
 public void ReadToByteBuffer( ByteBuffer buff, ByteStorageSupport storage )
 {
     MatFileInputStream mfis = new MatFileInputStream( Buf, _type );
     int elements = _size / SizeOf();
     mfis.ReadToByteBuffer( buff, elements, storage );
     //skip padding
     if( padding > 0 )
         Buf.ReadBytes( padding );
 }
        /// <summary>
        /// Reads the data into a <c>ByteBuffer</c>.
        /// </summary>
        /// <param name="dest">The destination <c>ByteBuffer</c></param>
        /// <param name="elements">The number of elements to read into a buffer</param>
        /// <param name="storage">The backing <c>ByteStorageSupport</c> that
        /// gives information on how data should be interpreted</param>
        /// <returns>Reference to the destination <c>ByteBuffer</c></returns>
        /// <exception cref="NotSupportedException">When attempting to read an unsupported
        /// class type from the buffer</exception>
        public ByteBuffer ReadToByteBuffer( ByteBuffer dest, int elements, 
			ByteStorageSupport storage )
        {
            int bytesAllocated = storage.GetBytesAllocated;
            int size = elements * bytesAllocated;

            // direct buffer copy
            if( MatDataTypes.SizeOf( _type ) == bytesAllocated )
            {
                int bufMaxSize = 1024;
                int bufSize = Math.Min( (int)(_buf.BaseStream.Length - _buf.BaseStream.Position), bufMaxSize );
                int bufPos = (int)_buf.BaseStream.Position;

                byte[] tmp = new byte[ bufSize ];

                while( dest.Remaining() > 0 )
                {
                    int length = Math.Min(dest.Remaining(), tmp.Length);
                    _buf.Read( tmp, 0, length );
                    dest.Put( tmp, 0, length );
                }
                _buf.BaseStream.Position = bufPos + size;
            }
            else
            {
                // Because Matlab writes data not respectively to the declared
                // matrix type, the reading is not straight forward (as above)
                Type clazz = storage.GetStorageType;
                while( dest.Remaining() > 0 )
                {
                    if( clazz.Equals( typeof(double) ) )
                    {
                        dest.PutDouble( ReadDouble() );
                        continue;
                    }
                    if( clazz.Equals( typeof(byte) ) )
                    {
                        dest.PutDouble( ReadByte() );
                        continue;
                    }
                    if( clazz.Equals( typeof(int) ) )
                    {
                        dest.PutDouble( ReadInt() );
                        continue;
                    }
                    if( clazz.Equals( typeof(long) ) )
                    {
                        dest.PutDouble( ReadLong() );
                        continue;
                    }
                    throw new NotSupportedException("Not supported buffer reader for " + clazz );
                }
            }
            dest.Rewind();
            return dest;
        }