Browse Source

Polishing contribution

Closes gh-30010
pull/30069/head
rstoyanchev 3 years ago
parent
commit
c56c16d7ba
  1. 14
      spring-web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java
  2. 10
      spring-web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java

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

@ -51,16 +51,10 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<
} }
@Override @Override
public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage inputMessage) throws IOException { public byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage message) throws IOException {
long contentLength = inputMessage.getHeaders().getContentLength(); long length = message.getHeaders().getContentLength();
final int len; return (length >= 0 && length < Integer.MAX_VALUE ?
if (contentLength >= 0 && contentLength <= Integer.MAX_VALUE) { message.getBody().readNBytes((int) length) : message.getBody().readAllBytes());
len = (int) contentLength;
}
else {
len = Integer.MAX_VALUE;
}
return inputMessage.getBody().readNBytes(len);
} }
@Override @Override

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,11 +32,13 @@ public class ByteArrayHttpMessageConverterTests {
private ByteArrayHttpMessageConverter converter; private ByteArrayHttpMessageConverter converter;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
converter = new ByteArrayHttpMessageConverter(); converter = new ByteArrayHttpMessageConverter();
} }
@Test @Test
public void canRead() { public void canRead() {
assertThat(converter.canRead(byte[].class, new MediaType("application", "octet-stream"))).isTrue(); assertThat(converter.canRead(byte[].class, new MediaType("application", "octet-stream"))).isTrue();
@ -73,10 +75,8 @@ public class ByteArrayHttpMessageConverterTests {
byte[] body = new byte[]{0x1, 0x2}; byte[] body = new byte[]{0x1, 0x2};
converter.write(body, null, outputMessage); converter.write(body, null, outputMessage);
assertThat(outputMessage.getBodyAsBytes()).as("Invalid result").isEqualTo(body); assertThat(outputMessage.getBodyAsBytes()).as("Invalid result").isEqualTo(body);
assertThat(outputMessage.getHeaders().getContentType()) assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_OCTET_STREAM); assertThat(outputMessage.getHeaders().getContentLength()).isEqualTo(2);
assertThat(outputMessage.getHeaders().getContentLength())
.as("Invalid content-length").isEqualTo(2);
} }
} }

Loading…
Cancel
Save