一、前言
关于gradle-wrapper源码包org.gradle.wrapper.IDownload、org.gradle.wrapper.Download定义下载功能接口及实现类,主要基于java.net.URL、java.net.URLConnection进行指定URL地址访问,在基于BufferedOutputStream缓存到本地路径下,详情参见源码说明。
二、源码说明
1.IDownload接口
package org.gradle.wrapper;@b@@b@import java.io.File;@b@@b@public abstract interface IDownload@b@{@b@ public abstract void download(String paramString, File paramFile)@b@ throws Exception;@b@}
2.Download实现类
package org.gradle.wrapper;@b@@b@import java.io.BufferedOutputStream;@b@import java.io.File;@b@import java.io.FileOutputStream;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.io.PrintStream;@b@import java.net.URL;@b@import java.net.URLConnection;@b@@b@class Download@b@ implements IDownload@b@{@b@ private static final int PROGRESS_CHUNK = 20000;@b@ private static final int BUFFER_SIZE = 10000;@b@@b@ public void download(String address, File destination)@b@ throws Exception@b@ {@b@ if (destination.exists())@b@ return;@b@@b@ destination.getParentFile().mkdirs();@b@@b@ downloadInternal(address, destination);@b@ }@b@@b@ private void downloadInternal(String address, File destination) throws Exception {@b@ OutputStream out = null;@b@@b@ InputStream in = null;@b@ try {@b@ URL url = new URL(address);@b@ out = new BufferedOutputStream(new FileOutputStream(destination));@b@@b@ URLConnection conn = url.openConnection();@b@ in = conn.getInputStream();@b@ byte[] buffer = new byte[10000];@b@@b@ long progressCounter = 0L;@b@ while ((numRead = in.read(buffer)) != -1) {@b@ int numRead;@b@ progressCounter += numRead;@b@ if (progressCounter / 20000L > 0L) {@b@ System.out.print(".");@b@ progressCounter -= 20000L;@b@ }@b@ out.write(buffer, 0, numRead);@b@ }@b@ } finally {@b@ System.out.println("");@b@ if (in != null)@b@ in.close();@b@@b@ if (out != null)@b@ out.close();@b@ }@b@ }@b@}