Skip to content

Commit b27fbd0

Browse files
committed
DATAREDIS-1212 Add helper methods to Message to avoid allocation in DefaultMessage
1 parent 247424a commit b27fbd0

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/main/java/org/springframework/data/redis/connection/DefaultMessage.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.connection;
1717

1818
import org.springframework.data.redis.connection.util.ByteArrayWrapper;
19+
import org.springframework.data.redis.util.ByteUtils;
1920
import org.springframework.lang.Nullable;
2021
import org.springframework.util.Assert;
2122
import org.springframework.util.ObjectUtils;
@@ -58,12 +59,12 @@ public boolean hasBody() {
5859
return !ObjectUtils.isEmpty(body);
5960
}
6061

61-
public String getChannelAsString() {
62-
return new String(channel);
62+
public boolean channelStartsWith(byte[] prefix) {
63+
return ByteUtils.startsWith(channel, prefix);
6364
}
6465

65-
public String getBodyAsString() {
66-
return new String(body);
66+
public boolean bodyStartsWith(byte[] prefix) {
67+
return ByteUtils.startsWith(body, prefix);
6768
}
6869

6970
public ByteArrayWrapper getChannelAsWrapper() {

src/main/java/org/springframework/data/redis/connection/Message.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.Serializable;
1919

2020
import org.springframework.data.redis.connection.util.ByteArrayWrapper;
21+
import org.springframework.data.redis.util.ByteUtils;
2122
import org.springframework.util.ObjectUtils;
2223

2324
/**
@@ -62,21 +63,21 @@ default boolean hasBody() {
6263
}
6364

6465
/**
65-
* Returns the string representation of the channel associated with the message.
66+
* Checks if the message channel starts with the given prefix
6667
*
67-
* @return message channel as string. Never {@literal null}.
68+
* @return {@code true} if the channel starts with the given prefix, otherwise {@code false}
6869
*/
69-
default String getChannelAsString() {
70-
return new String(getChannel());
70+
default boolean channelStartsWith(byte[] prefix) {
71+
return ByteUtils.startsWith(getChannel(), prefix);
7172
}
7273

7374
/**
74-
* Returns the string representation of the body (or the payload) of the message
75+
* Checks if the message body starts with the given prefix
7576
*
76-
* @return body as string. Never {@literal null}.
77+
* @return {@code true} if the body starts with the given prefix, otherwise {@code false}
7778
*/
78-
default String getBodyAsString() {
79-
return new String(getBody());
79+
default boolean bodyStartsWith(byte[] prefix) {
80+
return ByteUtils.startsWith(getBody(), prefix);
8081
}
8182

8283
/**

src/test/java/org/springframework/data/redis/connection/DefaultMessageTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ void testHasBody() {
4444
}
4545

4646
@Test
47-
void testGetChannelAsString() {
48-
assertThat(aMessageWithChannel(EMPTY_BYTES).getChannelAsString()).isEmpty();
49-
assertThat(aMessageWithChannel(CHANNEL_BYTES).getChannelAsString()).isEqualTo(CHANNEL);
47+
void testChannelStartsWith() {
48+
assertThat(aMessageWithChannel(EMPTY_BYTES).channelStartsWith(CHANNEL_BYTES)).isFalse();
49+
assertThat(aMessageWithChannel(CHANNEL_BYTES).channelStartsWith(CHANNEL_BYTES)).isTrue();
5050
}
5151

5252
@Test
53-
void testGetBodyAsString() {
54-
assertThat(aMessageWithBody(EMPTY_BYTES).getBodyAsString()).isEmpty();
55-
assertThat(aMessageWithBody(BODY_BYTES).getBodyAsString()).isEqualTo(BODY);
53+
void testBodyStartsWith() {
54+
assertThat(aMessageWithBody(EMPTY_BYTES).bodyStartsWith(BODY_BYTES)).isFalse();
55+
assertThat(aMessageWithBody(BODY_BYTES).bodyStartsWith(BODY_BYTES)).isTrue();
5656
}
5757

5858
@Test

0 commit comments

Comments
 (0)