From 746cc0f70bdc47409f3ce6c8b1c15cde3ec61140 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 23 Feb 2018 17:09:21 +0000 Subject: [PATCH] Make JarFile.size() return the number of entries in the jar Closes gh-12195 --- .../java/org/springframework/boot/loader/jar/JarFile.java | 6 +++--- .../springframework/boot/loader/jar/JarFileEntries.java | 6 +++++- .../org/springframework/boot/loader/jar/JarFileTests.java | 7 +++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java index 2361ba7d03e..11c1e16fb29 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -108,7 +108,7 @@ public class JarFile extends java.util.jar.JarFile { private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data, JarEntryFilter filter, JarFileType type) - throws IOException { + throws IOException { super(rootFile.getFile()); this.rootFile = rootFile; this.pathFromRoot = pathFromRoot; @@ -293,7 +293,7 @@ public class JarFile extends java.util.jar.JarFile { @Override public int size() { - return (int) this.data.getSize(); + return this.entries.getSize(); } @Override diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileEntries.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileEntries.java index 59968f9d942..2107c86391a 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileEntries.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileEntries.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -121,6 +121,10 @@ class JarFileEntries implements CentralDirectoryVisitor, Iterable { } } + int getSize() { + return this.size; + } + private void sort(int left, int right) { // Quick sort algorithm, uses hashCodes as the source but sorts all arrays if (left < right) { diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java index c4e5e791048..531ce356967 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarInputStream; import java.util.jar.Manifest; import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import org.junit.Before; import org.junit.Rule; @@ -169,7 +170,9 @@ public class JarFileTests { @Test public void getSize() throws Exception { - assertThat(this.jarFile.size()).isEqualTo((int) this.rootJarFile.length()); + try (ZipFile zip = new ZipFile(this.rootJarFile)) { + assertThat(this.jarFile.size()).isEqualTo(zip.size()); + } } @Test