public ByteBuffer readToByteBuffer(ByteBuffer dest, int elements,
                    ByteStorageSupport<?> storage) throws IOException
    {
        
        int bytesAllocated = storage.getBytesAllocated();
        int size = elements * storage.getBytesAllocated();
        
        //direct buffer copy
        if ( MatDataTypes.sizeOf(type) == bytesAllocated && buf.order().equals(dest.order()) )
        {
            int bufMaxSize = 1024;
            int bufSize = Math.min(buf.remaining(), bufMaxSize);
            int bufPos = buf.position();
            
            byte[] tmp = new byte[ bufSize ];
            
            while ( dest.remaining() > 0 )
            {
                int length = Math.min(dest.remaining(), tmp.length);
                buf.get( tmp, 0, length );
                dest.put( tmp, 0, length );
            }
            buf.position( bufPos + size );
        }
        else
        {
            //because Matlab writes data not respectively to the declared
            //matrix type, the reading is not straight forward (as above)
            Class<?> clazz = storage.getStorageClazz();
            while ( dest.remaining() > 0 )
            {
                if ( clazz.equals( Double.class) )
                {
                    dest.putDouble( readDouble() );
                }
                else if ( clazz.equals( Byte.class) )
                {
                    dest.put( readByte() );
                }
                else if ( clazz.equals( Integer.class) )
                {
                    dest.putInt( readInt() );
                }
                else if ( clazz.equals( Long.class) )
                {
                    dest.putLong( readLong() );
                }
                else  if ( clazz.equals( Float.class) )
                {
                    dest.putFloat( readFloat() );
                }
                else if ( clazz.equals( Short.class) )
                {
                    dest.putShort( readShort() );
                }
                else
                {
                    throw new RuntimeException("Not supported buffer reader for " + clazz );
                }
            }
        }
        dest.rewind();
        return dest;
    }
public ByteBuffer readToByteBuffer ( ByteBuffer dest , int elements , ByteStorageSupport < ? > storage ) throws IOException { int bytesAllocated = storage . getBytesAllocated ( ) ; int size = elements * storage . getBytesAllocated ( ) ; if ( MatDataTypes . sizeOf ( type ) == bytesAllocated && buf . order ( ) . equals ( dest . order ( ) ) ) { int bufMaxSize = 1024 ; int bufSize = Math . min ( buf . remaining ( ) , bufMaxSize ) ; int bufPos = buf . position ( ) ; byte [ ] tmp = new byte [ bufSize ] ; while ( dest . remaining ( ) > 0 ) { int length = Math . min ( dest . remaining ( ) , tmp . length ) ; buf . get ( tmp , 0 , length ) ; dest . put ( tmp , 0 , length ) ; } buf . position ( bufPos + size ) ; } else { Class < ? > clazz = storage . getStorageClazz ( ) ; while ( dest . remaining ( ) > 0 ) { if ( clazz . equals ( Double . class ) ) { dest . putDouble ( readDouble ( ) ) ; } else if ( clazz . equals ( Byte . class ) ) { dest . put ( readByte ( ) ) ; } else if ( clazz . equals ( Integer . class ) ) { dest . putInt ( readInt ( ) ) ; } else if ( clazz . equals ( Long . class ) ) { dest . putLong ( readLong ( ) ) ; } else if ( clazz . equals ( Float . class ) ) { dest . putFloat ( readFloat ( ) ) ; } else if ( clazz . equals ( Short . class ) ) { dest . putShort ( readShort ( ) ) ; } else { throw new RuntimeException ( "Not supported buffer reader for " + clazz ) ; } } } dest . rewind ( ) ; return dest ; }
Reads the data into a <code>{@link ByteBuffer}</code>. This method is
only supported for arrays with backing ByteBuffer (<code>{@link ByteStorageSupport}</code>).

@param dest
the destination <code>{@link ByteBuffer}</code>
@param elements
the number of elements to read into a buffer
@param storage
the backing <code>{@link ByteStorageSupport}</code> that
gives information how data should be interpreted
@return reference to the destination <code>{@link ByteBuffer}</code>
@throws IOException
if buffer is under-fed, or another IO problem occurs