1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
35
36
37
38
39
40
41
42 public abstract class AbstractTextMatrix3DReader<E>
43 extends AbstractMatrix3DReader<E>
44 {
45
46
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
86
87 }
88 catch (IndexOutOfBoundsException e)
89 {
90 throw new IOException("caught IndexOutOfBoundsException at line number " + lineNumber
91 + "\n" + e.getMessage());
92
93
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 }