/*
 * Copyright 2015-2016 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.opencb.commons.io.avro;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.specific.SpecificDatumReader;
import org.opencb.commons.io.DataReader;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created on 09/11/15.
 *
 * @author Jacobo Coll &lt;jacobo167@gmail.com&gt;
 */
public class AvroDataReader<T extends GenericRecord> implements DataReader<T> {

    private final Class<T> clazz;
    private final File file;
    private DataFileReader<T> dataFileReader;

    public AvroDataReader(File file, Class<T> clazz) {
        this.clazz = clazz;
        this.file = file;
    }


    @Override
    public boolean open() {
        DatumReader<T> datumReader = new SpecificDatumReader<>(clazz);
        try {
            dataFileReader = new DataFileReader<>(file, datumReader);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        return true;
    }

    @Override
    public boolean close() {
        try {
            dataFileReader.close();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        return true;
    }

    @Override
    public List<T> read(int batchSize) {
        List<T> batch = new ArrayList<>(batchSize);
        for (int i = 0; i < batchSize && dataFileReader.hasNext(); i++) {
            batch.add(dataFileReader.next());
        }
        return batch;
    }
}

@@ -662,32 +662,76 @@
 DataFileReader;%0A
+import org.apache.avro.file.DataFileStream;%0A
 import org.apach
@@ -935,32 +935,60 @@
 io.IOException;%0A
+import java.io.InputStream;%0A
 import java.io.U
@@ -1058,16 +1058,42 @@
 il.List;
+%0Aimport java.util.Objects;
 %0A%0A/**%0A *
@@ -1312,24 +1312,58 @@
  File file;%0A
+    private final InputStream is;%0A
     private 
@@ -1370,22 +1370,22 @@
 DataFile
-Reader
+Stream
 %3CT%3E data
@@ -1504,20 +1504,234 @@
 e = 
-file;%0A    %7D%0A
+Objects.requireNonNull(file);%0A        this.is = null;%0A    %7D%0A%0A    public AvroDataReader(InputStream is, Class%3CT%3E clazz) %7B%0A        this.is = Objects.requireNonNull(is);%0A        this.clazz = clazz;%0A        this.file = null;%0A    %7D
 %0A%0A  
@@ -1847,32 +1847,159 @@
 ;%0A        try %7B%0A
+            if (is != null) %7B%0A                dataFileReader = new DataFileStream%3C%3E(is, datumReader);%0A            %7D else %7B%0A    
             data
@@ -2044,32 +2044,46 @@
 , datumReader);%0A
+            %7D%0A
         %7D catch 

Improve AvroDataReader to support read from InputStream and File