Skip to content

Commit 362a8ea

Browse files
committed
Merge pull request #9893 from Rostyslav Dudka
* gh-9893: Polish "Make JarURLConnection return entry's last modified time" Make JarURLConnection return entry's last modified time
2 parents 5c13b8b + b7ac5f2 commit 362a8ea

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Phillip Webb
3838
* @author Andy Wilkinson
39+
* @author Rostyslav Dudka
3940
*/
4041
final class JarURLConnection extends java.net.JarURLConnection {
4142

@@ -234,6 +235,20 @@ public Permission getPermission() throws IOException {
234235
return this.permission;
235236
}
236237

238+
@Override
239+
public long getLastModified() {
240+
if (this.jarFile == null || this.jarEntryName.isEmpty()) {
241+
return 0;
242+
}
243+
try {
244+
JarEntry entry = getJarEntry();
245+
return (entry == null ? 0 : entry.getTime());
246+
}
247+
catch (IOException ex) {
248+
return 0;
249+
}
250+
}
251+
237252
static void setUseFastExceptions(boolean useFastExceptions) {
238253
JarURLConnection.useFastExceptions.set(useFastExceptions);
239254
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*
3636
* @author Andy Wilkinson
3737
* @author Phillip Webb
38+
* @author Rostyslav Dudka
3839
*/
3940
public class JarURLConnectionTests {
4041

@@ -150,6 +151,14 @@ public void getContentLengthLongReturnsLengthOfUnderlyingEntry() throws Exceptio
150151
assertThat(url.openConnection().getContentLengthLong()).isEqualTo(1);
151152
}
152153

154+
@Test
155+
public void getLastModifiedReturnsLastModifiedTimeOfJarEntry() throws Exception {
156+
URL url = new URL("jar:file:" + getAbsolutePath() + "!/1.dat");
157+
JarURLConnection connection = JarURLConnection.get(url, this.jarFile);
158+
assertThat(connection.getLastModified())
159+
.isEqualTo(connection.getJarEntry().getTime());
160+
}
161+
153162
private String getAbsolutePath() {
154163
return this.rootJarFile.getAbsolutePath().replace('\\', '/');
155164
}

0 commit comments

Comments
 (0)