// Path: halvade_upload_tool/src/be/ugent/intec/halvade/uploader/input/FileReaderFactory.java
// public class FileReaderFactory extends BaseFileReader implements Runnable {
//     protected ArrayList<BaseFileReader> readers;
//     protected BaseFileReader currentReader = null;
//     protected static FileReaderFactory allReaders = null;
//     protected int threads;
//     protected boolean fromHDFS = false;
// 
//     public FileReaderFactory(int threads, boolean fromHDFS) {
//         super(false);
//         readers = new ArrayList<>();
//         this.threads = threads;
//         this.fromHDFS = fromHDFS;
//     }
//     
//     public static FileReaderFactory getInstance(int threads, boolean fromHDFS) {
//         if(allReaders == null) {
//             allReaders = new FileReaderFactory(threads, fromHDFS);
//         }
//         return allReaders;
//     }
//     
//     public static BaseFileReader createFileReader(boolean fromHDFS, String fileA, String fileB, boolean interleaved) throws IOException {
//         if (fileB == null) {
//             return new SingleFastQReader(fromHDFS, fileA, interleaved);
//         } else {
//             return new PairedFastQReader(fromHDFS, fileA, fileB);
//         }     
//     }
//     
//     public void addReader(String fileA, String fileB, boolean interleaved) throws IOException {
//         readers.add(createFileReader(fromHDFS, fileA, fileB, interleaved));
//     }
//     
//     public void addReader(BaseFileReader reader) {
//         readers.add(reader);
//     }
// 
//     
//     public ReadBlock retrieveBlock() {
//         try {
//             ReadBlock block = null;
//             while((check || blocks.size() > 0) && block == null) {
//                 block = blocks.poll(1000, TimeUnit.MILLISECONDS);
//             }
//             return block;
//         } catch (InterruptedException ex) {
//             Logger.EXCEPTION(ex);
//             return null;
//         }
//     }
//     
//     
//     @Override
//     protected int addNextRead(ReadBlock block) throws IOException {
//         return currentReader.addNextRead(block);
//     }
//     
//     protected synchronized boolean getNextReader() {
//         if(currentReader == null) {
//             if(readers.size() > 0) {
//                 currentReader = readers.remove(readers.size() - 1);
//                 Logger.DEBUG("Reader: " + currentReader);
//                 return true;
//             } else {
//                 Logger.DEBUG("Processed all readers");
//                 return false;
//             }
//         } else return true;
//     }
// 
// 
//     protected boolean check = true;
//     protected ArrayBlockingQueue<ReadBlock> blocks;
//     protected int READ_BLOCK_CAPACITY_PER_THREAD = 10;
//     
//     @Override
//     public void run() {
//         blocks = new ArrayBlockingQueue<>(READ_BLOCK_CAPACITY_PER_THREAD*threads);
//         if(currentReader == null) {
//             if(!getNextReader()) check = false;
//         }
//         try {
//             while(check) {
//                 ReadBlock block = new ReadBlock();
//                 boolean hasReads = super.getNextBlock(block);
//                 if (!hasReads) {
//                     currentReader = null;
//                     if(!getNextReader())
//                         check = false;
//                 } else {
//                     blocks.put(block);
//                 }
//             }
//         } catch (InterruptedException ex) {
//             Logger.EXCEPTION(ex);
//         }
//     }
// }


import be.ugent.intec.halvade.uploader.input.ReadBlock;
import be.ugent.intec.halvade.uploader.input.FileReaderFactory;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/*
 * Copyright (C) 2014 ddecap
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package be.ugent.intec.halvade.uploader;


/**
 *
 * @author ddecap
 */
abstract class BaseInterleaveFiles extends Thread {
    protected static final int BUFFERSIZE = 8*1024;
    protected static long maxFileSize; // ~60MB
    protected FileReaderFactory factory;
    protected String fileBase;
    protected long read, written, count;
    protected String fsName;
    protected int thread;
    protected boolean useHadoopCompression = false;
    
    public BaseInterleaveFiles(String base, long maxFileSize, int thread, boolean fromHDFS) {
        this.fileBase = base;
        this.thread = thread;
        written = 0;
        read = 0;
        count = 0;
// Path: halvade_upload_tool/src/be/ugent/intec/halvade/uploader/input/FileReaderFactory.java
// public class FileReaderFactory extends BaseFileReader implements Runnable {
//     protected ArrayList<BaseFileReader> readers;
//     protected BaseFileReader currentReader = null;
//     protected static FileReaderFactory allReaders = null;
//     protected int threads;
//     protected boolean fromHDFS = false;
// 
//     public FileReaderFactory(int threads, boolean fromHDFS) {
//         super(false);
//         readers = new ArrayList<>();
//         this.threads = threads;
//         this.fromHDFS = fromHDFS;
//     }
//     
//     public static FileReaderFactory getInstance(int threads, boolean fromHDFS) {
//         if(allReaders == null) {
//             allReaders = new FileReaderFactory(threads, fromHDFS);
//         }
//         return allReaders;
//     }
//     
//     public static BaseFileReader createFileReader(boolean fromHDFS, String fileA, String fileB, boolean interleaved) throws IOException {
//         if (fileB == null) {
//             return new SingleFastQReader(fromHDFS, fileA, interleaved);
//         } else {
//             return new PairedFastQReader(fromHDFS, fileA, fileB);
//         }     
//     }
//     
//     public void addReader(String fileA, String fileB, boolean interleaved) throws IOException {
//         readers.add(createFileReader(fromHDFS, fileA, fileB, interleaved));
//     }
//     
//     public void addReader(BaseFileReader reader) {
//         readers.add(reader);
//     }
// 
//     
//     public ReadBlock retrieveBlock() {
//         try {
//             ReadBlock block = null;
//             while((check || blocks.size() > 0) && block == null) {
//                 block = blocks.poll(1000, TimeUnit.MILLISECONDS);
//             }
//             return block;
//         } catch (InterruptedException ex) {
//             Logger.EXCEPTION(ex);
//             return null;
//         }
//     }
//     
//     
//     @Override
//     protected int addNextRead(ReadBlock block) throws IOException {
//         return currentReader.addNextRead(block);
//     }
//     
//     protected synchronized boolean getNextReader() {
//         if(currentReader == null) {
//             if(readers.size() > 0) {
//                 currentReader = readers.remove(readers.size() - 1);
//                 Logger.DEBUG("Reader: " + currentReader);
//                 return true;
//             } else {
//                 Logger.DEBUG("Processed all readers");
//                 return false;
//             }
//         } else return true;
//     }
// 
// 
//     protected boolean check = true;
//     protected ArrayBlockingQueue<ReadBlock> blocks;
//     protected int READ_BLOCK_CAPACITY_PER_THREAD = 10;
//     
//     @Override
//     public void run() {
//         blocks = new ArrayBlockingQueue<>(READ_BLOCK_CAPACITY_PER_THREAD*threads);
//         if(currentReader == null) {
//             if(!getNextReader()) check = false;
//         }
//         try {
//             while(check) {
//                 ReadBlock block = new ReadBlock();
//                 boolean hasReads = super.getNextBlock(block);
//                 if (!hasReads) {
//                     currentReader = null;
//                     if(!getNextReader())
//                         check = false;
//                 } else {
//                     blocks.put(block);
//                 }
//             }
//         } catch (InterruptedException ex) {
//             Logger.EXCEPTION(ex);
//         }
//     }
// }


// Path: halvade_upload_tool/src/be/ugent/intec/halvade/uploader/BaseInterleaveFiles.java
import be.ugent.intec.halvade.uploader.input.ReadBlock;
import be.ugent.intec.halvade.uploader.input.FileReaderFactory;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/*
 * Copyright (C) 2014 ddecap
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package be.ugent.intec.halvade.uploader;


/**
 *
 * @author ddecap
 */
abstract class BaseInterleaveFiles extends Thread {
    protected static final int BUFFERSIZE = 8*1024;
    protected static long maxFileSize; // ~60MB
    protected FileReaderFactory factory;
    protected String fileBase;
    protected long read, written, count;
    protected String fsName;
    protected int thread;
    protected boolean useHadoopCompression = false;
    
    public BaseInterleaveFiles(String base, long maxFileSize, int thread, boolean fromHDFS) {
        this.fileBase = base;
        this.thread = thread;
        written = 0;
        read = 0;
        count = 0;
        factory = FileReaderFactory.getInstance(thread, fromHDFS);