Skip to content

Commit 746cc0f

Browse files
committed
Make JarFile.size() return the number of entries in the jar
Closes gh-12195
1 parent 1468585 commit 746cc0f

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -108,7 +108,7 @@ private JarFile(RandomAccessDataFile rootFile, String pathFromRoot,
108108

109109
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot,
110110
RandomAccessData data, JarEntryFilter filter, JarFileType type)
111-
throws IOException {
111+
throws IOException {
112112
super(rootFile.getFile());
113113
this.rootFile = rootFile;
114114
this.pathFromRoot = pathFromRoot;
@@ -293,7 +293,7 @@ private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
293293

294294
@Override
295295
public int size() {
296-
return (int) this.data.getSize();
296+
return this.entries.getSize();
297297
}
298298

299299
@Override

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFileEntries.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -121,6 +121,10 @@ public void visitEnd() {
121121
}
122122
}
123123

124+
int getSize() {
125+
return this.size;
126+
}
127+
124128
private void sort(int left, int right) {
125129
// Quick sort algorithm, uses hashCodes as the source but sorts all arrays
126130
if (left < right) {

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
import java.util.jar.JarInputStream;
3232
import java.util.jar.Manifest;
3333
import java.util.zip.ZipEntry;
34+
import java.util.zip.ZipFile;
3435

3536
import org.junit.Before;
3637
import org.junit.Rule;
@@ -169,7 +170,9 @@ public void getName() throws Exception {
169170

170171
@Test
171172
public void getSize() throws Exception {
172-
assertThat(this.jarFile.size()).isEqualTo((int) this.rootJarFile.length());
173+
try (ZipFile zip = new ZipFile(this.rootJarFile)) {
174+
assertThat(this.jarFile.size()).isEqualTo(zip.size());
175+
}
173176
}
174177

175178
@Test

0 commit comments

Comments
 (0)