@@ -191,131 +191,138 @@ else if (line.charAt(idx) == separator) {
191191 public GenericTable open (final String source ) throws IOException {
192192 // FIXME Assumes FileLocation
193193 final Location sourceLocation = new FileLocation (source );
194- final DataHandle <? extends Location > handle = //
195- dataHandleService .create (sourceLocation );
196- if (!handle .exists ()) {
197- throw new IOException ("Cannot open source" );
198- }
199- long length = handle .length ();
200-
201- final byte [] buffer = new byte [(int ) length ];
202- handle .read (buffer );
203- final String text = new String (buffer );
204-
205194 final GenericTable table = new DefaultGenericTable ();
206195
207- // split by any line delimiter
208- final String [] lines = text .split ("\\ R" );
209- if (lines .length == 0 ) return table ;
210- // process first line to get number of cols
196+ try (final DataHandle <? extends Location > handle = //
197+ dataHandleService .create (sourceLocation ))
211198 {
212- final ArrayList <String > tokens = processRow (lines [0 ]);
213- if (readColHeaders ) {
214- final List <String > colHeaders ;
215- if (readRowHeaders ) colHeaders = tokens .subList (1 , tokens .size ());
216- else colHeaders = tokens ;
217- final String [] colHeadersArr = new String [colHeaders .size ()];
218- table .appendColumns (colHeaders .toArray (colHeadersArr ));
199+ if (!handle .exists ()) {
200+ throw new IOException ("Cannot open source" );
219201 }
220- else {
202+ long length = handle .length ();
203+
204+ final byte [] buffer = new byte [(int ) length ];
205+ handle .read (buffer );
206+
207+ final String text = new String (buffer );
208+
209+
210+ // split by any line delimiter
211+ final String [] lines = text .split ("\\ R" );
212+ if (lines .length == 0 ) return table ;
213+ // process first line to get number of cols
214+ {
215+ final ArrayList <String > tokens = processRow (lines [0 ]);
216+ if (readColHeaders ) {
217+ final List <String > colHeaders ;
218+ if (readRowHeaders ) colHeaders = tokens .subList (1 , tokens .size ());
219+ else colHeaders = tokens ;
220+ final String [] colHeadersArr = new String [colHeaders .size ()];
221+ table .appendColumns (colHeaders .toArray (colHeadersArr ));
222+ }
223+ else {
224+ final List <String > cols ;
225+ if (readRowHeaders ) {
226+ cols = tokens .subList (1 , tokens .size ());
227+ table .appendColumns (cols .size ());
228+ table .appendRow (tokens .get (0 ));
229+ }
230+ else {
231+ cols = tokens ;
232+ table .appendColumns (cols .size ());
233+ table .appendRow ();
234+ }
235+ for (int i = 0 ; i < cols .size (); i ++) {
236+ table .set (i , 0 , parser .apply (cols .get (i )));
237+ }
238+ }
239+ }
240+ for (int lineNum = 1 ; lineNum < lines .length ; lineNum ++) {
241+ final String line = lines [lineNum ];
242+ final ArrayList <String > tokens = processRow (line );
221243 final List <String > cols ;
222244 if (readRowHeaders ) {
223245 cols = tokens .subList (1 , tokens .size ());
224- table .appendColumns (cols .size ());
225246 table .appendRow (tokens .get (0 ));
226247 }
227248 else {
228249 cols = tokens ;
229- table .appendColumns (cols .size ());
230250 table .appendRow ();
231251 }
252+ if (cols .size () != table .getColumnCount ()) {
253+ throw new IOException ("Line " + table .getRowCount () +
254+ " is not the same length as the first line." );
255+ }
232256 for (int i = 0 ; i < cols .size (); i ++) {
233- table .set (i , 0 , parser .apply (cols .get (i )));
257+ table .set (i , lineNum - 1 , parser .apply (cols .get (i )));
234258 }
235259 }
236260 }
237- for (int lineNum = 1 ; lineNum < lines .length ; lineNum ++) {
238- final String line = lines [lineNum ];
239- final ArrayList <String > tokens = processRow (line );
240- final List <String > cols ;
241- if (readRowHeaders ) {
242- cols = tokens .subList (1 , tokens .size ());
243- table .appendRow (tokens .get (0 ));
244- }
245- else {
246- cols = tokens ;
247- table .appendRow ();
248- }
249- if (cols .size () != table .getColumnCount ()) {
250- throw new IOException ("Line " + table .getRowCount () +
251- " is not the same length as the first line." );
252- }
253- for (int i = 0 ; i < cols .size (); i ++) {
254- table .set (i , lineNum - 1 , parser .apply (cols .get (i )));
255- }
256- }
257261 return table ;
258262 }
259263
260264 @ Override
261- public void save (final GenericTable table , final String source )
265+ public void save (final GenericTable table , final String destination )
262266 throws IOException
263267 {
264268 // FIXME Assumes FileLocation
265- final Location sourceLocation = new FileLocation (source );
266- final DataHandle <Location > handle = //
267- dataHandleService .create (sourceLocation );
269+ final Location dstLocation = new FileLocation (destination );
268270
269- final boolean writeRH = this .writeRowHeaders && table .getRowCount () > 0 &&
270- IntStream .range (0 , table .getRowCount ()).allMatch (row -> table
271- .getRowHeader (row ) != null );
272- final boolean writeCH = this .writeColHeaders && table
273- .getColumnCount () > 0 && table .stream ().allMatch (col -> col
274- .getHeader () != null );
271+ try (final DataHandle <Location > handle = //
272+ dataHandleService .create (dstLocation ))
273+ {
274+ final boolean writeRH = this .writeRowHeaders && table .getRowCount () > 0 &&
275+ IntStream .range (0 , table .getRowCount ()).allMatch (row -> table
276+ .getRowHeader (row ) != null );
277+ final boolean writeCH = this .writeColHeaders && table
278+ .getColumnCount () > 0 && table .stream ().allMatch (col -> col
279+ .getHeader () != null );
275280
276- final StringBuilder sb = new StringBuilder ();
277- // write column headers
278- if (writeCH ) {
279- if (writeRH ) {
280- sb .append (tryQuote (cornerText ));
281- if (table .getColumnCount () > 0 ) {
282- sb .append (separator );
283- sb .append (tryQuote (table .getColumnHeader (0 )));
281+ final StringBuilder sb = new StringBuilder ();
282+ // write column headers
283+ if (writeCH ) {
284+ if (writeRH ) {
285+ sb .append (tryQuote (cornerText ));
286+ if (table .getColumnCount () > 0 ) {
287+ sb .append (separator );
288+ sb .append (tryQuote (table .getColumnHeader (0 )));
289+ }
290+ }
291+ // avoid adding extra separator when there is 0 column
292+ else if (table .getColumnCount () > 0 ) {
293+ sb .append (tryQuote (table .getColumnHeader (0 )));
294+ }
295+ for (int col = 1 ; col < table .getColumnCount (); col ++) {
296+ sb .append (separator );
297+ sb .append (tryQuote (table .getColumnHeader (col )));
298+ }
299+ sb .append (eol );
300+ handle .writeBytes (sb .toString ());
301+ sb .setLength (0 );
284302 }
285- }
286- // avoid adding extra separator when there is 0 column
287- else if (table . getColumnCount () > 0 ) {
288- sb .append (tryQuote (table .getColumnHeader ( 0 )));
289- }
290- for ( int col = 1 ; col < table . getColumnCount (); col ++) {
291- sb .append (separator );
292- sb . append ( tryQuote ( table . getColumnHeader ( col )));
293- }
294- sb . append ( eol );
295- handle . writeBytes ( sb . toString ());
296- sb .setLength ( 0 );
297- }
298- // write each row
299- for ( int row = 0 ; row < table . getRowCount (); row ++) {
300- if ( writeRH ) {
301- sb . append ( tryQuote ( table . getRowHeader ( row )));
302- if ( table . getColumnCount () > 0 ) {
303- sb .append ( separator );
304- sb .append ( tryQuote ( formatter . apply ( table . get ( 0 , row ))) );
303+ // write each row
304+ for ( int row = 0 ; row < table . getRowCount (); row ++) {
305+ if (writeRH ) {
306+ sb .append (tryQuote (table .getRowHeader ( row )));
307+ if ( table . getColumnCount () > 0 ) {
308+ sb . append ( separator );
309+ sb .append (tryQuote ( formatter . apply ( table . get ( 0 , row ))) );
310+ }
311+ }
312+ // avoid adding extra separator when there is 0 column
313+ else if ( table . getColumnCount () > 0 ) {
314+ sb .append ( tryQuote ( formatter . apply ( table . get ( 0 , row ))) );
315+ }
316+ for ( int col = 1 ; col < table . getColumnCount (); col ++) {
317+ sb . append ( separator );
318+ sb . append ( tryQuote ( formatter . apply ( table . get ( col , row ))));
319+ }
320+ sb . append ( eol );
321+ handle . writeBytes ( sb .toString () );
322+ sb .setLength ( 0 );
305323 }
306- }
307- // avoid adding extra separator when there is 0 column
308- else if (table .getColumnCount () > 0 ) {
309- sb .append (tryQuote (formatter .apply (table .get (0 , row ))));
310- }
311- for (int col = 1 ; col < table .getColumnCount (); col ++) {
312- sb .append (separator );
313- sb .append (tryQuote (formatter .apply (table .get (col , row ))));
314- }
315- sb .append (eol );
316- handle .writeBytes (sb .toString ());
317- sb .setLength (0 );
318324 }
325+
319326 }
320327
321328 /**
0 commit comments