* @throws IOException when error occurs while reading the buffer.
*/
private MLArray readMatrix(ByteBuffer buf, boolean isRoot ) throws IOException
{
//result
MLArray mlArray;
ISMatTag tag;
//read flags
int[] flags = readFlags(buf);
int attributes = ( flags.length != 0 ) ? flags[0] : 0;
int nzmax = ( flags.length != 0 ) ? flags[1] : 0;
int type = attributes & 0xff;
//read Array dimension
int[] dims = readDimension(buf);
//read array Name
String name = readName(buf);
//if this array is filtered out return immediately
if ( isRoot && !filter.matches(name) )
{
return null;
}
//read data >> consider changing it to stategy pattern
switch ( type )
{
case MLArray.mxSTRUCT_CLASS:
MLStructure struct = new MLStructure(name, dims, type, attributes);
//field name length - this subelement always uses the compressed data element format
tag = new ISMatTag(buf);
int maxlen = buf.getInt(); //maximum field length
////// read fields data as Int8
tag = new ISMatTag(buf);
//calculate number of fields
int numOfFields = tag.size/maxlen;
//padding after field names
int padding = (tag.size%8) != 0 ? 8-(tag.size%8) : 0;
String[] fieldNames = new String[numOfFields];
for ( int i = 0; i < numOfFields; i++ )
{
byte[] names = new byte[maxlen];
buf.get(names);
fieldNames[i] = zeroEndByteArrayToString(names);
}
buf.position( buf.position() + padding );
//read fields
for ( int index = 0; index < struct.getM()*struct.getN(); index++ )
{
for ( int i = 0; i < numOfFields; i++ )
{
//read matrix recursively
tag = new ISMatTag(buf);
MLArray fieldValue = readMatrix( buf, false);
struct.setField(fieldNames[i], fieldValue, index);
}
}
mlArray = struct;
break;
case MLArray.mxCELL_CLASS:
MLCell cell = new MLCell(name, dims, type, attributes);
for ( int i = 0; i < cell.getM()*cell.getN(); i++ )
{
tag = new ISMatTag(buf);
//read matrix recursively
MLArray cellmatrix = readMatrix( buf, false);
cell.set(cellmatrix, i);
}
mlArray = cell;
break;
case MLArray.mxDOUBLE_CLASS: