Skip to content

Commit d9d7499

Browse files
committed
Merge branch '1.5.x' into 2.0.x
2 parents 7a27882 + 34af023 commit d9d7499

File tree

2 files changed

+127
-7
lines changed

2 files changed

+127
-7
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,26 +339,34 @@ public void write(OutputStream outputStream) throws IOException {
339339
/**
340340
* {@link InputStream} that can peek ahead at zip header bytes.
341341
*/
342-
private static class ZipHeaderPeekInputStream extends FilterInputStream {
342+
static class ZipHeaderPeekInputStream extends FilterInputStream {
343343

344344
private static final byte[] ZIP_HEADER = new byte[] { 0x50, 0x4b, 0x03, 0x04 };
345345

346346
private final byte[] header;
347347

348+
private final int headerLength;
349+
350+
private int position;
351+
348352
private ByteArrayInputStream headerStream;
349353

350354
protected ZipHeaderPeekInputStream(InputStream in) throws IOException {
351355
super(in);
352356
this.header = new byte[4];
353-
int len = in.read(this.header);
354-
this.headerStream = new ByteArrayInputStream(this.header, 0, len);
357+
this.headerLength = in.read(this.header);
358+
this.headerStream = new ByteArrayInputStream(this.header, 0,
359+
this.headerLength);
355360
}
356361

357362
@Override
358363
public int read() throws IOException {
359364
int read = (this.headerStream != null ? this.headerStream.read() : -1);
360365
if (read != -1) {
361-
this.headerStream = null;
366+
this.position++;
367+
if (this.position >= this.headerLength) {
368+
this.headerStream = null;
369+
}
362370
return read;
363371
}
364372
return super.read();
@@ -373,11 +381,20 @@ public int read(byte[] b) throws IOException {
373381
public int read(byte[] b, int off, int len) throws IOException {
374382
int read = (this.headerStream != null ? this.headerStream.read(b, off, len)
375383
: -1);
376-
if (read != -1) {
384+
if (read > 0) {
385+
this.position += read;
386+
}
387+
else {
388+
read = 0;
389+
}
390+
if (read < len) {
391+
read += super.read(b, off + read, len - read);
392+
this.position += read;
393+
}
394+
if (this.position >= this.headerLength) {
377395
this.headerStream = null;
378-
return read;
379396
}
380-
return super.read(b, off, len);
397+
return read;
381398
}
382399

383400
public boolean hasZipHeader() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
/**
20+
* Tests for {@link ZipHeaderPeekInputStream}.
21+
*
22+
* @author Andy Wilkinson
23+
*/
24+
import java.io.ByteArrayInputStream;
25+
import java.io.IOException;
26+
27+
import org.junit.Test;
28+
29+
import org.springframework.boot.loader.tools.JarWriter.ZipHeaderPeekInputStream;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
public class ZipHeaderPeekInputStreamTests {
34+
35+
@Test
36+
public void hasZipHeaderReturnsTrueWhenStreamStartsWithZipHeader()
37+
throws IOException {
38+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
39+
new ByteArrayInputStream(new byte[] { 0x50, 0x4b, 0x03, 0x04, 5, 6 }));) {
40+
assertThat(in.hasZipHeader()).isTrue();
41+
}
42+
}
43+
44+
@Test
45+
public void hasZipHeaderReturnsFalseWheStreamDoesNotStartWithZipHeader()
46+
throws IOException {
47+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
48+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) {
49+
assertThat(in.hasZipHeader()).isFalse();
50+
}
51+
}
52+
53+
@Test
54+
public void readIndividualBytes() throws IOException {
55+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
56+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) {
57+
assertThat(in.read()).isEqualTo(0);
58+
assertThat(in.read()).isEqualTo(1);
59+
assertThat(in.read()).isEqualTo(2);
60+
assertThat(in.read()).isEqualTo(3);
61+
assertThat(in.read()).isEqualTo(4);
62+
assertThat(in.read()).isEqualTo(5);
63+
}
64+
}
65+
66+
@Test
67+
public void readMultipleBytes() throws IOException {
68+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
69+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) {
70+
byte[] bytes = new byte[3];
71+
assertThat(in.read(bytes)).isEqualTo(3);
72+
assertThat(bytes).containsExactly(0, 1, 2);
73+
assertThat(in.read(bytes)).isEqualTo(3);
74+
assertThat(bytes).containsExactly(3, 4, 5);
75+
assertThat(in.read(bytes)).isEqualTo(-1);
76+
}
77+
}
78+
79+
@Test
80+
public void readingMoreThanEntireStreamReadsToEndOfStream() throws IOException {
81+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
82+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) {
83+
byte[] bytes = new byte[8];
84+
assertThat(in.read(bytes)).isEqualTo(6);
85+
assertThat(bytes).containsExactly(0, 1, 2, 3, 4, 5, 0, 0);
86+
assertThat(in.read(bytes)).isEqualTo(-1);
87+
}
88+
}
89+
90+
@Test
91+
public void readOfSomeOfTheHeaderThenMoreThanEntireStreamReadsToEndOfStream()
92+
throws IOException {
93+
try (ZipHeaderPeekInputStream in = new ZipHeaderPeekInputStream(
94+
new ByteArrayInputStream(new byte[] { 0, 1, 2, 3, 4, 5 }))) {
95+
byte[] bytes = new byte[8];
96+
assertThat(in.read(bytes, 0, 3)).isEqualTo(3);
97+
assertThat(bytes).containsExactly(0, 1, 2, 0, 0, 0, 0, 0);
98+
assertThat(in.read(bytes, 3, 5)).isEqualTo(3);
99+
assertThat(bytes).containsExactly(0, 1, 2, 3, 4, 5, 0, 0);
100+
}
101+
}
102+
103+
}

0 commit comments

Comments
 (0)