Skip to content

Commit c56c16d

Browse files
committed
Polishing contribution
Closes gh-30010
1 parent df1f813 commit c56c16d

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,10 @@ public boolean supports(Class<?> clazz) {
5151
}
5252

5353
@Override
54-
public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage inputMessage) throws IOException {
55-
long contentLength = inputMessage.getHeaders().getContentLength();
56-
final int len;
57-
if (contentLength >= 0 && contentLength <= Integer.MAX_VALUE) {
58-
len = (int) contentLength;
59-
}
60-
else {
61-
len = Integer.MAX_VALUE;
62-
}
63-
return inputMessage.getBody().readNBytes(len);
54+
public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage message) throws IOException {
55+
long length = message.getHeaders().getContentLength();
56+
return (length >= 0 && length < Integer.MAX_VALUE ?
57+
message.getBody().readNBytes((int) length) : message.getBody().readAllBytes());
6458
}
6559

6660
@Override

spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,11 +32,13 @@ public class ByteArrayHttpMessageConverterTests {
3232

3333
private ByteArrayHttpMessageConverter converter;
3434

35+
3536
@BeforeEach
3637
public void setUp() {
3738
converter = new ByteArrayHttpMessageConverter();
3839
}
3940

41+
4042
@Test
4143
public void canRead() {
4244
assertThat(converter.canRead(byte[].class, new MediaType("application", "octet-stream"))).isTrue();
@@ -73,10 +75,8 @@ public void write() throws IOException {
7375
byte[] body = new byte[]{0x1, 0x2};
7476
converter.write(body, null, outputMessage);
7577
assertThat(outputMessage.getBodyAsBytes()).as("Invalid result").isEqualTo(body);
76-
assertThat(outputMessage.getHeaders().getContentType())
77-
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
78-
assertThat(outputMessage.getHeaders().getContentLength())
79-
.as("Invalid content-length").isEqualTo(2);
78+
assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
79+
assertThat(outputMessage.getHeaders().getContentLength()).isEqualTo(2);
8080
}
8181

8282
}

0 commit comments

Comments
 (0)