Skip to content

Commit 2552184

Browse files
committed
addressing copilot comments
1 parent 753f5b5 commit 2552184

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

sdk/storage/azure-storage-common/src/main/java/com/azure/storage/common/implementation/structuredmessage/StructuredMessageEncoder.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,25 @@ private byte[] generateSegmentHeader(int segmentContentSize) {
121121
*
122122
* @param unencodedBuffer The buffer to be encoded.
123123
* @return A Flux of encoded ByteBuffers.
124-
* @throws IllegalArgumentException If the buffer length exceeds the content length, or the content has already been
125-
* encoded.
126124
*/
127125
public Flux<ByteBuffer> encode(ByteBuffer unencodedBuffer) {
128126
StorageImplUtils.assertNotNull("unencodedBuffer", unencodedBuffer);
129127

130-
if (currentContentOffset == contentLength) {
131-
return Flux
132-
.error(LOGGER.logExceptionAsError(new IllegalArgumentException("Content has already been encoded.")));
133-
}
128+
return Flux.defer(() -> {
129+
if (currentContentOffset == contentLength) {
130+
return Flux.error(
131+
LOGGER.logExceptionAsError(new IllegalArgumentException("Content has already been encoded.")));
132+
}
134133

135-
if ((unencodedBuffer.remaining() + currentContentOffset) > contentLength) {
136-
return Flux.error(
137-
LOGGER.logExceptionAsError(new IllegalArgumentException("Buffer length exceeds content length.")));
138-
}
134+
if ((unencodedBuffer.remaining() + currentContentOffset) > contentLength) {
135+
return Flux.error(
136+
LOGGER.logExceptionAsError(new IllegalArgumentException("Buffer length exceeds content length.")));
137+
}
139138

140-
if (!unencodedBuffer.hasRemaining()) {
141-
return Flux.empty();
142-
}
139+
if (!unencodedBuffer.hasRemaining()) {
140+
return Flux.empty();
141+
}
143142

144-
return Flux.defer(() -> {
145143
List<ByteBuffer> buffers = new ArrayList<>();
146144

147145
// if we are at the beginning of the message, encode message header

sdk/storage/azure-storage-common/src/test/java/com/azure/storage/common/implementation/structuredmessage/MessageEncoderTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.jupiter.params.ParameterizedTest;
1010
import org.junit.jupiter.params.provider.Arguments;
1111
import org.junit.jupiter.params.provider.MethodSource;
12+
import reactor.test.StepVerifier;
1213

1314
import java.io.ByteArrayOutputStream;
1415
import java.io.IOException;
@@ -24,6 +25,7 @@
2425
import static com.azure.storage.common.implementation.structuredmessage.StructuredMessageConstants.V1_HEADER_LENGTH;
2526
import static com.azure.storage.common.implementation.structuredmessage.StructuredMessageConstants.V1_SEGMENT_HEADER_LENGTH;
2627
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2729
import static org.junit.jupiter.api.Assertions.assertThrows;
2830

2931
public class MessageEncoderTests {
@@ -215,16 +217,18 @@ public void emptyBuffer() {
215217
public void contentAlreadyEncoded() {
216218
StructuredMessageEncoder encoder = new StructuredMessageEncoder(4, 2, StructuredMessageFlags.NONE);
217219
encoder.encode(ByteBuffer.wrap(new byte[] { 1, 2, 3, 4 })).blockLast();
218-
assertThrows(IllegalArgumentException.class,
219-
() -> encoder.encode(ByteBuffer.wrap(new byte[] { 1, 2 })).blockLast());
220+
StepVerifier.create(encoder.encode(ByteBuffer.wrap(new byte[] { 1, 2 })))
221+
.expectError(IllegalArgumentException.class)
222+
.verify();
220223
}
221224

222225
@Test
223226
public void bufferLengthExceedsContentLength() {
224227
StructuredMessageEncoder encoder = new StructuredMessageEncoder(4, 2, StructuredMessageFlags.NONE);
225228
encoder.encode(ByteBuffer.wrap(new byte[] { 1, 2, 3 })).blockLast();
226-
assertThrows(IllegalArgumentException.class,
227-
() -> encoder.encode(ByteBuffer.wrap(new byte[] { 1, 2 })).blockLast());
229+
StepVerifier.create(encoder.encode(ByteBuffer.wrap(new byte[] { 1, 2 })))
230+
.expectError(IllegalArgumentException.class)
231+
.verify();
228232
}
229233

230234
@Test

0 commit comments

Comments
 (0)