package ca.intelliagent.replaydecoder;

import ca.intelliagent.replaydecoder.decompression.ReplayDecompressor;
import ca.intelliagent.replaydecoder.decryption.ReplayDecrypter;
import ca.intelliagent.replaydecoder.exception.CannotDecodeReplayException;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.file.*;

public abstract class ReplayDecoder {

    protected final ReplayFileReader replayFileReader;
    protected final Path defaultDirectory;

    protected ReplayDecoder(ReplayFileReader replayFileReader, Path outputDirectory) {
        this.replayFileReader = replayFileReader;
        defaultDirectory = outputDirectory;
    }


    protected ByteBuffer decode() {
        byte[] compressedCrypted = replayFileReader.getCryptedBlock();
        String replayExtracted =
                replayFileReader.getReplayName().substring(0, replayFileReader.getReplayName().indexOf(".wotreplay"));

        StringBuilder sb = new StringBuilder();
        String decompressed = sb.append(defaultDirectory).append(replayExtracted).append(" - Decompressed.dat").toString();
        ReplayDecrypter replayDecrypter = new ReplayDecrypter(compressedCrypted);

        sb = new StringBuilder();
        String decryptedFile = sb.append(defaultDirectory).append(replayExtracted).append(" - Decrypted.dat").toString();
        FileOutputStream decompressFile = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(decryptedFile);

            System.out.println("Decrypting : " + replayExtracted);
            replayDecrypter.decrypt(fos);


            fis = new FileInputStream(decryptedFile);
            decompressFile = new FileOutputStream(decompressed);
            ReplayDecompressor replayDecompressor = new ReplayDecompressor(fis);

            System.out.println("Decompressing : " + replayExtracted);
            byte[] decompressedData = replayDecompressor.unzip();
            decompressFile.write(decompressedData);
            fis.close();
            Files.delete(Paths.get(decryptedFile));

            return ByteBuffer.wrap(decompressedData);

        } catch (Exception e) {
            throw new CannotDecodeReplayException("Cannot decode replay", e);
        }
    }
}

@@ -104,16 +104,84 @@
 ressor;%0A
+import ca.intelliagent.replaydecoder.decompression.ZlibCompression;%0A
 import c
@@ -430,16 +430,58 @@
 .file.*;
+%0Aimport java.util.zip.DataFormatException;
 %0A%0Apublic
@@ -586,25 +586,18 @@
 ted 
-final Path defaul
+Path outpu
 tDir
@@ -754,83 +754,844 @@
     
-defaultDirectory = outputDirectory
+this.outputDirectory = outputDirectory;%0A    %7D%0A%0A    protected  ReplayDecoder(ReplayFileReader replayFileReader)%7B%0A        this.replayFileReader = replayFileReader;%0A    %7D%0A%0A    public ByteBuffer decode()%7B%0A        final byte%5B%5D compressedCrypted = replayFileReader.getCryptedBlock();%0A        final ReplayDecrypter replayDecrypter = new ReplayDecrypter(compressedCrypted);%0A        byte%5B%5D decripted = replayDecrypter.decryptToByteArray(compressedCrypted);%0A        try%7B%0A            return ByteBuffer.wrap(ZlibCompression.decompressData(decripted));%0A        %7D catch (DataFormatException e) %7B%0A            throw new CannotDecodeReplayException(%22Cannot decode replay%22, e)
 ;%0A    
+ 
-%7D%0A%0A%0A    protected ByteBuffer decode
+   %7D%0A    %7D%0A%0A    //TODO The output directory should be given here%0A    //TODO Refactor this method so it uses the decode method%0A    public ByteBuffer decodeToOutputDirectory
 () %7B
@@ -1654,32 +1654,33 @@
 CryptedBlock();%0A
+%0A
         String r
@@ -1896,38 +1896,37 @@
 sed = sb.append(
-defaul
+outpu
 tDirectory).appe
@@ -2149,14 +2149,13 @@
 end(
-defaul
+outpu
 tDir
@@ -2264,23 +2264,16 @@
 ressFile
- = null
 ;%0A      
@@ -2293,23 +2293,16 @@
 ream fis
- = null
 ;%0A      
@@ -2327,15 +2327,8 @@
  fos
- = null
 ;%0A%0A 

Add decode method without output file