Skip to content

Commit 57179c0

Browse files
committed
Add InputStreamSourceToByteArrayConverter
Add an `InputStreamSourceToByteArrayConverter` that can be used to convert from an `InputStreamSource` (such as a `Resource`) to a byte array. Closes gh-21285
1 parent d52bf83 commit 57179c0

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/ApplicationConversionService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public static void addApplicationConverters(ConverterRegistry registry) {
116116
registry.addConverter(new StringToDataSizeConverter());
117117
registry.addConverter(new NumberToDataSizeConverter());
118118
registry.addConverter(new StringToFileConverter());
119+
registry.addConverter(new InputStreamSourceToByteArrayConverter());
119120
registry.addConverterFactory(new LenientStringToEnumConverterFactory());
120121
registry.addConverterFactory(new LenientBooleanToEnumConverterFactory());
121122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.convert;
18+
19+
import java.io.IOException;
20+
21+
import org.springframework.boot.origin.Origin;
22+
import org.springframework.core.convert.converter.Converter;
23+
import org.springframework.core.io.InputStreamSource;
24+
import org.springframework.core.io.Resource;
25+
import org.springframework.util.FileCopyUtils;
26+
27+
/**
28+
* {@link Converter} to convert from an {@link InputStreamSource} to a {@code byte[]}.
29+
*
30+
* @author Phillip Webb
31+
*/
32+
class InputStreamSourceToByteArrayConverter implements Converter<InputStreamSource, byte[]> {
33+
34+
@Override
35+
public byte[] convert(InputStreamSource source) {
36+
try {
37+
return FileCopyUtils.copyToByteArray(source.getInputStream());
38+
}
39+
catch (IOException ex) {
40+
throw new IllegalStateException("Unable to read from " + getName(source), ex);
41+
}
42+
}
43+
44+
private String getName(InputStreamSource source) {
45+
Origin origin = Origin.from(source);
46+
if (origin != null) {
47+
return origin.toString();
48+
}
49+
if (source instanceof Resource) {
50+
return ((Resource) source).getDescription();
51+
}
52+
return "input stream source";
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2012-2020 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+
* https://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.convert;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.IOException;
21+
import java.util.stream.Stream;
22+
23+
import org.junit.jupiter.params.provider.Arguments;
24+
25+
import org.springframework.boot.origin.Origin;
26+
import org.springframework.boot.origin.OriginProvider;
27+
import org.springframework.core.convert.ConversionFailedException;
28+
import org.springframework.core.convert.ConversionService;
29+
import org.springframework.core.io.InputStreamSource;
30+
import org.springframework.core.io.Resource;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
34+
import static org.mockito.BDDMockito.given;
35+
import static org.mockito.Mockito.mock;
36+
import static org.mockito.Mockito.withSettings;
37+
38+
/**
39+
* Tests for {@link InputStreamSourceToByteArrayConverter}.
40+
*
41+
* @author Phillip Webb
42+
*/
43+
class InputStreamSourceToByteArrayConverterTests {
44+
45+
@ConversionServiceTest
46+
void convertConvertsSource(ConversionService conversionService) {
47+
InputStreamSource source = () -> new ByteArrayInputStream(new byte[] { 0, 1, 2 });
48+
assertThat(conversionService.convert(source, byte[].class)).containsExactly(0, 1, 2);
49+
}
50+
51+
@ConversionServiceTest
52+
void convertWhenFailsWithIOExceptionThrowsException(ConversionService conversionService) throws Exception {
53+
InputStreamSource source = mock(InputStreamSource.class);
54+
given(source.getInputStream()).willThrow(IOException.class);
55+
assertThatExceptionOfType(ConversionFailedException.class)
56+
.isThrownBy(() -> conversionService.convert(source, byte[].class))
57+
.withCauseExactlyInstanceOf(IllegalStateException.class)
58+
.withMessageContaining("Unable to read from input stream source");
59+
}
60+
61+
@ConversionServiceTest
62+
void convertWhenFailsWithIOExceptionFromOriginProviderThrowsException(ConversionService conversionService)
63+
throws Exception {
64+
Origin origin = new TestOrigin("mylocation");
65+
InputStreamSource source = mock(InputStreamSource.class, withSettings().extraInterfaces(OriginProvider.class));
66+
given(source.getInputStream()).willThrow(IOException.class);
67+
given(((OriginProvider) source).getOrigin()).willReturn(origin);
68+
assertThatExceptionOfType(ConversionFailedException.class)
69+
.isThrownBy(() -> conversionService.convert(source, byte[].class))
70+
.withCauseExactlyInstanceOf(IllegalStateException.class)
71+
.withMessageContaining("Unable to read from mylocation");
72+
}
73+
74+
@ConversionServiceTest
75+
void convertWhenFailsWithIOExceptionFromResourceThrowsException(ConversionService conversionService)
76+
throws Exception {
77+
Resource source = mock(Resource.class);
78+
given(source.getInputStream()).willThrow(IOException.class);
79+
given(source.getDescription()).willReturn("myresource");
80+
assertThatExceptionOfType(ConversionFailedException.class)
81+
.isThrownBy(() -> conversionService.convert(source, byte[].class))
82+
.withCauseExactlyInstanceOf(IllegalStateException.class)
83+
.withMessageContaining("Unable to read from myresource");
84+
}
85+
86+
static Stream<? extends Arguments> conversionServices() {
87+
return ConversionServiceArguments
88+
.with((service) -> service.addConverter(new InputStreamSourceToByteArrayConverter()));
89+
}
90+
91+
private static class TestOrigin implements Origin {
92+
93+
private final String string;
94+
95+
TestOrigin(String string) {
96+
this.string = string;
97+
}
98+
99+
@Override
100+
public String toString() {
101+
return this.string;
102+
}
103+
104+
}
105+
106+
}

0 commit comments

Comments
 (0)