package org.canova.api.records.reader.impl;

import org.apache.commons.io.FileUtils;
import org.canova.api.io.data.Text;
import org.canova.api.records.reader.RecordReader;
import org.canova.api.split.InputSplit;
import org.canova.api.writable.Writable;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.*;

/**
 * File reader/writer
 *
 * @author Adam Gibson
 */
public class FileRecordReader implements RecordReader {

    private URI[] locations;
    private int currIndex = 0;
    private Iterator<File> iter;


    @Override
    public void initialize(InputSplit split) throws IOException, InterruptedException {
        this.locations = split.locations();
        if(locations != null && locations.length > 0) {
            iter = FileUtils.iterateFiles(new File(locations[0]),null,true);
        }

    }

    @Override
    public Collection<Writable> next() {
        List<Writable> ret = new ArrayList<>();
        if(iter.hasNext()) {
            try {
                ret.add(new Text(FileUtils.readFileToString(iter.next())));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ret;
        }


        throw new NoSuchElementException("No more elements found!");
    }

    @Override
    public boolean hasNext() {
        return iter != null && iter.hasNext();
    }

    @Override
    public void close() throws IOException {

    }
}

@@ -78,16 +78,54 @@
 eUtils;%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -334,16 +334,50 @@
 eption;%0A
+import java.io.InputStreamReader;%0A
 import j
@@ -1030,47 +1030,569 @@
     
-if(iter.hasNext()) %7B%0A            try %7B%0A
+try %7B%0A            ret.add(new Text(FileUtils.readFileToString(iter.next())));%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        if(iter.hasNext()) %7B%0A            return ret;%0A        %7D%0A        else %7B%0A            currIndex++;%0A            try %7B%0A                close();%0A                iter = IOUtils.lineIterator(new InputStreamReader(locations%5BcurrIndex%5D.toURL().openStream()));%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A%0A            if(iter.hasNext()) %7B%0A                try %7B%0A    
     
@@ -1667,32 +1667,36 @@
 ));%0A            
+    
 %7D catch (IOExcep
@@ -1713,32 +1713,36 @@
                 
+ 
+   
 e.printStackTrac
@@ -1750,32 +1750,36 @@
 ();%0A            
+    
 %7D%0A            re
@@ -1768,32 +1768,36 @@
   %7D%0A            
+ 
+   
 return ret;%0A    
@@ -1800,18 +1800,32 @@
         
+    %7D%0A%0A        
 %7D
-%0A
 %0A%0A      

add file record reader/writer