Commit 73b60aac authored by Sina Samangooei's avatar Sina Samangooei
Browse files

re added changes to MatFileWriter

parent 9acb8c58
Loading
Loading
Loading
Loading
+111 −104
Original line number Diff line number Diff line
@@ -58,6 +58,20 @@ import com.jmatio.types.MLStructure;
 */
public class MatFileWriter
{

	/**
	 * A hack stolen from Greg Wilkins of Mortbay.
	 * A {@link ByteArrayOutputStream} with revealed internals so there is no more wasteful
	 * copying when calling {@link ByteArrayOutputStream#toByteArray()}
	 * @author ss
	 *
	 */
	static class ByteArrayOutputStream2 extends ByteArrayOutputStream
	{
	    public ByteArrayOutputStream2(){super();}
	    public byte[] getBuf(){return buf;}
	    public int getCount(){return count;}
	}
//    private static final Logger logger = Logger.getLogger(MatFileWriter.class);

    /**
@@ -172,31 +186,24 @@ public class MatFileWriter
            //write data
            for ( MLArray matrix : data )
            {
                //prepare buffer for MATRIX data
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream( baos );
                //write MATRIX bytes into buffer
                writeMatrix( dos, matrix );
                
                //compress data to save storage
                Deflater compresser = new Deflater();

                byte[] input = baos.toByteArray();
                
                ByteArrayOutputStream compressed = new ByteArrayOutputStream();
                ByteArrayOutputStream2 compressed = new ByteArrayOutputStream2();
                DataOutputStream dout = new DataOutputStream(new DeflaterOutputStream(compressed, compresser));

                dout.write(input);
                
                writeMatrix( dout, matrix );
                dout.flush();
                dout.close();
                compressed.close();

                //write COMPRESSED tag and compressed data into output channel
                byte[] compressedBytes = compressed.toByteArray();
                ByteBuffer buf = ByteBuffer.allocateDirect(2 * 4 /* Int size */ + compressedBytes.length);

                int compressedSize = compressed.getCount();
                ByteBuffer buf = ByteBuffer.allocateDirect(2 * 4 /* Int size */ + compressedSize);
                buf.putInt( MatDataTypes.miCOMPRESSED );
                buf.putInt( compressedBytes.length );
                buf.put( compressedBytes );

                buf.putInt( compressedSize);
                buf.put(compressed.getBuf(), 0, compressedSize);

                buf.flip();
                channel.write( buf );