001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.io;
025    
026    import java.io.BufferedInputStream;
027    import java.io.File;
028    import java.io.FileInputStream;
029    import java.io.FileNotFoundException;
030    import java.io.IOException;
031    import java.io.InputStream;
032    import java.io.ObjectInputStream;
033    
034    import org.ujmp.core.Matrix;
035    import org.ujmp.core.MatrixFactory;
036    import org.ujmp.core.enums.ValueType;
037    
038    public class ImportMatrixSER {
039    
040            public static Matrix fromFile(File file, Object... parameters) throws FileNotFoundException,
041                            IOException, ClassNotFoundException {
042                    FileInputStream stream = new FileInputStream(file);
043                    Matrix m = fromStream(stream);
044                    stream.close();
045                    return m;
046            }
047    
048            public static Matrix fromStream(InputStream stream, Object... parameters)
049                            throws FileNotFoundException, IOException, ClassNotFoundException {
050                    ObjectInputStream s = new ObjectInputStream(new BufferedInputStream(stream));
051    
052                    ValueType valueType = (ValueType) s.readObject();
053                    boolean isSparse = s.readBoolean();
054                    int sizeLength = s.readInt();
055                    long[] size = new long[sizeLength];
056                    for (int i = 0; i < sizeLength; i++) {
057                            size[i] = s.readLong();
058                    }
059    
060                    Matrix m = null;
061                    if (isSparse) {
062                            m = MatrixFactory.sparse(valueType, size);
063                    } else {
064                            m = MatrixFactory.dense(valueType, size);
065                    }
066    
067                    long[] c = new long[sizeLength];
068                    while (s.readBoolean()) {
069                            for (int i = 0; i < sizeLength; i++) {
070                                    c[i] = s.readLong();
071                            }
072                            switch (valueType) {
073                            case DOUBLE:
074                                    m.setAsDouble(s.readDouble(), c);
075                                    break;
076                            case INT:
077                                    m.setAsInt(s.readInt(), c);
078                                    break;
079                            default:
080                                    m.setAsObject(s.readObject(), c);
081                                    break;
082                            }
083                    }
084    
085                    s.close();
086                    return m;
087            }
088    }