/*
 * A Gradle plugin for the creation of Minecraft mods and MinecraftForge plugins.
 * Copyright (C) 2013 Minecraft Forge
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package net.minecraftforge.gradle.tasks;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;

import net.minecraftforge.gradle.common.Constants;
import net.minecraftforge.gradle.util.caching.Cached;
import net.minecraftforge.gradle.util.caching.CachedTask;

import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.ParallelizableTask;
import org.gradle.api.tasks.TaskAction;

@ParallelizableTask
public class Download extends CachedTask
{
    @Input
    private Object url;

    @Cached
    @OutputFile
    private Object output;

    @TaskAction
    public void doTask() throws IOException
    {
        File outputFile = getProject().file(getOutput());
        outputFile.getParentFile().mkdirs();
        outputFile.createNewFile();

        getLogger().info("Downloading " + getUrl() + " to " + outputFile);

        HttpURLConnection connect = (HttpURLConnection) (new URL(getUrl())).openConnection();
        connect.setRequestProperty("User-Agent", Constants.USER_AGENT);
        connect.setInstanceFollowRedirects(true);

        ReadableByteChannel  inChannel  = Channels.newChannel(connect.getInputStream());
        FileChannel          outChannel = new FileOutputStream(outputFile).getChannel();

        // If length is longer than what is available, it copies what is available according to java docs.
        // Therefore, I use Long.MAX_VALUE which is a theoretical maximum.
        outChannel.transferFrom(inChannel, 0, Long.MAX_VALUE);

        outChannel.close();
        inChannel.close();

        getLogger().info("Download complete");
    }

    public File getOutput()
    {
        return getProject().file(output);
    }

    public void setOutput(Object output)
    {
        this.output = output;
    }

    public String getUrl()
    {
        return Constants.resolveString(url);
    }

    public void setUrl(Object url)
    {
        this.url = url;
    }
}

@@ -994,65 +994,8 @@
 on;%0A
-import java.io.InputStream;%0Aimport java.io.OutputStream;%0A
 impo
@@ -1021,24 +1021,24 @@
 Connection;%0A
+
 import java.
@@ -1495,16 +1495,57 @@
 ction;%0A%0A
+import com.google.common.io.Closeables;%0A%0A
 @Paralle
@@ -2295,28 +2295,28 @@
 File
-Channel     
+OutputStream
      out
 Chan
@@ -2311,23 +2311,20 @@
      out
-Channel
+File
  = new F
@@ -2350,16 +2350,67 @@
 putFile)
+;%0A        FileChannel          outChannel = outFile
 .getChan
@@ -2659,24 +2659,24 @@
 AX_VALUE);%0A%0A
-
         outC
@@ -2690,16 +2690,97 @@
 close();
+ //Should close outFile but just in case%0A        Closeables.close(outFile, true);
 %0A       

Fix closeable warning.