package edu.pdx.cs410J.grader;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.io.*;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

class JarMaker {
  private Map<File, String> sourceFilesAndNames;
  private File jarFile;
  private String createdBy;
  private String manifestVersion;

  public JarMaker(Map<File, String> sourceFilesAndNames, File jarFile, String createdBy, String manifestVersion) {
    this.sourceFilesAndNames = sourceFilesAndNames;
    this.jarFile = jarFile;
    this.createdBy = createdBy;
    this.manifestVersion = manifestVersion;
  }

  public File makeJar() throws IOException {
    // Create a Manifest for the Jar file containing the name of the
    // author (userName) and a version that is based on the current
    // date/time.
    Manifest manifest = new Manifest();
    Attributes attrs = manifest.getMainAttributes();
    attrs.put(new Attributes.Name("Created-By"), createdBy);

    attrs.put(Attributes.Name.MANIFEST_VERSION, manifestVersion);

    // Create a JarOutputStream around the jar file
    JarOutputStream jos;
    {
      OutputStream os = new FileOutputStream(jarFile);
      jos = new JarOutputStream(os, manifest);
      jos.setMethod(JarOutputStream.DEFLATED);
    }

    // Add the source files to the Jar
    for (Map.Entry<File, String> fileEntry : sourceFilesAndNames.entrySet()) {
      {
        File file = fileEntry.getKey();
        String fileName = fileEntry.getValue();
        System.out.println("Adding " + fileName + " to jar");
        JarEntry entry = new JarEntry(fileName);
        entry.setTime(file.lastModified());
        entry.setSize(file.length());

        InputStream is;
        byte[] buffer = new byte[1024];
        int read;

        entry.setMethod(JarEntry.DEFLATED);

        // Add the entry to the JAR file
        jos.putNextEntry(entry);
        is = new BufferedInputStream(new FileInputStream(file));
        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
          jos.write(buffer, 0, read);
        }
        is.close();
        jos.closeEntry();
      }
    }

    return jarFile;
  }

  public static void main(String[] args) throws IOException {
    String jarFileName = null;
    Set<File> files = Sets.newHashSet();

    for (String arg : args) {
      if (jarFileName == null) {
        jarFileName = arg;

      } else {
        File file = new File(arg);
        if (file.exists()) {
          files.add(file);
        }
      }
    }

    if (jarFileName == null) {
      usage("Missing jar file name");
    }

    if (files.isEmpty()) {
      usage("Missing files");
    }

    Map<File, String> sourceFilesAndNames = Maps.toMap(files, new Function<File, String>() {
      @Override
      public String apply(File file) {
        return file.getPath();
      }
    });

    assert jarFileName != null;
    File jarFile = new File(jarFileName);

    new JarMaker(sourceFilesAndNames, jarFile, System.getProperty("user.name"), new Date().toString()).makeJar();
  }

  private static void usage(String message) {
    PrintStream err = System.err;
    err.println("** " + message);
    err.println("java JarMaker jarFileName files+");
    err.println("  jarFileName    The name of the jar file to create");
    err.println("  files+         One or more files to include in the jar");
    err.println();

    System.exit(1);
  }
}

@@ -142,16 +142,57 @@
 ct.Sets;
+%0Aimport com.google.common.io.ByteStreams;
 %0A%0Aimport
@@ -1334,22 +1334,18 @@
  jos
-;%0A    %7B%0A      
+ = new Jar
 Outp
@@ -1356,14 +1356,9 @@
 ream
- os = 
+(
 new 
@@ -1386,44 +1386,8 @@
 ile)
-;%0A      jos = new JarOutputStream(os
 , ma
@@ -1395,18 +1395,16 @@
 ifest);%0A
-  
     jos.
@@ -1439,22 +1439,16 @@
 FLATED);
-%0A    %7D
 %0A%0A    //
@@ -1853,91 +1853,8 @@
 );%0A%0A
-        InputStream is;%0A        byte%5B%5D buffer = new byte%5B1024%5D;%0A        int read;%0A%0A
     
@@ -1972,207 +1972,67 @@
 y);%0A
-  
+%0A
       
-is = new BufferedInputStream(new FileInputStream(file));%0A        while ((read = is.read(buffer, 0, buffer.length)) != -1) %7B%0A          jos.write(buffer, 0, read);%0A        %7D%0A        is.close();
+  ByteStreams.copy(new FileInputStream(file), jos);%0A
 %0A   

Use ByteStreams.copy() to copy file into jar file.