Skip to content

Commit

Permalink
DATAREDIS-1212 Add helper methods to Message to avoid allocation in…
Browse files Browse the repository at this point in the history
… `DefaultMessage`
  • Loading branch information
theigl committed Sep 28, 2020
1 parent 247424a commit b27fbd0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.redis.connection;

import org.springframework.data.redis.connection.util.ByteArrayWrapper;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -58,12 +59,12 @@ public boolean hasBody() {
return !ObjectUtils.isEmpty(body);
}

public String getChannelAsString() {
return new String(channel);
public boolean channelStartsWith(byte[] prefix) {
return ByteUtils.startsWith(channel, prefix);
}

public String getBodyAsString() {
return new String(body);
public boolean bodyStartsWith(byte[] prefix) {
return ByteUtils.startsWith(body, prefix);
}

public ByteArrayWrapper getChannelAsWrapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.Serializable;

import org.springframework.data.redis.connection.util.ByteArrayWrapper;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.util.ObjectUtils;

/**
Expand Down Expand Up @@ -62,21 +63,21 @@ default boolean hasBody() {
}

/**
* Returns the string representation of the channel associated with the message.
* Checks if the message channel starts with the given prefix
*
* @return message channel as string. Never {@literal null}.
* @return {@code true} if the channel starts with the given prefix, otherwise {@code false}
*/
default String getChannelAsString() {
return new String(getChannel());
default boolean channelStartsWith(byte[] prefix) {
return ByteUtils.startsWith(getChannel(), prefix);
}

/**
* Returns the string representation of the body (or the payload) of the message
* Checks if the message body starts with the given prefix
*
* @return body as string. Never {@literal null}.
* @return {@code true} if the body starts with the given prefix, otherwise {@code false}
*/
default String getBodyAsString() {
return new String(getBody());
default boolean bodyStartsWith(byte[] prefix) {
return ByteUtils.startsWith(getBody(), prefix);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ void testHasBody() {
}

@Test
void testGetChannelAsString() {
assertThat(aMessageWithChannel(EMPTY_BYTES).getChannelAsString()).isEmpty();
assertThat(aMessageWithChannel(CHANNEL_BYTES).getChannelAsString()).isEqualTo(CHANNEL);
void testChannelStartsWith() {
assertThat(aMessageWithChannel(EMPTY_BYTES).channelStartsWith(CHANNEL_BYTES)).isFalse();
assertThat(aMessageWithChannel(CHANNEL_BYTES).channelStartsWith(CHANNEL_BYTES)).isTrue();
}

@Test
void testGetBodyAsString() {
assertThat(aMessageWithBody(EMPTY_BYTES).getBodyAsString()).isEmpty();
assertThat(aMessageWithBody(BODY_BYTES).getBodyAsString()).isEqualTo(BODY);
void testBodyStartsWith() {
assertThat(aMessageWithBody(EMPTY_BYTES).bodyStartsWith(BODY_BYTES)).isFalse();
assertThat(aMessageWithBody(BODY_BYTES).bodyStartsWith(BODY_BYTES)).isTrue();
}

@Test
Expand Down

0 comments on commit b27fbd0

Please sign in to comment.