Skip to content

Commit 7c7259b

Browse files
yeaxiwilkinsona
authored andcommitted
Make JarURLConnection return entry's last modified time
See spring-projectsgh-9893
1 parent 5c13b8b commit 7c7259b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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

Lines changed: 16 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,21 @@ public Permission getPermission() throws IOException {
234235
return this.permission;
235236
}
236237

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

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)