// initially, we don't know what size buffer to hold
int nrows = -1;
int ncols = -1;
Matrix r = null;
MatrixWritable m = new MatrixWritable();
int row = 0;
for (File file : files) {
DataInputStream in = new DataInputStream(new FileInputStream(file));
m.readFields(in);
in.close();
if (nrows == -1) {
// now we can set an upper bound on how large our result will be
nrows = m.get().rowSize() * files.size();
ncols = m.get().columnSize();
r = new DenseMatrix(nrows, ncols);
}
r.viewPart(row, m.get().rowSize(), 0, r.columnSize()).assign(m.get());
row += m.get().rowSize();
}
// at the end, row will have the true size of the result
if (row != nrows && r != null) {
// and if that isn't the size of the buffer, we need to crop the result a bit
r = r.viewPart(0, row, 0, ncols);