package com.destroystokyo.paperclip;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class PatchData {
    private final URL patchFile;
    private final URL originalUrl;
    private final byte[] originalHash;
    private final byte[] patchedHash;

    public PatchData(JSONObject obj) {
        final String patch = (String) obj.get("patch");
        // First try and parse the patch as a uri
        URL patchFile = PatchData.class.getResource("/" + patch);;
        if (new File(patch).exists()) {
            try {
                patchFile = new File(patch).toURI().toURL();
            } catch (MalformedURLException ignored) {}
        }
        if (patchFile == null) {
            throw new IllegalArgumentException("Couldn't find " + patch);
        }
        this.patchFile = patchFile;
        try {
            this.originalUrl = new URL((String) obj.get("sourceUrl"));
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Invalid URL", e);
        }
        this.originalHash = Utils.fromHex((String) obj.get("originalHash"));
        this.patchedHash = Utils.fromHex((String) obj.get("patchedHash"));
    }

    public URL getPatchFile() {
        return patchFile;
    }

    public URL getOriginalUrl() {
        return originalUrl;
    }

    public byte[] getOriginalHash() {
        return originalHash;
    }

    public byte[] getPatchedHash() {
        return patchedHash;
    }

    public static PatchData parse(InputStream in) throws IOException {
        try {
            Object obj = new JSONParser().parse(new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)));
            return new PatchData((JSONObject) obj);
        } catch (ParseException | ClassCastException e) {
            throw new IllegalArgumentException("Invalid json", e);
        }
    }
}

@@ -31,16 +31,140 @@
 rclip;%0A%0A
+import org.json.simple.JSONObject;%0Aimport org.json.simple.parser.JSONParser;%0Aimport org.json.simple.parser.ParseException;%0A%0A
 import j
@@ -178,32 +178,32 @@
 BufferedReader;%0A
-
 import java.io.F
@@ -340,65 +340,8 @@
 on;%0A
-import java.net.URI;%0Aimport java.net.URISyntaxException;%0A
 impo
@@ -404,132 +404,8 @@
 s;%0A%0A
-import org.json.simple.JSONObject;%0Aimport org.json.simple.parser.JSONParser;%0Aimport org.json.simple.parser.ParseException;%0A%0A
 publ
@@ -716,16 +716,16 @@
 s a uri%0A
+
         
@@ -781,17 +781,16 @@
  patch);
-;
 %0A       

Fix double ;