package net.intelie.disq;

import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class DiskQueueReader implements Closeable {
    private final StateFile state;
    private final Path directory;
    private DataFileReader reader;

    public DiskQueueReader(Path directory) throws IOException {
        this.directory = directory;
        this.state = new StateFile(this.directory.resolve("state"), true);
        this.reader = openReader();
    }

    private DataFileReader openReader() throws IOException {
        Path file = makeDataPath(state.getReadFile());
        if (!Files.exists(file)) return null;
        return new DataFileReader(file, state.getReadPosition());
    }

    public long bytes() {
        return state.getBytes();
    }

    public long count() {
        return state.getCount();
    }

    public boolean moveNext(Buffer buffer) throws IOException {
        if (checkReadEOF())
            return false;
        int read = reader.read(buffer);
        state.addReadCount(read);
        return true;
    }

    private Path makeDataPath(int state) {
        return directory.resolve(String.format("data%02x", state));
    }

    private boolean checkReadEOF() throws IOException {
        while (state.readFileEof())
            if (!maybeAdvanceFile())
                break;
        return state.getCount() == 0;
    }

    private boolean maybeAdvanceFile() throws IOException {
        state.advanceReadFile(reader.size());
        reader.close();
        reader = openReader();
        return reader != null;
    }

    @Override
    public void close() throws IOException {
        try {
            if (reader != null)
                reader.close();
        } finally {
            state.close();
        }
    }
}

@@ -1669,26 +1669,8 @@
 n %7B%0A
-        try %7B%0A    
     
@@ -1701,28 +1701,24 @@
             
-  
-  
 reader.close
@@ -1725,65 +1725,8 @@
 ();%0A
-        %7D finally %7B%0A            state.close();%0A        %7D%0A
     

remove unneeded close