Skip to content

Commit 12ef5cf

Browse files
garyrussellartembilan
authored andcommitted
TCP Serializer/Deserializer Factory Class
To simplify Java config and in preparation for DSL support. Polishing; rename class; lengthen method names
1 parent 2aadf5e commit 12ef5cf

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
*/
3131
public class ByteArrayCrLfSerializer extends AbstractPooledBufferByteArraySerializer {
3232

33+
/**
34+
* A single reusable instance.
35+
*/
36+
public static final ByteArrayCrLfSerializer INSTANCE = new ByteArrayCrLfSerializer();
37+
3338
private static final byte[] CRLF = "\r\n".getBytes();
3439

3540
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
*/
2424
public class ByteArrayLfSerializer extends ByteArraySingleTerminatorSerializer {
2525

26+
/**
27+
* A single reusable instance.
28+
*/
29+
public static final ByteArrayLfSerializer INSTANCE = new ByteArrayLfSerializer();
30+
2631
public ByteArrayLfSerializer() {
2732
super((byte) 0x0a);
2833
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
*/
4444
public class ByteArrayRawSerializer extends AbstractPooledBufferByteArraySerializer {
4545

46+
/**
47+
* A single reusable instance that does not treat timeouts as end of message.
48+
*/
49+
public static final ByteArrayRawSerializer INSTANCE = new ByteArrayRawSerializer();
50+
4651
private final boolean treatTimeoutAsEndOfMessage;
4752

4853
public ByteArrayRawSerializer() {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
*/
3333
public class ByteArrayStxEtxSerializer extends AbstractPooledBufferByteArraySerializer {
3434

35+
/**
36+
* A single reusable instance.
37+
*/
38+
public static final ByteArrayStxEtxSerializer INSTANCE = new ByteArrayStxEtxSerializer();
39+
3540
public static final int STX = 0x02;
3641

3742
public static final int ETX = 0x03;
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.ip.tcp.serializer;
18+
19+
/**
20+
* Factory class to create TCP Serializer/Deserializers used to
21+
* encode/decode messages to/from a TCP stream.
22+
* This is used to simplify configuration with Java, such as
23+
*
24+
* <pre class="code">
25+
* TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(1234);
26+
* server.setSerializer(TcpCodecs.lf());
27+
* server.setDserializer(TcpCodecs.lf());
28+
* ...
29+
* </pre>
30+
*
31+
* @author Gary Russell
32+
* @since 5.0
33+
*
34+
*/
35+
public final class TcpCodecs {
36+
37+
private static ByteArrayLengthHeaderSerializer oneByteLHS;
38+
39+
private static ByteArrayLengthHeaderSerializer twoByteLHS;
40+
41+
private static ByteArrayLengthHeaderSerializer fourByteLHS;
42+
43+
private TcpCodecs() {
44+
super();
45+
}
46+
47+
/**
48+
* @return a {@link ByteArrayCrLfSerializer}.
49+
*/
50+
public static ByteArrayCrLfSerializer crlf() {
51+
return ByteArrayCrLfSerializer.INSTANCE;
52+
}
53+
54+
/**
55+
* @return a {@link ByteArrayLfSerializer}.
56+
*/
57+
public static ByteArrayLfSerializer lf() {
58+
return ByteArrayLfSerializer.INSTANCE;
59+
}
60+
61+
/**
62+
* @return a {@link ByteArrayRawSerializer}.
63+
*/
64+
public static ByteArrayRawSerializer raw() {
65+
return ByteArrayRawSerializer.INSTANCE;
66+
}
67+
68+
/**
69+
* @return a {@link ByteArrayStxEtxSerializer}.
70+
*/
71+
public static ByteArrayStxEtxSerializer stxetx() {
72+
return ByteArrayStxEtxSerializer.INSTANCE;
73+
}
74+
75+
/**
76+
* @param terminator the terminator indicating message end.
77+
* @return a {@link ByteArraySingleTerminatorSerializer} using the supplied
78+
* terminator.
79+
*/
80+
public static ByteArraySingleTerminatorSerializer singleTerminator(byte terminator) {
81+
return new ByteArraySingleTerminatorSerializer(terminator);
82+
}
83+
84+
/**
85+
* @return a {@link ByteArrayLengthHeaderSerializer} with a 1 byte header.
86+
*/
87+
public static ByteArrayLengthHeaderSerializer lengthHeader1() {
88+
if (oneByteLHS == null) {
89+
oneByteLHS = new ByteArrayLengthHeaderSerializer(1);
90+
}
91+
return oneByteLHS;
92+
}
93+
94+
/**
95+
* @return a {@link ByteArrayLengthHeaderSerializer} with a 2 byte header.
96+
*/
97+
public static ByteArrayLengthHeaderSerializer lengthHeader2() {
98+
if (twoByteLHS == null) {
99+
twoByteLHS = new ByteArrayLengthHeaderSerializer(2);
100+
}
101+
return twoByteLHS;
102+
}
103+
104+
/**
105+
* @return a {@link ByteArrayLengthHeaderSerializer} with a 4 byte header.
106+
*/
107+
public static ByteArrayLengthHeaderSerializer lengthHeader4() {
108+
if (fourByteLHS == null) {
109+
fourByteLHS = new ByteArrayLengthHeaderSerializer(4);
110+
}
111+
return fourByteLHS;
112+
}
113+
114+
/**
115+
* @param bytes header length.
116+
* @return a {@link ByteArrayLengthHeaderSerializer} with a 1, 2 or 4 byte header.
117+
*/
118+
public static ByteArrayLengthHeaderSerializer lengthHeader(int bytes) {
119+
switch (bytes) {
120+
case 1:
121+
return lengthHeader1();
122+
case 2:
123+
return lengthHeader2();
124+
case 4:
125+
return lengthHeader4();
126+
default:
127+
throw new IllegalArgumentException("Only 1, 2 or 4 byte headers are supported");
128+
}
129+
}
130+
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.ip.tcp.serializer;
18+
19+
import static org.hamcrest.Matchers.instanceOf;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertThat;
22+
23+
import org.junit.Test;
24+
25+
import org.springframework.integration.test.util.TestUtils;
26+
27+
/**
28+
* @author Gary Russell
29+
* @since 5.0
30+
*
31+
*/
32+
public class TcpCodecsTests {
33+
34+
@Test
35+
public void testAll() {
36+
AbstractByteArraySerializer codec = TcpCodecs.crlf();
37+
assertThat(codec, instanceOf(ByteArrayCrLfSerializer.class));
38+
codec = TcpCodecs.lf();
39+
assertThat(codec, instanceOf(ByteArrayLfSerializer.class));
40+
codec = TcpCodecs.raw();
41+
assertThat(codec, instanceOf(ByteArrayRawSerializer.class));
42+
codec = TcpCodecs.stxetx();
43+
assertThat(codec, instanceOf(ByteArrayStxEtxSerializer.class));
44+
codec = TcpCodecs.singleTerminator((byte) 23);
45+
assertThat(codec, instanceOf(ByteArraySingleTerminatorSerializer.class));
46+
assertEquals((byte) 23, TestUtils.getPropertyValue(codec, "terminator"));
47+
codec = TcpCodecs.lengthHeader1();
48+
assertThat(codec, instanceOf(ByteArrayLengthHeaderSerializer.class));
49+
assertEquals(1, TestUtils.getPropertyValue(codec, "headerSize"));
50+
codec = TcpCodecs.lengthHeader2();
51+
assertThat(codec, instanceOf(ByteArrayLengthHeaderSerializer.class));
52+
assertEquals(2, TestUtils.getPropertyValue(codec, "headerSize"));
53+
codec = TcpCodecs.lengthHeader4();
54+
assertThat(codec, instanceOf(ByteArrayLengthHeaderSerializer.class));
55+
assertEquals(4, TestUtils.getPropertyValue(codec, "headerSize"));
56+
codec = TcpCodecs.lengthHeader(1);
57+
assertThat(codec, instanceOf(ByteArrayLengthHeaderSerializer.class));
58+
assertEquals(1, TestUtils.getPropertyValue(codec, "headerSize"));
59+
codec = TcpCodecs.lengthHeader(2);
60+
assertThat(codec, instanceOf(ByteArrayLengthHeaderSerializer.class));
61+
assertEquals(2, TestUtils.getPropertyValue(codec, "headerSize"));
62+
codec = TcpCodecs.lengthHeader(4);
63+
assertThat(codec, instanceOf(ByteArrayLengthHeaderSerializer.class));
64+
assertEquals(4, TestUtils.getPropertyValue(codec, "headerSize"));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)