Skip to content

Commit 1cea029

Browse files
committed
Some simple code & Javadoc clean up for TCP/IP serializers
1 parent 53445fe commit 1cea029

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/ByteArrayLengthHeaderSerializer.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121
import java.io.OutputStream;
2222
import java.nio.ByteBuffer;
2323

24+
import org.springframework.util.Assert;
25+
2426
/**
2527
* Reads data in an InputStream to a byte[]; data must be preceded by
2628
* a binary length (network byte order, not included in resulting byte[]).
27-
*
29+
* <p>
2830
* Writes a byte[] to an OutputStream after a binary length.
2931
* The length field contains the length of data following the length
3032
* field. (network byte order).
31-
*
33+
* <p>
3234
* The default length field is a 4 byte signed integer. During deserialization,
3335
* negative values will be rejected.
3436
* Other options are an unsigned byte, and unsigned short.
35-
*
37+
* <p>
3638
* For other header formats, override {@link #readHeader(InputStream)} and
3739
* {@link #writeHeader(OutputStream, int)}.
3840
*
@@ -80,11 +82,10 @@ public ByteArrayLengthHeaderSerializer() {
8082
* @param headerSize The header size.
8183
*/
8284
public ByteArrayLengthHeaderSerializer(int headerSize) {
83-
if (headerSize != HEADER_SIZE_INT &&
84-
headerSize != HEADER_SIZE_UNSIGNED_BYTE &&
85-
headerSize != HEADER_SIZE_UNSIGNED_SHORT) {
86-
throw new IllegalArgumentException("Illegal header size: " + headerSize);
87-
}
85+
Assert.isTrue(headerSize == HEADER_SIZE_INT
86+
|| headerSize == HEADER_SIZE_UNSIGNED_BYTE
87+
|| headerSize == HEADER_SIZE_UNSIGNED_SHORT,
88+
() -> "Illegal header size: " + headerSize);
8889
this.headerSize = headerSize;
8990
}
9091

@@ -177,6 +178,7 @@ public void serialize(byte[] bytes, OutputStream outputStream) throws IOExceptio
177178
*/
178179
protected int read(InputStream inputStream, byte[] buffer, boolean header)
179180
throws IOException {
181+
180182
int lengthRead = 0;
181183
int needed = buffer.length;
182184
while (lengthRead < needed) {
@@ -247,9 +249,7 @@ protected int readHeader(InputStream inputStream) throws IOException {
247249
case HEADER_SIZE_INT:
248250
int messageLength = ByteBuffer.wrap(lengthPart).getInt();
249251
if (messageLength < 0) {
250-
throw new IllegalArgumentException("Length header: "
251-
+ messageLength
252-
+ " is negative");
252+
throw new IllegalArgumentException("Length header: " + messageLength + " is negative");
253253
}
254254
return messageLength;
255255
case HEADER_SIZE_UNSIGNED_BYTE:
@@ -260,8 +260,8 @@ protected int readHeader(InputStream inputStream) throws IOException {
260260
throw new IllegalArgumentException("Bad header size: " + this.headerSize);
261261
}
262262
}
263-
catch (SoftEndOfStreamException e) { // NOSONAR catch and throw
264-
throw e; // it's an IO exception and we don't want an event for this
263+
catch (SoftEndOfStreamException ex) { // NOSONAR catch and throw
264+
throw ex; // it's an IO exception, and we don't want an event for this
265265
}
266266
catch (IOException | RuntimeException ex) {
267267
publishEvent(ex, lengthPart, -1);

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/ByteArrayLfSerializer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -17,7 +17,11 @@
1717
package org.springframework.integration.ip.tcp.serializer;
1818

1919
/**
20+
* The {@link ByteArraySingleTerminatorSerializer} extension for the {@code LF}
21+
* message delimiter.
22+
*
2023
* @author Gary Russell
24+
*
2125
* @since 2.2
2226
*
2327
*/

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/MapJsonSerializer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -33,11 +33,11 @@
3333

3434
/**
3535
* Serializes a {@link Map} as JSON. Deserializes JSON to
36-
* a {@link Map}. The default {@link org.springframework.integration.support.json.JsonObjectMapperProvider#newInstance()} can be
36+
* a {@link Map}. The default {@link JsonObjectMapperProvider#newInstance()} can be
3737
* overridden using {@link #setJsonObjectMapper(JsonObjectMapper)}.
3838
* <p>
3939
* The JSON deserializer can't delimit multiple JSON
40-
* objects. Therefore another (de)serializer is used to
40+
* objects. Therefore, another (de)serializer is used to
4141
* apply structure to the stream. By default, this is a
4242
* simple {@link ByteArrayLfSerializer}, which inserts/expects
4343
* LF (0x0a) between messages.

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/SoftEndOfStreamException.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.ip.tcp.serializer;
1818

19+
import java.io.Serial;
20+
1921
/**
2022
* Used to communicate that a stream has closed, but between logical
2123
* messages.
@@ -27,6 +29,7 @@
2729
*/
2830
public class SoftEndOfStreamException extends RuntimeException {
2931

32+
@Serial
3033
private static final long serialVersionUID = -2209857413498073058L;
3134

3235
/**

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/TcpDeserializationExceptionEvent.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2024 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.ip.tcp.serializer;
1818

19+
import java.io.Serial;
20+
1921
import org.springframework.integration.ip.event.IpIntegrationEvent;
2022

2123
/**
@@ -29,6 +31,7 @@
2931
*/
3032
public class TcpDeserializationExceptionEvent extends IpIntegrationEvent {
3133

34+
@Serial
3235
private static final long serialVersionUID = 8812537718016054732L;
3336

3437
private final byte[] buffer;

0 commit comments

Comments
 (0)