Skip to content

Commit ae3bb15

Browse files
garyrussellartembilan
authored andcommitted
TcpCodecs factory improvement
- add convenience factory methods to set the max message size while creating the codec * Polishing - PR comments; deprecate protected field; checkstyle fixes.
1 parent 60be204 commit ae3bb15

File tree

9 files changed

+181
-22
lines changed

9 files changed

+181
-22
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -39,9 +39,16 @@ public abstract class AbstractByteArraySerializer implements
3939
Deserializer<byte[]>,
4040
ApplicationEventPublisherAware {
4141

42-
protected int maxMessageSize = 2048;
42+
/**
43+
* The default maximum message size when deserializing.
44+
* @since 5.1.3
45+
*/
46+
public static final int DEFAULT_MAX_MESSAGE_SIZE = 2048;
47+
48+
@Deprecated
49+
protected int maxMessageSize = DEFAULT_MAX_MESSAGE_SIZE; // NOSONAR - TODO private in 5.2, use getter
4350

44-
protected final Log logger = LogFactory.getLog(this.getClass());
51+
protected final Log logger = LogFactory.getLog(this.getClass()); // NOSONAR
4552

4653
private ApplicationEventPublisher applicationEventPublisher;
4754

@@ -50,6 +57,7 @@ public abstract class AbstractByteArraySerializer implements
5057
* Default 2048.
5158
* @return The max message size.
5259
*/
60+
@SuppressWarnings("deprecation")
5361
public int getMaxMessageSize() {
5462
return this.maxMessageSize;
5563
}
@@ -59,6 +67,7 @@ public int getMaxMessageSize() {
5967
* Default 2048.
6068
* @param maxMessageSize The max message size.
6169
*/
70+
@SuppressWarnings("deprecation")
6271
public void setMaxMessageSize(int maxMessageSize) {
6372
this.maxMessageSize = maxMessageSize;
6473
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -77,7 +77,7 @@ public void setPoolWaitTimeout(long poolWaitTimeout) {
7777

7878
@Override
7979
public final byte[] deserialize(InputStream inputStream) throws IOException {
80-
byte[] buffer = this.pool == null ? new byte[this.maxMessageSize] : this.pool.getItem();
80+
byte[] buffer = this.pool == null ? new byte[getMaxMessageSize()] : this.pool.getItem();
8181
try {
8282
return doDeserialize(inputStream, buffer);
8383
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -66,8 +66,8 @@ public int fillToCrLf(InputStream inputStream, byte[] buffer) throws IOException
6666
break;
6767
}
6868
buffer[n++] = (byte) bite;
69-
if (n >= this.maxMessageSize) {
70-
throw new IOException("CRLF not found before max message length: " + this.maxMessageSize);
69+
if (n >= getMaxMessageSize()) {
70+
throw new IOException("CRLF not found before max message length: " + getMaxMessageSize());
7171
}
7272
}
7373
return n - 1; // trim \r

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -105,9 +105,9 @@ public byte[] deserialize(InputStream inputStream) throws IOException {
105105
}
106106
byte[] messagePart = null;
107107
try {
108-
if (messageLength > this.maxMessageSize) {
108+
if (messageLength > getMaxMessageSize()) {
109109
throw new IOException("Message length " + messageLength +
110-
" exceeds max message length: " + this.maxMessageSize);
110+
" exceeds max message length: " + getMaxMessageSize());
111111
}
112112
messagePart = new byte[messageLength];
113113
read(inputStream, messagePart, false);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -94,9 +94,9 @@ protected byte[] doDeserialize(InputStream inputStream, byte[] buffer) throws IO
9494
}
9595
break;
9696
}
97-
if (n >= this.maxMessageSize) {
97+
if (n >= getMaxMessageSize()) {
9898
throw new IOException("Socket was not closed before max message length: "
99-
+ this.maxMessageSize);
99+
+ getMaxMessageSize());
100100
}
101101
buffer[n++] = (byte) bite;
102102
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -60,10 +60,10 @@ protected byte[] doDeserialize(InputStream inputStream, byte[] buffer) throws IO
6060
break;
6161
}
6262
buffer[n++] = (byte) bite;
63-
if (n >= this.maxMessageSize) {
63+
if (n >= getMaxMessageSize()) {
6464
throw new IOException("Terminator '0x" + Integer.toHexString(this.terminator & 0xff)
6565
+ "' not found before max message length: "
66-
+ this.maxMessageSize);
66+
+ getMaxMessageSize());
6767
}
6868
}
6969
return copyToSizedArray(buffer, n);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -63,9 +63,9 @@ public byte[] doDeserialize(InputStream inputStream, byte[] buffer) throws IOExc
6363
while ((bite = inputStream.read()) != ETX) {
6464
checkClosure(bite);
6565
buffer[n++] = (byte) bite;
66-
if (n >= this.maxMessageSize) {
66+
if (n >= getMaxMessageSize()) {
6767
throw new IOException("ETX not found before max message length: "
68-
+ this.maxMessageSize);
68+
+ getMaxMessageSize());
6969
}
7070
}
7171
return copyToSizedArray(buffer, n);

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

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -45,44 +45,57 @@ private TcpCodecs() {
4545
}
4646

4747
/**
48+
* Return a serializer with the default max message size for deserialization.
4849
* @return a {@link ByteArrayCrLfSerializer}.
50+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
4951
*/
5052
public static ByteArrayCrLfSerializer crlf() {
5153
return ByteArrayCrLfSerializer.INSTANCE;
5254
}
5355

5456
/**
57+
* Return a serializer with the default max message size for deserialization.
58+
* {@value AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE}.
5559
* @return a {@link ByteArrayLfSerializer}.
60+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
5661
*/
5762
public static ByteArrayLfSerializer lf() {
5863
return ByteArrayLfSerializer.INSTANCE;
5964
}
6065

6166
/**
67+
* Return a serializer with the default max message size for deserialization.
6268
* @return a {@link ByteArrayRawSerializer}.
69+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
6370
*/
6471
public static ByteArrayRawSerializer raw() {
6572
return ByteArrayRawSerializer.INSTANCE;
6673
}
6774

6875
/**
76+
* Return a serializer with the default max message size for deserialization.
6977
* @return a {@link ByteArrayStxEtxSerializer}.
78+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
7079
*/
7180
public static ByteArrayStxEtxSerializer stxetx() {
7281
return ByteArrayStxEtxSerializer.INSTANCE;
7382
}
7483

7584
/**
85+
* Return a serializer with the default max message size for deserialization.
7686
* @param terminator the terminator indicating message end.
7787
* @return a {@link ByteArraySingleTerminatorSerializer} using the supplied
7888
* terminator.
89+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
7990
*/
8091
public static ByteArraySingleTerminatorSerializer singleTerminator(byte terminator) {
8192
return new ByteArraySingleTerminatorSerializer(terminator);
8293
}
8394

8495
/**
96+
* Return a serializer with the default max message size for deserialization.
8597
* @return a {@link ByteArrayLengthHeaderSerializer} with a 1 byte header.
98+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
8699
*/
87100
public static ByteArrayLengthHeaderSerializer lengthHeader1() {
88101
if (oneByteLHS == null) {
@@ -92,7 +105,9 @@ public static ByteArrayLengthHeaderSerializer lengthHeader1() {
92105
}
93106

94107
/**
108+
* Return a serializer with the default max message size for deserialization.
95109
* @return a {@link ByteArrayLengthHeaderSerializer} with a 2 byte header.
110+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
96111
*/
97112
public static ByteArrayLengthHeaderSerializer lengthHeader2() {
98113
if (twoByteLHS == null) {
@@ -102,7 +117,9 @@ public static ByteArrayLengthHeaderSerializer lengthHeader2() {
102117
}
103118

104119
/**
120+
* Return a serializer with the default max message size for deserialization.
105121
* @return a {@link ByteArrayLengthHeaderSerializer} with a 4 byte header.
122+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
106123
*/
107124
public static ByteArrayLengthHeaderSerializer lengthHeader4() {
108125
if (fourByteLHS == null) {
@@ -112,8 +129,10 @@ public static ByteArrayLengthHeaderSerializer lengthHeader4() {
112129
}
113130

114131
/**
132+
* Return a serializer with the default max message size for deserialization.
115133
* @param bytes header length.
116134
* @return a {@link ByteArrayLengthHeaderSerializer} with a 1, 2 or 4 byte header.
135+
* @see AbstractByteArraySerializer#DEFAULT_MAX_MESSAGE_SIZE
117136
*/
118137
public static ByteArrayLengthHeaderSerializer lengthHeader(int bytes) {
119138
switch (bytes) {
@@ -128,4 +147,102 @@ public static ByteArrayLengthHeaderSerializer lengthHeader(int bytes) {
128147
}
129148
}
130149

150+
/**
151+
* Return a serializer with the provided max message size for deserialization.
152+
* @param maxMessageSize the max message size.
153+
* @return a {@link ByteArrayCrLfSerializer}.
154+
* @since 5.1.3
155+
*/
156+
public static ByteArrayCrLfSerializer crlf(int maxMessageSize) {
157+
ByteArrayCrLfSerializer codec = new ByteArrayCrLfSerializer();
158+
codec.setMaxMessageSize(maxMessageSize);
159+
return codec;
160+
}
161+
162+
/**
163+
* Return a serializer with the provided max message size for deserialization.
164+
* @param maxMessageSize the max message size.
165+
* @return a {@link ByteArrayLfSerializer}.
166+
* @since 5.1.3
167+
*/
168+
public static ByteArrayLfSerializer lf(int maxMessageSize) {
169+
ByteArrayLfSerializer codec = new ByteArrayLfSerializer();
170+
codec.setMaxMessageSize(maxMessageSize);
171+
return codec;
172+
}
173+
174+
/**
175+
* Return a serializer with the provided max message size for deserialization.
176+
* @param maxMessageSize the max message size.
177+
* @return a {@link ByteArrayRawSerializer}.
178+
* @since 5.1.3
179+
*/
180+
public static ByteArrayRawSerializer raw(int maxMessageSize) {
181+
ByteArrayRawSerializer codec = new ByteArrayRawSerializer();
182+
codec.setMaxMessageSize(maxMessageSize);
183+
return codec;
184+
}
185+
186+
/**
187+
* Return a serializer with the provided max message size for deserialization.
188+
* @param maxMessageSize the max message size.
189+
* @return a {@link ByteArrayStxEtxSerializer}.
190+
* @since 5.1.3
191+
*/
192+
public static ByteArrayStxEtxSerializer stxetx(int maxMessageSize) {
193+
ByteArrayStxEtxSerializer codec = new ByteArrayStxEtxSerializer();
194+
codec.setMaxMessageSize(maxMessageSize);
195+
return codec;
196+
}
197+
198+
/**
199+
* Return a serializer with the provided max message size for deserialization.
200+
* @param terminator the terminator indicating message end.
201+
* @param maxMessageSize the max message size.
202+
* @return a {@link ByteArraySingleTerminatorSerializer} using the supplied
203+
* terminator.
204+
* @since 5.1.3
205+
*/
206+
public static ByteArraySingleTerminatorSerializer singleTerminator(byte terminator, int maxMessageSize) {
207+
ByteArraySingleTerminatorSerializer codec = new ByteArraySingleTerminatorSerializer(terminator);
208+
codec.setMaxMessageSize(maxMessageSize);
209+
return codec;
210+
}
211+
212+
/**
213+
* Return a serializer with the provided max message size for deserialization.
214+
* @param maxMessageSize the max message size.
215+
* @return a {@link ByteArrayLengthHeaderSerializer} with a 1 byte header.
216+
* @since 5.1.3
217+
*/
218+
public static ByteArrayLengthHeaderSerializer lengthHeader1(int maxMessageSize) {
219+
ByteArrayLengthHeaderSerializer codec = new ByteArrayLengthHeaderSerializer(1);
220+
codec.setMaxMessageSize(maxMessageSize);
221+
return codec;
222+
}
223+
224+
/**
225+
* Return a serializer with the provided max message size for deserialization.
226+
* @param maxMessageSize the max message size.
227+
* @return a {@link ByteArrayLengthHeaderSerializer} with a 2 byte header.
228+
* @since 5.1.3
229+
*/
230+
public static ByteArrayLengthHeaderSerializer lengthHeader2(int maxMessageSize) {
231+
ByteArrayLengthHeaderSerializer codec = new ByteArrayLengthHeaderSerializer(2);
232+
codec.setMaxMessageSize(maxMessageSize);
233+
return codec;
234+
}
235+
236+
/**
237+
* Return a serializer with the provided max message size for deserialization.
238+
* @param maxMessageSize the max message size.
239+
* @return a {@link ByteArrayLengthHeaderSerializer} with a 4 byte header.
240+
* @since 5.1.3
241+
*/
242+
public static ByteArrayLengthHeaderSerializer lengthHeader4(int maxMessageSize) {
243+
ByteArrayLengthHeaderSerializer codec = new ByteArrayLengthHeaderSerializer(4);
244+
codec.setMaxMessageSize(maxMessageSize);
245+
return codec;
246+
}
247+
131248
}

0 commit comments

Comments
 (0)