mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-03-30 21:05:58 -05:00
Reformat to use Fabric API's checkstyle (#137)
* Reformat to use Fabric API's checkstyle * Fix * Fix * Update * Travis and fixes * possible fix for checkstyle? * Helps if i push the checkstyle.xml file... * Log checkstyle issues to console - used by travis * Fix some more issues * opps
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package net.fabricmc.loom.util;
|
||||
|
||||
import java.io.File;
|
||||
@@ -29,21 +30,18 @@ import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.logging.Logger;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
|
||||
public class DownloadUtil {
|
||||
/**
|
||||
* Download from the given {@link URL} to the given {@link File} so long as there are differences between them
|
||||
* Download from the given {@link URL} to the given {@link File} so long as there are differences between them.
|
||||
*
|
||||
* @param from The URL of the file to be downloaded
|
||||
* @param to The destination to be saved to, and compared against if it exists
|
||||
* @param logger The logger to print everything to, typically from {@link Project#getLogger()}
|
||||
*
|
||||
* @throws IOException If an exception occurs during the process
|
||||
*/
|
||||
public static void downloadIfChanged(URL from, File to, Logger logger) throws IOException {
|
||||
@@ -51,24 +49,28 @@ public class DownloadUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Download from the given {@link URL} to the given {@link File} so long as there are differences between them
|
||||
* Download from the given {@link URL} to the given {@link File} so long as there are differences between them.
|
||||
*
|
||||
* @param from The URL of the file to be downloaded
|
||||
* @param to The destination to be saved to, and compared against if it exists
|
||||
* @param logger The logger to print information to, typically from {@link Project#getLogger()}
|
||||
* @param quiet Whether to only print warnings (when <code>true</code>) or everything
|
||||
*
|
||||
* @throws IOException If an exception occurs during the process
|
||||
*/
|
||||
public static void downloadIfChanged(URL from, File to, Logger logger, boolean quiet) throws IOException {
|
||||
HttpURLConnection connection = (HttpURLConnection) from.openConnection();
|
||||
|
||||
//If the output already exists we'll use it's last modified time
|
||||
if (to.exists()) connection.setIfModifiedSince(to.lastModified());
|
||||
if (to.exists()) {
|
||||
connection.setIfModifiedSince(to.lastModified());
|
||||
}
|
||||
|
||||
//Try use the ETag if there's one for the file we're downloading
|
||||
String etag = loadETag(to, logger);
|
||||
if (etag != null) connection.setRequestProperty("If-None-Match", etag);
|
||||
|
||||
if (etag != null) {
|
||||
connection.setRequestProperty("If-None-Match", etag);
|
||||
}
|
||||
|
||||
//We want to download gzip compressed stuff
|
||||
connection.setRequestProperty("Accept-Encoding", "gzip");
|
||||
@@ -80,21 +82,29 @@ public class DownloadUtil {
|
||||
connection.connect();
|
||||
|
||||
int code = connection.getResponseCode();
|
||||
|
||||
if ((code < 200 || code > 299) && code != HttpURLConnection.HTTP_NOT_MODIFIED) {
|
||||
//Didn't get what we expected
|
||||
throw new IOException(connection.getResponseMessage());
|
||||
}
|
||||
|
||||
long modifyTime = connection.getHeaderFieldDate("Last-Modified", -1);
|
||||
|
||||
if (to.exists() && (code == HttpURLConnection.HTTP_NOT_MODIFIED || modifyTime > 0 && to.lastModified() >= modifyTime)) {
|
||||
if (!quiet) logger.info("'{}' Not Modified, skipping.", to);
|
||||
if (!quiet) {
|
||||
logger.info("'{}' Not Modified, skipping.", to);
|
||||
}
|
||||
|
||||
return; //What we've got is already fine
|
||||
}
|
||||
|
||||
long contentLength = connection.getContentLengthLong();
|
||||
if (!quiet && contentLength >= 0) logger.info("'{}' Changed, downloading {}", to, toNiceSize(contentLength));
|
||||
|
||||
try {//Try download to the output
|
||||
if (!quiet && contentLength >= 0) {
|
||||
logger.info("'{}' Changed, downloading {}", to, toNiceSize(contentLength));
|
||||
}
|
||||
|
||||
try { //Try download to the output
|
||||
FileUtils.copyInputStreamToFile(connection.getInputStream(), to);
|
||||
} catch (IOException e) {
|
||||
to.delete(); //Probably isn't good if it fails to copy/save
|
||||
@@ -102,23 +112,27 @@ public class DownloadUtil {
|
||||
}
|
||||
|
||||
//Set the modify time to match the server's (if we know it)
|
||||
if (modifyTime > 0) to.setLastModified(modifyTime);
|
||||
if (modifyTime > 0) {
|
||||
to.setLastModified(modifyTime);
|
||||
}
|
||||
|
||||
//Save the ETag (if we know it)
|
||||
String eTag = connection.getHeaderField("ETag");
|
||||
|
||||
if (eTag != null) {
|
||||
//Log if we get a weak ETag and we're not on quiet
|
||||
if (!quiet && eTag.startsWith("W/")) logger.warn("Weak ETag found.");
|
||||
if (!quiet && eTag.startsWith("W/")) {
|
||||
logger.warn("Weak ETag found.");
|
||||
}
|
||||
|
||||
saveETag(to, eTag, logger);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new file in the same directory as the given file with <code>.etag</code> on the end of the name
|
||||
* Creates a new file in the same directory as the given file with <code>.etag</code> on the end of the name.
|
||||
*
|
||||
* @param file The file to produce the ETag for
|
||||
*
|
||||
* @return The (uncreated) ETag file for the given file
|
||||
*/
|
||||
private static File getETagFile(File file) {
|
||||
@@ -126,16 +140,18 @@ public class DownloadUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to load an ETag for the given file, if it exists
|
||||
* Attempt to load an ETag for the given file, if it exists.
|
||||
*
|
||||
* @param to The file to load an ETag for
|
||||
* @param logger The logger to print errors to if it goes wrong
|
||||
*
|
||||
* @return The ETag for the given file, or <code>null</code> if it doesn't exist
|
||||
*/
|
||||
private static String loadETag(File to, Logger logger) {
|
||||
File eTagFile = getETagFile(to);
|
||||
if (!eTagFile.exists()) return null;
|
||||
|
||||
if (!eTagFile.exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Files.asCharSource(eTagFile, StandardCharsets.UTF_8).read();
|
||||
@@ -146,7 +162,7 @@ public class DownloadUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the given ETag for the given file, replacing it if it already exists
|
||||
* Saves the given ETag for the given file, replacing it if it already exists.
|
||||
*
|
||||
* @param to The file to save the ETag for
|
||||
* @param eTag The ETag to be saved
|
||||
@@ -154,8 +170,12 @@ public class DownloadUtil {
|
||||
*/
|
||||
private static void saveETag(File to, String eTag, Logger logger) {
|
||||
File eTagFile = getETagFile(to);
|
||||
|
||||
try {
|
||||
if (!eTagFile.exists()) eTagFile.createNewFile();
|
||||
if (!eTagFile.exists()) {
|
||||
eTagFile.createNewFile();
|
||||
}
|
||||
|
||||
Files.asCharSink(eTagFile, StandardCharsets.UTF_8).write(eTag);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Error saving ETag file '{}'.", eTagFile, e);
|
||||
@@ -163,10 +183,9 @@ public class DownloadUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the given number of bytes as a more human readable string
|
||||
* Format the given number of bytes as a more human readable string.
|
||||
*
|
||||
* @param bytes The number of bytes
|
||||
*
|
||||
* @return The given number of bytes formatted to kilobytes, megabytes or gigabytes if appropriate
|
||||
*/
|
||||
private static String toNiceSize(long bytes) {
|
||||
@@ -192,8 +211,9 @@ public class DownloadUtil {
|
||||
}
|
||||
|
||||
File etagFile = getETagFile(file);
|
||||
|
||||
if (etagFile.exists()) {
|
||||
etagFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user