package com.vlkan.hrrs.api.file;

import com.google.common.base.MoreObjects;
import com.vlkan.hrrs.api.HttpRequestRecordReaderSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import java.io.*;
import java.nio.charset.Charset;

import static com.google.common.base.Preconditions.checkNotNull;

@NotThreadSafe
public class HttpRequestRecordReaderFileSource implements HttpRequestRecordReaderSource {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestRecordReaderFileSource.class);

    private final File file;

    private final Charset charset;

    private final BufferedReader reader;

    public HttpRequestRecordReaderFileSource(File file, Charset charset) {
        this.file = checkNotNull(file, "file");
        this.charset = checkNotNull(charset, "charset");
        this.reader = createReader(file, charset);
        LOGGER.trace("instantiated (file={}, charset={})", file, charset);
    }

    private static BufferedReader createReader(File file, Charset charset) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charset);
            return new BufferedReader(inputStreamReader);
        } catch (IOException error) {
            String message = String.format("failed opening file (file=%s, charset=%s)", file, charset);
            throw new RuntimeException(message, error);
        }
    }

    public File getFile() {
        return file;
    }

    public Charset getCharset() {
        return charset;
    }

    @Nullable
    @Override
    public String read() {
        try {
            return reader.readLine();
        } catch (IOException error) {
            String message = String.format("failed reading line (file=%s)", file);
            throw new RuntimeException(message, error);
        }
    }

    @Override
    public void close() throws IOException {
        LOGGER.trace("closing");
        reader.close();
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("file", file)
                .add("charset", charset)
                .toString();
    }

}

@@ -319,16 +319,54 @@
 Charset;
+%0Aimport java.util.zip.GZIPInputStream;
 %0A%0Aimport
@@ -1220,24 +1220,216 @@
 ream(file);%0A
+            try %7B%0A                InputStream readerInputStream = isGzipped(file)%0A                        ? new GZIPInputStream(fileInputStream)%0A                        : fileInputStream;%0A    
             
@@ -1488,20 +1488,22 @@
 mReader(
-file
+reader
 InputStr
@@ -1509,32 +1509,36 @@
 ream, charset);%0A
+    
             retu
@@ -1575,24 +1575,150 @@
 eamReader);%0A
+            %7D catch (IOException error) %7B%0A                fileInputStream.close();%0A                throw error;%0A            %7D%0A
         %7D ca
@@ -1895,32 +1895,32 @@
 essage, error);%0A
-
         %7D%0A    %7D%0A
@@ -1912,32 +1912,154 @@
        %7D%0A    %7D%0A%0A
+    private static boolean isGzipped(File file) %7B%0A        return file.getAbsolutePath().matches(%22.*%5C%5C.%5BgG%5D%5BzZ%5D$%22);%0A    %7D%0A%0A
     public File 

Add GZIP support to record reader file source.