/*
* 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;
}
}