Some simple code & Javadoc clean up for TCP/IP serializers

This commit is contained in:
Artem Bilan
2024-07-03 12:37:37 -04:00
parent 53445fe771
commit 1cea029fab
5 changed files with 29 additions and 19 deletions

View File

@@ -21,18 +21,20 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import org.springframework.util.Assert;
/**
* Reads data in an InputStream to a byte[]; data must be preceded by
* a binary length (network byte order, not included in resulting byte[]).
*
* <p>
* Writes a byte[] to an OutputStream after a binary length.
* The length field contains the length of data following the length
* field. (network byte order).
*
* <p>
* The default length field is a 4 byte signed integer. During deserialization,
* negative values will be rejected.
* Other options are an unsigned byte, and unsigned short.
*
* <p>
* For other header formats, override {@link #readHeader(InputStream)} and
* {@link #writeHeader(OutputStream, int)}.
*
@@ -80,11 +82,10 @@ public class ByteArrayLengthHeaderSerializer extends AbstractByteArraySerializer
* @param headerSize The header size.
*/
public ByteArrayLengthHeaderSerializer(int headerSize) {
if (headerSize != HEADER_SIZE_INT &&
headerSize != HEADER_SIZE_UNSIGNED_BYTE &&
headerSize != HEADER_SIZE_UNSIGNED_SHORT) {
throw new IllegalArgumentException("Illegal header size: " + headerSize);
}
Assert.isTrue(headerSize == HEADER_SIZE_INT
|| headerSize == HEADER_SIZE_UNSIGNED_BYTE
|| headerSize == HEADER_SIZE_UNSIGNED_SHORT,
() -> "Illegal header size: " + headerSize);
this.headerSize = headerSize;
}
@@ -177,6 +178,7 @@ public class ByteArrayLengthHeaderSerializer extends AbstractByteArraySerializer
*/
protected int read(InputStream inputStream, byte[] buffer, boolean header)
throws IOException {
int lengthRead = 0;
int needed = buffer.length;
while (lengthRead < needed) {
@@ -247,9 +249,7 @@ public class ByteArrayLengthHeaderSerializer extends AbstractByteArraySerializer
case HEADER_SIZE_INT:
int messageLength = ByteBuffer.wrap(lengthPart).getInt();
if (messageLength < 0) {
throw new IllegalArgumentException("Length header: "
+ messageLength
+ " is negative");
throw new IllegalArgumentException("Length header: " + messageLength + " is negative");
}
return messageLength;
case HEADER_SIZE_UNSIGNED_BYTE:
@@ -260,8 +260,8 @@ public class ByteArrayLengthHeaderSerializer extends AbstractByteArraySerializer
throw new IllegalArgumentException("Bad header size: " + this.headerSize);
}
}
catch (SoftEndOfStreamException e) { // NOSONAR catch and throw
throw e; // it's an IO exception and we don't want an event for this
catch (SoftEndOfStreamException ex) { // NOSONAR catch and throw
throw ex; // it's an IO exception, and we don't want an event for this
}
catch (IOException | RuntimeException ex) {
publishEvent(ex, lengthPart, -1);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2024 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.
@@ -17,7 +17,11 @@
package org.springframework.integration.ip.tcp.serializer;
/**
* The {@link ByteArraySingleTerminatorSerializer} extension for the {@code LF}
* message delimiter.
*
* @author Gary Russell
*
* @since 2.2
*
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2024 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.
@@ -33,11 +33,11 @@ import org.springframework.util.Assert;
/**
* Serializes a {@link Map} as JSON. Deserializes JSON to
* a {@link Map}. The default {@link org.springframework.integration.support.json.JsonObjectMapperProvider#newInstance()} can be
* a {@link Map}. The default {@link JsonObjectMapperProvider#newInstance()} can be
* overridden using {@link #setJsonObjectMapper(JsonObjectMapper)}.
* <p>
* The JSON deserializer can't delimit multiple JSON
* objects. Therefore another (de)serializer is used to
* objects. Therefore, another (de)serializer is used to
* apply structure to the stream. By default, this is a
* simple {@link ByteArrayLfSerializer}, which inserts/expects
* LF (0x0a) between messages.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2024 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.
@@ -16,6 +16,8 @@
package org.springframework.integration.ip.tcp.serializer;
import java.io.Serial;
/**
* Used to communicate that a stream has closed, but between logical
* messages.
@@ -27,6 +29,7 @@ package org.springframework.integration.ip.tcp.serializer;
*/
public class SoftEndOfStreamException extends RuntimeException {
@Serial
private static final long serialVersionUID = -2209857413498073058L;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2024 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.
@@ -16,6 +16,8 @@
package org.springframework.integration.ip.tcp.serializer;
import java.io.Serial;
import org.springframework.integration.ip.event.IpIntegrationEvent;
/**
@@ -29,6 +31,7 @@ import org.springframework.integration.ip.event.IpIntegrationEvent;
*/
public class TcpDeserializationExceptionEvent extends IpIntegrationEvent {
@Serial
private static final long serialVersionUID = 8812537718016054732L;
private final byte[] buffer;