8000 Use UTF-8 when using getBytes · cloudevents/sdk-java@baeabf6 · GitHub
[go: up one dir, main page]

Skip to content

Commit baeabf6

Browse files
author
Doug Davis
committed
Use UTF-8 when using getBytes
Closes #488 Signed-off-by: Doug Davis <dug@microsoft.com>
1 parent 9132a13 commit baeabf6

File tree

25 files changed

+61
-38
lines changed

25 files changed

+61
-38
lines changed

core/src/test/java/io/cloudevents/core/data/PojoCloudEventDataTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.cloudevents.core.data;
22

3+
import java.nio.charset.StandardCharsets;
4+
35
import org.junit.jupiter.api.Test;
46

57
import static org.assertj.core.api.Assertions.assertThat;
@@ -8,23 +10,23 @@ class PojoCloudEventDataTest {
810

911
@Test
1012
void testWrapAndMemoization() {
11-
PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, i -> i.toString().getBytes());
13+
PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, i -> i.toString().getBytes(StandardCharsets.UTF_8));
1214

1315
assertThat(data.getValue())
1416
.isEqualTo(10);
1517

1618
byte[] firstConversion = data.toBytes();
1719

1820
assertThat(firstConversion)
19-
.isEqualTo("10".getBytes());
21+
.isEqualTo("10".getBytes(StandardCharsets.UTF_8));
2022

2123
assertThat(data.toBytes())
2224
.isSameAs(firstConversion);
2325
}
2426

2527
@Test
2628
void testAlreadySerializedValue() {
27-
byte[] serialized = "10".getBytes();
29+
byte[] serialized = "10".getBytes(StandardCharsets.UTF_8);
2830
PojoCloudEventData<Integer> data = PojoCloudEventData.wrap(10, v -> serialized);
2931

3032
assertThat(data.getValue())

core/src/test/java/io/cloudevents/core/mock/CSVFormat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public byte[] serialize(CloudEvent event) {
5555
event.getData() != null
5656
? new String(Base64.getEncoder().encode(event.getData().toBytes()), StandardCharsets.UTF_8)
5757
: "null"
58-
).getBytes();
58+
).getBytes(StandardCharsets.UTF_8);
5959
}
6060

6161
@Override
@@ -70,7 +70,7 @@ public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper mapper) {
7070
URI dataschema = splitted[5].equals("null") ? null : URI.create(splitted[5]);
7171
String subject = splitted[6].equals("null") ? null : splitted[6];
7272
OffsetDateTime time = splitted[7].equals("null") ? null : Time.parseTime(splitted[7]);
73-
byte[] data = splitted[8].equals("null") ? null : Base64.getDecoder().decode(splitted[8].getBytes());
73+
byte[] data = splitted[8].equals("null") ? null : Base64.getDecoder().decode(splitted[8].getBytes(StandardCharsets.UTF_8));
7474

7575
CloudEventBuilder builder = CloudEventBuilder.fromSpecVersion(sv)
7676
.withId(id)

core/src/test/java/io/cloudevents/core/mock/MyCloudEventData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.cloudevents.core.mock;
22

33
10000 import io.cloudevents.CloudEventData;
4-
4+
import java.nio.charset.StandardCharsets;
55
import java.util.Objects;
66

77
public class MyCloudEventData implements CloudEventData {
@@ -14,7 +14,7 @@ public MyCloudEventData(int value) {
1414

1515
@Override
1616
public byte[] toBytes() {
17-
return Integer.toString(value).getBytes();
17+
return Integer.toString(value).getBytes(StandardCharsets.UTF_8);
1818
}
1919

2020
public int getValue() {

core/src/test/java/io/cloudevents/core/test/Data.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.math.BigDecimal;
2525
import java.net.URI;
26+
import java.nio.charset.StandardCharsets;
2627
import java.time.OffsetDateTime;
2728
import java.util.Objects;
2829
import java.util.stream.Stream;
@@ -39,9 +40,9 @@ public class Data {
3940
public static final String SUBJECT = "sub";
4041
public static final OffsetDateTime TIME = Time.parseTime("2018-04-26T14:48:09+02:00");
4142

42-
public static byte[] DATA_JSON_SERIALIZED = "{}".getBytes();
43-
public static byte[] DATA_XML_SERIALIZED = "<stuff></stuff>".getBytes();
44-
public static byte[] DATA_TEXT_SERIALIZED = "Hello World Lorena!".getBytes();
43+
public static byte[] DATA_JSON_SERIALIZED = "{}".getBytes(StandardCharsets.UTF_8);
44+
public static byte[] DATA_XML_SERIALIZED = "<stuff></stuff>".getBytes(StandardCharsets.UTF_8);
45+
public static byte[] DATA_TEXT_SERIALIZED = "Hello World Lorena!".getBytes(StandardCharsets.UTF_8);
4546
public static byte[] BINARY_VALUE = { (byte) 0xE0, (byte) 0xFF, (byte) 0x00, (byte) 0x44, (byte) 0xAA }; // Base64: 4P8ARKo=
4647

4748
public static final CloudEvent V1_MIN = CloudEventBuilder.v1()

docs/core.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final CloudEvent event = CloudEventBuilder.v1()
3434
.withId("000")
3535
.withType("example.demo")
3636
.withSource(URI.create("http://example.com"))
37-
.withData("text/plain","Hello world!".getBytes())
37+
.withData("text/plain","Hello world!".getBytes("UTF-8"))
3838
.build();
3939
```
4040

examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.io.PrintWriter;
1717
import java.net.URI;
18+
import java.nio.charset.StandardCharsets;
1819

1920
/**
2021
* A example vertx-based AMQP client that interacts with a remote AMQP server to send and receive CloudEvent messages.
@@ -71,7 +72,7 @@ private static void sendMessage() {
7172
.withSource(URI.create("http://127.0.0.1/amqp-client"))
7273
.withType("com.example.sampletype1")
7374
.withTime(Time.parseTime("2020-11-06T21:47:12.037467+00:00"))
74-
.withData(payload.toString().getBytes())
75+
.withData(payload.toString().getBytes(StandardCharsets.UTF_8))
7576
.build();
7677

7778
final Message message = ProtonAmqpMessageFactory.createWriter().writeBinary(event);

examples/amqp-proton/src/main/java/io/cloudevents/examples/amqp/vertx/AmqpServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.PrintWriter;
44
import java.net.URI;
5+
import java.nio.charset.StandardCharsets;
56
import java.time.OffsetDateTime;
67
import java.util.ArrayList;
78
import java.util.Arrays;
@@ -81,7 +82,7 @@ private static void onConnectRequest(final ProtonConnection con) {
8182
.withType("com.example.sampletype1")
8283
.withSource(URI.create("http://127.0.0.1/amqp-server"))
8384
.withTime(OffsetDateTime.now())
84-
.withData("{\"temp\": 5}".getBytes())
85+
.withData("{\"temp\": 5}".getBytes(StandardCharsets.UTF_8))
8586
.build();
8687

8788
final Message message = writer.writeBinary(event);

examples/kafka/src/main/java/io/cloudevents/examples/kafka/SampleProducer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.kafka.common.serialization.StringSerializer;
1313

1414
import java.net.URI;
15+
import java.nio.charset.StandardCharsets;
1516
import java.util.Properties;
1617
import java.util.UUID;
1718

@@ -53,7 +54,7 @@ public static void main(String[] args) {
5354
// Create the event starting from the template
5455
CloudEvent event = eventTemplate.newBuilder()
5556
.withId(id)
56-
.withData("text/plain", data.getBytes())
57+
.withData("text/plain", data.getBytes(StandardCharsets.UTF_8))
5758
.build();
5859

5960
// Send the record

examples/restful-ws-microprofile-liberty/src/main/java/io/cloudevents/examples/microprofile/CloudEventsJaxrsService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CloudEventsJaxrsService {
2020
public CloudEvent retrieveEvent(){
2121
System.out.println("Received request for an event");
2222
return CloudEventBuilder.v1()
23-
.withData("{\"message\":\"Welcome to this Cloudevents + Microprofile example\"}".getBytes())
23+
.withData("{\"message\":\"Welcome to this Cloudevents + Microprofile example\"}".getBytes(StandardCharsets.UTF_8))
2424
.withDataContentType("application/json")
2525
.withId("hello")
2626
.withType("example.http")
@@ -45,7 +45,7 @@ public Response postEvent(CloudEvent event){
4545
@Consumes(MediaType.APPLICATION_JSON)
4646
public CloudEvent echo(CloudEvent event){
4747
return CloudEventBuilder.v1()
48-
.withData("application/json", ("{\"echo\": \"" + new String(event.getData().toBytes(),StandardCharsets.UTF_8) + "\"}").getBytes())
48+
.withData("application/json", ("{\"echo\": \"" + new String(event.getData().toBytes(),StandardCharsets.UTF_8) + "\"}").getBytes(StandardCharsets.UTF_8))
4949
.withId("echo")
5050
.withType("echo.http")
5151
.withSource(URI.create("http://localhost"))

examples/spring-reactive/src/test/java/io/cloudevents/examples/spring/DemoApplicationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.cloudevents.examples.spring;
22

33
import java.net.URI;
4+
import java.nio.charset.StandardCharsets;
45

56
import org.junit.jupiter.api.Test;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -76,7 +77,7 @@ void structuredRequestResponseCloudEventToString() {
7677
.bodyValue(CloudEventBuilder.v1() //
7778
.withId("12345") //
7879
.withType("io.spring.event") //
79-
.withSource(URI.create("https://spring.io/events")).withData("{\"value\":\"Dave\"}".getBytes()) //
80+
.withSource(URI.create("https://spring.io/events")).withData("{\"value\":\"Dave\"}".getBytes(StandardCharsets.UTF_8)) //
8081
.build()) //
8182
.exchange() //
8283
.expectStatus().isOk() //
@@ -102,7 +103,7 @@ void structuredRequestResponseCloudEventToCloudEvent() {
102103
.withId("12345") //
103104
.withType("io.spring.event") //
104105
.withSource(URI.create("https://spring.io/events")) //
105-
.withData("{\"value\":\"Dave\"}".getBytes()) //
106+
.withData("{\"value\":\"Dave\"}".getBytes(StandardCharsets.UTF_8)) //
106107
.build()) //
107108
.exchange() //
108109
.expectStatus().isOk() //

0 commit comments

Comments
 (0)
0