Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-30010
  • Loading branch information
rstoyanchev committed Mar 2, 2023
1 parent df1f813 commit c56c16d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,10 @@ public boolean supports(Class<?> clazz) {
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,11 +32,13 @@ public class ByteArrayHttpMessageConverterTests {

private ByteArrayHttpMessageConverter converter;


@BeforeEach
public void setUp() {
converter = new ByteArrayHttpMessageConverter();
}


@Test
public void canRead() {
assertThat(converter.canRead(byte[].class, new MediaType("application", "octet-stream"))).isTrue();
Expand Down Expand Up @@ -73,10 +75,8 @@ public void write() throws IOException {
byte[] body = new byte[]{0x1, 0x2};
converter.write(body, null, outputMessage);
assertThat(outputMessage.getBodyAsBytes()).as("Invalid result").isEqualTo(body);
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type").isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
assertThat(outputMessage.getHeaders().getContentLength())
.as("Invalid content-length").isEqualTo(2);
assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
assertThat(outputMessage.getHeaders().getContentLength()).isEqualTo(2);
}

}

0 comments on commit c56c16d

Please sign in to comment.