TextMessage.toString() does not throw StringIndexOutOfBoundsException for payload with multibyte characters
Issue: SPR-12307
This commit is contained in:
parent
aaf69eb1f1
commit
da14aeea7a
|
@ -69,10 +69,6 @@ public abstract class AbstractWebSocketMessage<T> implements WebSocketMessage<T>
|
||||||
return this.last;
|
return this.last;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return AbstractWebSocketMessage.class.hashCode() * 13 + ObjectUtils.nullSafeHashCode(this.payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object other) {
|
public boolean equals(Object other) {
|
||||||
|
@ -86,10 +82,15 @@ public abstract class AbstractWebSocketMessage<T> implements WebSocketMessage<T>
|
||||||
return ObjectUtils.nullSafeEquals(this.payload, otherMessage.payload);
|
return ObjectUtils.nullSafeEquals(this.payload, otherMessage.payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return ObjectUtils.nullSafeHashCode(this.payload);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return getClass().getSimpleName() + " payload= " + toStringPayload()
|
return getClass().getSimpleName() + " payload=[" + toStringPayload() +
|
||||||
+ ", byteCount=" + getPayloadLength() + ", last=" + isLast() + "]";
|
"], byteCount=" + getPayloadLength() + ", last=" + isLast() + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract String toStringPayload();
|
protected abstract String toStringPayload();
|
||||||
|
|
|
@ -26,7 +26,6 @@ import java.nio.ByteBuffer;
|
||||||
*/
|
*/
|
||||||
public final class BinaryMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
public final class BinaryMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new binary WebSocket message with the given ByteBuffer payload.
|
* Create a new binary WebSocket message with the given ByteBuffer payload.
|
||||||
* @param payload the non-null payload
|
* @param payload the non-null payload
|
||||||
|
@ -78,7 +77,7 @@ public final class BinaryMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
||||||
* @param isLast if the message is the last of a series of partial messages
|
* @param isLast if the message is the last of a series of partial messages
|
||||||
*/
|
*/
|
||||||
public BinaryMessage(byte[] payload, int offset, int length, boolean isLast) {
|
public BinaryMessage(byte[] payload, int offset, int length, boolean isLast) {
|
||||||
super((payload != null) ? ByteBuffer.wrap(payload, offset, length) : null, isLast);
|
super(payload != null ? ByteBuffer.wrap(payload, offset, length) : null, isLast);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,6 @@ import java.nio.ByteBuffer;
|
||||||
*/
|
*/
|
||||||
public final class PingMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
public final class PingMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new ping message with an empty payload.
|
* Create a new ping message with an empty payload.
|
||||||
*/
|
*/
|
||||||
|
@ -45,12 +44,12 @@ public final class PingMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getPayloadLength() {
|
public int getPayloadLength() {
|
||||||
return (getPayload() != null) ? getPayload().remaining() : 0;
|
return (getPayload() != null ? getPayload().remaining() : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String toStringPayload() {
|
protected String toStringPayload() {
|
||||||
return (getPayload() != null) ? getPayload().toString() : null;
|
return (getPayload() != null ? getPayload().toString() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@ import java.nio.ByteBuffer;
|
||||||
*/
|
*/
|
||||||
public final class PongMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
public final class PongMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new pong message with an empty payload.
|
* Create a new pong message with an empty payload.
|
||||||
*/
|
*/
|
||||||
|
@ -45,12 +44,12 @@ public final class PongMessage extends AbstractWebSocketMessage<ByteBuffer> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getPayloadLength() {
|
public int getPayloadLength() {
|
||||||
return (getPayload() != null) ? getPayload().remaining() : 0;
|
return (getPayload() != null ? getPayload().remaining() : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String toStringPayload() {
|
protected String toStringPayload() {
|
||||||
return (getPayload() != null) ? getPayload().toString() : null;
|
return (getPayload() != null ? getPayload().toString() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -41,9 +41,8 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new text WebSocket message from the given byte[]. It is assumed the
|
* Create a new text WebSocket message from the given byte[]. It is assumed
|
||||||
* byte array can be encoded into an UTF-8 String.
|
* the byte array can be encoded into an UTF-8 String.
|
||||||
*
|
|
||||||
* @param payload the non-null payload
|
* @param payload the non-null payload
|
||||||
*/
|
*/
|
||||||
public TextMessage(byte[] payload) {
|
public TextMessage(byte[] payload) {
|
||||||
|
@ -76,7 +75,8 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String toStringPayload() {
|
protected String toStringPayload() {
|
||||||
return (getPayloadLength() > 10) ? getPayload().substring(0, 10) + ".." : getPayload();
|
String payload = getPayload();
|
||||||
|
return (payload.length() > 10 ? payload.substring(0, 10) + ".." : payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2014 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.web.socket;
|
||||||
|
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test fixture for {@link TextMessage}.
|
||||||
|
*
|
||||||
|
* @author Shinobu Aoki
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
*/
|
||||||
|
public class TextMessageTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toStringWithAscii() {
|
||||||
|
String expected = "foo,bar";
|
||||||
|
TextMessage actual = new TextMessage(expected);
|
||||||
|
assertThat(actual.getPayload(), Matchers.is(expected));
|
||||||
|
assertThat(actual.toString(), Matchers.containsString(expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toStringWithMultibyteString() {
|
||||||
|
String expected = "\u3042\u3044\u3046\u3048\u304a";
|
||||||
|
TextMessage actual = new TextMessage(expected);
|
||||||
|
assertThat(actual.getPayload(), Matchers.is(expected));
|
||||||
|
assertThat(actual.toString(), Matchers.containsString(expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue