Exemple #1
0
        /// <summary>
        /// <see cref="Object.ToString()"/>
        /// </summary>
        public override string ToString()
        {
            string s;

            s = "[tag: " + MatDataTypes.TypeToString(_type) + " size: " + _size + "]";
            return(s);
        }
Exemple #2
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);
        }
Exemple #3
0
        /// <summary>
        /// Reads data (number of bytes read is determined by <i>data type</i>)
        /// from the stream to <c>int</c>.
        /// </summary>
        /// <returns><c>int</c></returns>
        /// <exception cref="ArgumentException">If input stream type is not known</exception>
        public int ReadInt()
        {
            switch (_type)
            {
            case MatDataTypes.miUINT8:
            case MatDataTypes.miUTF8:
                return(_buf.ReadByte() & 0xff);

            case MatDataTypes.miINT8:
                return(_buf.ReadByte());

            case MatDataTypes.miUINT16:
            case MatDataTypes.miUTF16:
                return(_buf.ReadInt16() & 0xFFFF);

            case MatDataTypes.miINT16:
                return(_buf.ReadInt16());

            case MatDataTypes.miUINT32:
            case MatDataTypes.miUTF32:
                return((int)(_buf.ReadInt32() & 0xFFFFFFFF));

            case MatDataTypes.miINT32:
                return(_buf.ReadInt32());

            case MatDataTypes.miUINT64:
                return((int)_buf.ReadInt64());

            case MatDataTypes.miINT64:
                return((int)_buf.ReadInt64());

            case MatDataTypes.miSINGLE:
                return((int)_buf.ReadSingle());

            case MatDataTypes.miDOUBLE:
                return((int)_buf.ReadDouble());

            default:
                throw new ArgumentException("Unknown data type: " +
                                            MatDataTypes.TypeToString(_type));
            }
        }
Exemple #4
0
 /// <summary>
 /// Get size of single data in this tag.
 /// </summary>
 /// <returns>The number of bytes for single data</returns>
 public int SizeOf()
 {
     return(MatDataTypes.SizeOf(_type));
 }
Exemple #5
0
 /// <summary>
 /// Get size of single data in this tag.
 /// </summary>
 /// <returns>The number of bytes for single data</returns>
 public int SizeOf() => MatDataTypes.SizeOf(_type);