package com.k13n.lt_codes;

import java.io.*;
import java.net.URL;

import org.junit.Test;
import static org.junit.Assert.*;

public class AppTest {
  private static final String FILENAME = "firework.jpg";
  private static final String OUTFILE = "/tmp/" + FILENAME + ".out";
  private static final int PACKET_SIZE = 512;

  @Test
  public void itWorksWithPerfectChannel() throws Exception {
    byte[] data = readFile(FILENAME);
    Encoder enc = new Encoder(data, PACKET_SIZE);
    final Decoder dec = new DefaultDecoder(enc.getSeed(), enc.getNPackets());

    enc.encode(new Encoder.Callback() {
      public boolean call(Encoder encoder, TransmissonPacket packet) {
        return dec.receive(packet);
      }
    });

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    dec.write(bos);
    assertArrayEquals(data, bos.toByteArray());

    dec.write(new FileOutputStream(OUTFILE));
  }

  @Test
  public void itWorksWithLossyChannel() throws Exception {
    byte[] data = readFile(FILENAME);
    Encoder enc = new Encoder(data, PACKET_SIZE);
    final Decoder dec = new DefaultDecoder(enc.getSeed(), enc.getNPackets());

    final ErasureChannel channel = new ErasureChannel(
      new ErasureChannel.Callback() {
        @Override
        public void call(ErasureChannel channel, TransmissonPacket packet) {
          dec.receive(packet);
        }
      }, 0.3);
    enc.encode(new Encoder.Callback() {
      public boolean call(Encoder encoder, TransmissonPacket packet) {
        channel.transmit(packet);
        return dec.isDecodingFinished();
      }
    });

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    dec.write(bos);
    assertArrayEquals(data, bos.toByteArray());

    dec.write(new FileOutputStream(OUTFILE));
  }

  private byte[] readFile(String filename) {
    filename = "/" + filename;
    byte[] data = null;
    try {
      URL url = this.getClass().getResource(filename);
      InputStream is = getClass().getResourceAsStream(filename);
      assertNotNull(url);
      File file = new File(url.getFile());

      data = new byte[(int) file.length()];
      DataInputStream s = new DataInputStream(is);
      s.readFully(data);
      s.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return data;
  }

}

@@ -313,11 +313,12 @@
 E = 
-512
+1024
 ;%0A%0A 
@@ -465,32 +465,34 @@
 , PACKET_SIZE);%0A
+//
     final Decode
@@ -548,24 +548,104 @@
 NPackets());
+%0A    final Decoder dec = new IncrementalDecoder(enc.getNPackets(), PACKET_SIZE);
 %0A%0A    enc.en
@@ -790,32 +790,79 @@
      %7D%0A    %7D);%0A%0A
+    dec.write(new FileOutputStream(OUTFILE));%0A%0A
     ByteArrayOut
@@ -977,55 +977,8 @@
 ());
-%0A%0A    dec.write(new FileOutputStream(OUTFILE));
 %0A  %7D
@@ -1130,24 +1130,26 @@
 CKET_SIZE);%0A
+//
     final De
@@ -1209,24 +1209,104 @@
 NPackets());
+%0A    final Decoder dec = new IncrementalDecoder(enc.getNPackets(), PACKET_SIZE);
 %0A%0A    final 
@@ -1738,24 +1738,71 @@
  %7D%0A    %7D);%0A%0A
+    dec.write(new FileOutputStream(OUTFILE));%0A%0A
     ByteArra
@@ -1862,32 +1862,32 @@
 dec.write(bos);%0A
+
     assertArrayE
@@ -1921,55 +1921,8 @@
 ());
-%0A%0A    dec.write(new FileOutputStream(OUTFILE));
 %0A  %7D

Use some binary data (image) in AppTest