Skip to content

Commit 814e908

Browse files
committed
Merge branch '1.5.x' into 2.0.x
2 parents 864a6b3 + 6081db5 commit 814e908

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

Diff for: spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/JarWriter.java

+15-7
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,15 @@ public int read(byte[] b) throws IOException {
381381
public int read(byte[] b, int off, int len) throws IOException {
382382
int read = (this.headerStream != null ? this.headerStream.read(b, off, len)
383383
: -1);
384-
if (read > 0) {
385-
this.position += read;
386-
}
387-
else {
388-
read = 0;
384+
if (read <= 0) {
385+
return readRemainder(b, off, len);
389386
}
387+
this.position += read;
390388
if (read < len) {
391-
read += super.read(b, off + read, len - read);
392-
this.position += read;
389+
int remainderRead = readRemainder(b, off + read, len - read);
390+
if (remainderRead > 0) {
391+
read += remainderRead;
392+
}
393393
}
394394
if (this.position >= this.headerLength) {
395395
this.headerStream = null;
@@ -401,6 +401,14 @@ public boolean hasZipHeader() {
401401
return Arrays.equals(this.header, ZIP_HEADER);
402402
}
403403

404+
private int readRemainder(byte[] b, int off, int len) throws IOException {
405+
int read = super.read(b, off, len);
406+
if (read > 0) {
407+
this.position += read;
408+
}
409+
return read;
410+
}
411+
404412
}
405413

406414
/**

Diff for: spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/ZipHeaderPeekInputStreamTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,36 @@ public void readOfSomeOfTheHeaderThenMoreThanEntireStreamReadsToEndOfStream()
100100
}
101101
}
102102

103+
@Test
104+
public void readMoreThanEntireStreamWhenStreamLengthIsLessThanZipHeaderLength()
105+
throws IOException {
106+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
107+
new ByteArrayInputStream(new byte[] { 10 }))) {
108+
byte[] bytes = new byte[8];
109+
assertThat(in.read(bytes)).isEqualTo(1);
110+
assertThat(bytes).containsExactly(10, 0, 0, 0, 0, 0, 0, 0);
111+
}
112+
}
113+
114+
@Test
115+
public void readMoreThanEntireStreamWhenStreamLengthIsSameAsHeaderLength()
116+
throws IOException {
117+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
118+
new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }))) {
119+
byte[] bytes = new byte[8];
120+
assertThat(in.read(bytes)).isEqualTo(4);
121+
assertThat(bytes).containsExactly(1, 2, 3, 4, 0, 0, 0, 0);
122+
}
123+
}
124+
125+
@Test
126+
public void readMoreThanEntireStreamWhenStreamLengthIsZero() throws IOException {
127+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
128+
new ByteArrayInputStream(new byte[0]))) {
129+
byte[] bytes = new byte[8];
130+
assertThat(in.read(bytes)).isEqualTo(-1);
131+
assertThat(bytes).containsExactly(0, 0, 0, 0, 0, 0, 0, 0);
132+
}
133+
}
134+
103135
}

0 commit comments

Comments
 (0)