package com.github.pozo.punk;

import com.github.pozo.punk.png.PNGChunk;
import com.github.pozo.punk.png.PNGChunkList;
import com.github.pozo.punk.png.PNGReader;
import com.github.pozo.punk.png.PNGWriter;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by pozo on 2016.07.14..
 */
public class Punk {
    private static final String CHUNK_NAME = "puNk";

    public void encode(InputStream injectable, InputStream targetPNG, OutputStream target) throws IOException {
        final FileReader fileReader = new FileReader(injectable);
        final PNGReader png = new PNGReader(targetPNG);

        final PNGChunkList pngChunks = png.readChunks();
        final int beforeLastIndex = pngChunks.size() - 1;

        final byte[] fileContent = fileReader.readFully();
        final PNGChunk pngChunk = new PNGChunk(CHUNK_NAME, fileContent);

        pngChunks.insert(beforeLastIndex, pngChunk);

        byte[] bytes = pngChunks.toByteArray();

        PNGWriter pngWriter = new PNGWriter(target);
        pngWriter.writeFully(bytes);
    }

    public void decode(InputStream source, OutputStream target) throws IOException {

    }
}

@@ -60,148 +60,9 @@
 png.
-PNGChunk;%0Aimport com.github.pozo.punk.png.PNGChunkList;%0Aimport com.github.pozo.punk.png.PNGReader;%0Aimport com.github.pozo.punk.png.PNGWriter
+*
 ;%0A%0Ai
@@ -75,27 +75,17 @@
 java.io.
-IOException
+*
 ;%0Aimport
@@ -94,51 +94,21 @@
 ava.
-io.InputStream;%0Aimport java.io.OutputStream
+util.Iterator
 ;%0A%0A/
@@ -946,57 +946,970 @@
 eam 
-source, OutputStream target) throws IOException %7B
+targetPNG, OutputStream target) throws IOException %7B%0A        final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(target);%0A%0A        final PNGReader png = new PNGReader(targetPNG);%0A        final PNGChunkList pngChunks = png.readChunks();%0A%0A        boolean hasPunkChunk = false;%0A%0A        final Iterator%3CPNGChunkHolder%3E iterator = pngChunks.iterator();%0A%0A        while(iterator.hasNext() && !hasPunkChunk) %7B%0A            final PNGChunkHolder pngChunk = iterator.next();%0A%0A            final String type = pngChunk.getPngChunk().getType();%0A            final byte%5B%5D data = pngChunk.getPngChunk().getData();%0A%0A            if(CHUNK_NAME.equals(type)) %7B%0A                bufferedOutputStream.write(data);%0A                bufferedOutputStream.close();%0A%0A                hasPunkChunk = true;%0A            %7D%0A        %7D%0A%0A        if(!hasPunkChunk) %7B%0A            bufferedOutputStream.close();%0A            throw new IOException(%22puNk chunk is not found%22);%0A        %7D
 %0A%0A  

decode part