Commit 9acb8c58 authored by Sina Samangooei's avatar Sina Samangooei
Browse files

merged with jon

parents f50cd300 fb48d2d9
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -11,4 +11,5 @@ For example: in MatFileWriter.java I have cut down the use of needless ByteArray

These changes are completely irrelevant with small matlab files (sub 100Mb) but when the data being written is in the gigabytes these changes resulted in substantial memory usage improvements

In addition, support for reading from streams has been added. 
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
	<modelVersion>4.0.0</modelVersion>
	<groupId>jmatio</groupId>
	<artifactId>jmatio</artifactId>
	<version>0.2.3</version>
	<version>0.2.3.1</version>
	<name>The OpenIMAJ branch of JMATIO</name>
	<url>http://www.openimaj.org/thirdparty/jmatio</url>
	<inceptionYear>2011</inceptionYear>
+108 −3
Original line number Diff line number Diff line
@@ -18,12 +18,12 @@ import java.nio.channels.FileChannel;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.zip.InflaterInputStream;

import com.jmatio.common.MatDataTypes;
import com.jmatio.io.MatFileWriter.ByteArrayOutputStream2;
import com.jmatio.types.ByteStorageSupport;
import com.jmatio.types.MLArray;
import com.jmatio.types.MLCell;
@@ -63,7 +63,8 @@ import com.jmatio.types.MLUInt8;
 * @author Wojciech Gradkowski (<a href="mailto:wgradkowski@gmail.com">wgradkowski@gmail.com</a>)
 */
/**
 * @author Wojciech Gradkowski (<a href="mailto:wgradkowski@gmail.com">wgradkowski@gmail.com</a>)
 * @author Wojciech Gradkowski (<a
 *         href="mailto:wgradkowski@gmail.com">wgradkowski@gmail.com</a>)
 * 
 */
public class MatFileReader
@@ -160,6 +161,48 @@ public class MatFileReader
        data    = new LinkedHashMap<String, MLArray>();
    }

    /**
     * Creates instance of <code>MatFileReader</code> and reads MAT-file from
     * <code>file</code>.
     * 
     * This method reads MAT-file without filtering.
     * 
     * @param stream
     *            the MAT-file stream
     * @throws IOException
     *             when error occurred while processing the file.
     */
    public MatFileReader(InputStream stream) throws IOException
    {
        this(stream, new MatFileFilter());
    }

    /**
     * Creates instance of <code>MatFileReader</code> and reads MAT-file from
     * <code>file</code>.
     * <p>
     * Results are filtered by <code>MatFileFilter</code>. Arrays that do not
     * meet filter match condition will not be available in results.
     * <p>
     * <i>Note: this method reads file using the memory mapped file policy, see
     * notes to </code>
     * {@link #read(File, MatFileFilter, com.jmatio.io.MatFileReader.MallocPolicy)}
     * </code>
     * 
     * @param stream
     *            the MAT-file stream
     * @param MatFileFilter
     *            array name filter.
     * @throws IOException
     *             when error occurred while processing the file.
     */
    public MatFileReader(InputStream stream, MatFileFilter filter) throws IOException
    {
        this();

        read(stream, filter);
    }
    
    /**
     * Reads the content of a MAT-file and returns the mapped content.
     * <p>
@@ -176,6 +219,23 @@ public class MatFileReader
    {
       return read(file, new MatFileFilter(), MEMORY_MAPPED_FILE);
    }

    /**
     * Reads the content of a MAT-file and returns the mapped content.
     * <p>
     * This method calls <code>read(stream, new MatFileFilter())</code>.
     * 
     * @param stream
     *            a valid MAT-file stream to be read
     * @return the same as <code>{@link #getContent()}</code>
     * @throws IOException
     *             if error occurs during file processing
     */
    public synchronized Map<String, MLArray> read(InputStream stream) throws IOException
    {
        return read(stream, new MatFileFilter());
    }

    /**
     * Reads the content of a MAT-file and returns the mapped content.
     * <p>
@@ -341,6 +401,50 @@ public class MatFileReader
        
    }
    
    /**
     * Read a mat file from a stream. Internally this will read the stream fully
     * into memory before parsing it.
     * 
     * @param stream
     *            a valid MAT-file stream to be read
     * @param filter
     *            the array filter applied during reading
     * 
     * @return the same as <code>{@link #getContent()}</code>
     * @see MatFileFilter
     * @throws IOException
     *             if error occurs during file processing
     */
    public synchronized Map<String, MLArray> read(InputStream stream, MatFileFilter filter) throws IOException
    {
        this.filter = filter;

        data.clear();

        ByteBuffer buf = null;

        final ByteArrayOutputStream2 baos = new ByteArrayOutputStream2();
        copy(stream, baos);
        buf = ByteBuffer.wrap(baos.getBuf(), 0, baos.getCount());

        // read in file header
        readHeader(buf);

        while (buf.remaining() > 0)
        {
            readData(buf);
        }

        return getContent();
    }

    private void copy(InputStream stream, ByteArrayOutputStream2 output) throws IOException {
        final byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while (-1 != (n = stream.read(buffer))) {
            output.write(buffer, 0, n);
        }
    }
    /**
     * Workaround taken from bug <a
     * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038">#4724038</a>
@@ -1175,4 +1279,5 @@ public class MatFileReader
            return ac;
        }
    }

}
+13 −6
Original line number Diff line number Diff line
package com.jmatio.io;


import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
@@ -25,7 +24,9 @@ import com.jmatio.types.MLStructure;
 * MAT-file writer.
 * 
 * Usage:
 * <pre><code>
 * 
 * <pre>
 * <code>
 * //1. First create example arrays
 * double[] src = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
 * MLDouble mlDouble = new MLDouble( "double_arr", src, 3 );
@@ -37,17 +38,23 @@ import com.jmatio.types.MLStructure;
 * list.add( mlChar );
 * 
 * new MatFileWriter( "mat_file.mat", list );
 * </code></pre>
 * </code>
 * </pre>
 * 
 * this is "equal" to Matlab commands:
 * <pre><code>
 * 
 * <pre>
 * <code>
 * >> double_arr = [ 1 2; 3 4; 5 6];
 * >> char_arr = 'I am dummy';
 * >>
 * >> save('mat_file.mat', 'double_arr', 'char_arr');
 * </pre></code>
 * </pre>
 * 
 * </code>
 * 
 * @author Wojciech Gradkowski (<a href="mailto:wgradkowski@gmail.com">wgradkowski@gmail.com</a>)
 * @author Wojciech Gradkowski (<a
 *         href="mailto:wgradkowski@gmail.com">wgradkowski@gmail.com</a>)
 */
public class MatFileWriter
{