View Javadoc

1   /*
2   
3       dsh-matrix-io  Matrix readers and writers.
4       Copyright (c) 2008-2013 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.matrix.io.impl;
25  
26  import java.io.BufferedReader;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.io.InputStreamReader;
30  
31  import org.dishevelled.matrix.Matrix3D;
32  
33  /**
34   * Abstract tab-delimited text reader for matrices of objects in three dimensions.
35   * The first line should be the size of the matrix as <code>slices\trows\tcolumns\tcardinality</code>
36   * and each additional line should be formatted as <code>slice\trow\tcolumn\tvalue</code>.
37   *
38   * @param <E> 3D matrix element type
39   * @author  Michael Heuer
40   * @version $Revision$ $Date$
41   */
42  public abstract class AbstractTextMatrix3DReader<E>
43      extends AbstractMatrix3DReader<E>
44  {
45  
46      /** {@inheritDoc} */
47      public final Matrix3D<E> read(final InputStream inputStream) throws IOException
48      {
49          if (inputStream == null)
50          {
51              throw new IllegalArgumentException ("inputStream must not be null");
52          }
53          int lineNumber = 0;
54          BufferedReader reader = null;
55          Matrix3D<E> matrix = null;
56          try
57          {
58              reader = new BufferedReader(new InputStreamReader(inputStream));
59              while (reader.ready())
60              {
61                  String[] tokens = reader.readLine().split("\t");
62                  if (matrix == null)
63                  {
64                      long slices = Long.parseLong(tokens[0]);
65                      long rows = Long.parseLong(tokens[1]);
66                      long columns = Long.parseLong(tokens[2]);
67                      int cardinality = Integer.parseInt(tokens[3]);
68                      matrix = createMatrix3D(slices, rows, columns, cardinality);
69                  }
70                  else
71                  {
72                      long slice = Long.parseLong(tokens[0]);
73                      long row = Long.parseLong(tokens[1]);
74                      long column = Long.parseLong(tokens[2]);
75                      E e = parse(tokens[3]);
76                      matrix.set(slice, row, column, e);
77                  }
78                  lineNumber++;
79              }
80          }
81          catch (NumberFormatException e)
82          {
83              throw new IOException("caught NumberFormatException at line number " + lineNumber
84                                    + "\n" + e.getMessage());
85              // jdk 1.6+
86              //throw new IOException("caught NumberFormatException at line number " + lineNumber, e);
87          }
88          catch (IndexOutOfBoundsException e)
89          {
90              throw new IOException("caught IndexOutOfBoundsException at line number " + lineNumber
91                                    + "\n" + e.getMessage());
92              // jdk 1.6+
93              //throw new IOException("caught IndexOutOfBoundsException at line number " + lineNumber, e);
94          }
95          finally
96          {
97              MatrixIOUtils.closeQuietly(reader);
98          }
99          if (matrix == null)
100         {
101             throw new IOException("could not create create matrix, first line should contain"
102                     + " rows/\tcolumns/\tcardinality");
103         }
104         return matrix;
105     }
106 }