Skip to content

Commit 6081db5

Browse files
committed
Make ZipHeaderInputStream read sub 5 byte entries correctly
Closes gh-13525
1 parent 99f993b commit 6081db5

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,15 @@ public int read(byte[] b) throws IOException {
357357
public int read(byte[] b, int off, int len) throws IOException {
358358
int read = (this.headerStream != null ? this.headerStream.read(b, off, len)
359359
: -1);
360-
if (read > 0) {
361-
this.position += read;
362-
}
363-
else {
364-
read = 0;
360+
if (read <= 0) {
361+
return readRemainder(b, off, len);
365362
}
363+
this.position += read;
366364
if (read < len) {
367-
read += super.read(b, off + read, len - read);
368-
this.position += read;
365+
int remainderRead = readRemainder(b, off + read, len - read);
366+
if (remainderRead > 0) {
367+
read += remainderRead;
368+
}
369369
}
370370
if (this.position >= this.headerLength) {
371371
this.headerStream = null;
@@ -377,6 +377,14 @@ public boolean hasZipHeader() {
377377
return Arrays.equals(this.header, ZIP_HEADER);
378378
}
379379

380+
private int readRemainder(byte[] b, int off, int len) throws IOException {
381+
int read = super.read(b, off, len);
382+
if (read > 0) {
383+
this.position += read;
384+
}
385+
return read;
386+
}
387+
380388
}
381389

382390
/**

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,56 @@ public void readOfSomeOfTheHeaderThenMoreThanEntireStreamReadsToEndOfStream()
142142
}
143143
}
144144

145+
@Test
146+
public void readMoreThanEntireStreamWhenStreamLengthIsLessThanZipHeaderLength()
147+
throws IOException {
148+
ZipHeaderPeekInputStream in = null;
149+
try {
150+
in = new ZipHeaderPeekInputStream(
151+
new ByteArrayInputStream(new byte[] { 10 }));
152+
byte[] bytes = new byte[8];
153+
assertThat(in.read(bytes)).isEqualTo(1);
154+
assertThat(bytes).containsExactly(10, 0, 0, 0, 0, 0, 0, 0);
155+
}
156+
finally {
157+
if (in != null) {
158+
in.close();
159+
}
160+
}
161+
}
162+
163+
@Test
164+
public void readMoreThanEntireStreamWhenStreamLengthIsSameAsHeaderLength()
165+
throws IOException {
166+
ZipHeaderPeekInputStream in = null;
167+
try {
168+
in = new ZipHeaderPeekInputStream(
169+
new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }));
170+
byte[] bytes = new byte[8];
171+
assertThat(in.read(bytes)).isEqualTo(4);
172+
assertThat(bytes).containsExactly(1, 2, 3, 4, 0, 0, 0, 0);
173+
}
174+
finally {
175+
if (in != null) {
176+
in.close();
177+
}
178+
}
179+
}
180+
181+
@Test
182+
public void readMoreThanEntireStreamWhenStreamLengthIsZero() throws IOException {
183+
ZipHeaderPeekInputStream in = null;
184+
try {
185+
in = new ZipHeaderPeekInputStream(new ByteArrayInputStream(new byte[0]));
186+
byte[] bytes = new byte[8];
187+
assertThat(in.read(bytes)).isEqualTo(-1);
188+
assertThat(bytes).containsExactly(0, 0, 0, 0, 0, 0, 0, 0);
189+
}
190+
finally {
191+
if (in != null) {
192+
in.close();
193+
}
194+
}
195+
}
196+
145197
}

0 commit comments

Comments
 (0)