Skip to content

Commit d961e4b

Browse files
authored
Exposing BinaryData in EventData (Azure#18839)
* Cherry-picking BinaryData changes. * Update CHANGELOG with GA BinaryData comment.
1 parent d080d6a commit d961e4b

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

sdk/eventhubs/azure-messaging-eventhubs/CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Release History
22

33
## 5.5.0-beta.1 (Unreleased)
4-
4+
- Use `BinaryData` in `EventData`.
55

66
## 5.4.0 (2021-01-14)
77
### New features
@@ -15,8 +15,8 @@
1515

1616
## 5.4.0-beta.1 (2020-11-12)
1717
### Breaking changes
18-
- Removed `ObjectBatch` and related `createBatch()` and `send()` operations in favor of
19-
supporting `BinaryData` in `EventData`.
18+
- Removed `ObjectBatch` and related `createBatch()` and `send()` operations in favor of supporting `BinaryData` in
19+
`EventData`.
2020

2121
## 5.3.1 (2020-10-30)
2222
### Bug fixes

sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventData.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.messaging.eventhubs;
55

6+
import com.azure.core.util.BinaryData;
67
import com.azure.core.util.Context;
78

89
import java.nio.ByteBuffer;
@@ -53,7 +54,7 @@ public class EventData {
5354
static final Set<String> RESERVED_SYSTEM_PROPERTIES;
5455

5556
private final Map<String, Object> properties;
56-
private final byte[] body;
57+
private final BinaryData body;
5758
private final SystemProperties systemProperties;
5859
private Context context;
5960

@@ -75,10 +76,7 @@ public class EventData {
7576
* @throws NullPointerException if {@code body} is {@code null}.
7677
*/
7778
public EventData(byte[] body) {
78-
this.body = Objects.requireNonNull(body, "'body' cannot be null.");
79-
this.context = Context.NONE;
80-
this.properties = new HashMap<>();
81-
this.systemProperties = new SystemProperties();
79+
this(BinaryData.fromBytes(Objects.requireNonNull(body, "'body' cannot be null.")));
8280
}
8381

8482
/**
@@ -101,6 +99,15 @@ public EventData(String body) {
10199
this(Objects.requireNonNull(body, "'body' cannot be null.").getBytes(UTF_8));
102100
}
103101

102+
/**
103+
* Creates an event with the provided {@link BinaryData} as payload.
104+
*
105+
* @param body The {@link BinaryData} payload for this event.
106+
*/
107+
public EventData(BinaryData body) {
108+
this(body, new SystemProperties(), Context.NONE);
109+
}
110+
104111
/**
105112
* Creates an event with the given {@code body}, system properties and context.
106113
*
@@ -109,7 +116,7 @@ public EventData(String body) {
109116
* @param context A specified key-value pair of type {@link Context}.
110117
* @throws NullPointerException if {@code body}, {@code systemProperties}, or {@code context} is {@code null}.
111118
*/
112-
EventData(byte[] body, SystemProperties systemProperties, Context context) {
119+
EventData(BinaryData body, SystemProperties systemProperties, Context context) {
113120
this.body = Objects.requireNonNull(body, "'body' cannot be null.");
114121
this.context = Objects.requireNonNull(context, "'context' cannot be null.");
115122
this.systemProperties = Objects.requireNonNull(systemProperties, "'systemProperties' cannot be null.");
@@ -155,7 +162,7 @@ public Map<String, Object> getSystemProperties() {
155162
* @return A byte array representing the data.
156163
*/
157164
public byte[] getBody() {
158-
return Arrays.copyOf(body, body.length);
165+
return body.toBytes();
159166
}
160167

161168
/**
@@ -164,7 +171,16 @@ public byte[] getBody() {
164171
* @return UTF-8 decoded string representation of the event data.
165172
*/
166173
public String getBodyAsString() {
167-
return new String(body, UTF_8);
174+
return new String(body.toBytes(), UTF_8);
175+
}
176+
177+
/**
178+
* Returns the {@link BinaryData} payload associated with this event.
179+
*
180+
* @return the {@link BinaryData} payload associated with this event.
181+
*/
182+
public BinaryData getBodyAsBinaryData() {
183+
return body;
168184
}
169185

170186
/**
@@ -226,15 +242,15 @@ public boolean equals(Object o) {
226242
}
227243

228244
EventData eventData = (EventData) o;
229-
return Arrays.equals(body, eventData.body);
245+
return Arrays.equals(body.toBytes(), eventData.body.toBytes());
230246
}
231247

232248
/**
233249
* {@inheritDoc}
234250
*/
235251
@Override
236252
public int hashCode() {
237-
return Arrays.hashCode(body);
253+
return Arrays.hashCode(body.toBytes());
238254
}
239255

240256
/**

sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventDataBatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public int getSizeInBytes() {
115115
*/
116116
public boolean tryAdd(final EventData eventData) {
117117
if (eventData == null) {
118-
throw logger.logExceptionAsWarning(new IllegalArgumentException("eventData cannot be null"));
118+
throw logger.logExceptionAsWarning(new NullPointerException("eventData cannot be null"));
119119
}
120120
EventData event = tracerProvider.isEnabled() ? traceMessageSpan(eventData) : eventData;
121121

sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventHubMessageSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.azure.core.amqp.AmqpMessageConstant;
77
import com.azure.core.amqp.implementation.MessageSerializer;
88
import com.azure.core.exception.AzureException;
9+
import com.azure.core.util.BinaryData;
910
import com.azure.core.util.Context;
1011
import com.azure.core.util.logging.ClientLogger;
1112
import com.azure.messaging.eventhubs.implementation.ManagementChannel;
@@ -227,7 +228,7 @@ private EventData deserializeEventData(Message message) {
227228
}
228229

229230
final EventData.SystemProperties systemProperties = new EventData.SystemProperties(receiveProperties);
230-
final EventData eventData = new EventData(body, systemProperties, Context.NONE);
231+
final EventData eventData = new EventData(BinaryData.fromBytes(body), systemProperties, Context.NONE);
231232
final Map<String, Object> properties = message.getApplicationProperties() == null
232233
? new HashMap<>()
233234
: message.getApplicationProperties().getValue();

sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventDataBatchTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void setup() {
3333

3434
@Test
3535
public void nullEventData() {
36-
assertThrows(IllegalArgumentException.class, () -> {
36+
assertThrows(NullPointerException.class, () -> {
3737
final EventDataBatch batch = new EventDataBatch(1024, null, PARTITION_KEY, null, null, null, null);
3838
batch.tryAdd(null);
3939
});

sdk/eventhubs/azure-messaging-eventhubs/src/test/java/com/azure/messaging/eventhubs/EventHubPartitionAsyncConsumerTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.azure.core.amqp.AmqpRetryPolicy;
99
import com.azure.core.amqp.implementation.AmqpReceiveLink;
1010
import com.azure.core.amqp.implementation.MessageSerializer;
11+
import com.azure.core.util.BinaryData;
1112
import com.azure.core.util.Context;
1213
import com.azure.core.util.logging.ClientLogger;
1314
import com.azure.messaging.eventhubs.implementation.AmqpReceiveLinkProcessor;
@@ -170,9 +171,12 @@ void receiveMultipleTimes() {
170171
final Message message3 = mock(Message.class);
171172
final String secondOffset = "54";
172173
final String lastOffset = "65";
173-
final EventData event1 = new EventData("Foo".getBytes(), getSystemProperties("25", 14), Context.NONE);
174-
final EventData event2 = new EventData("Bar".getBytes(), getSystemProperties(secondOffset, 21), Context.NONE);
175-
final EventData event3 = new EventData("Baz".getBytes(), getSystemProperties(lastOffset, 53), Context.NONE);
174+
final EventData event1 = new EventData(BinaryData.fromBytes("Foo".getBytes()), getSystemProperties("25", 14),
175+
Context.NONE);
176+
final EventData event2 = new EventData(BinaryData.fromBytes("Bar".getBytes()),
177+
getSystemProperties(secondOffset, 21), Context.NONE);
178+
final EventData event3 = new EventData(BinaryData.fromBytes("Baz".getBytes()),
179+
getSystemProperties(lastOffset, 53), Context.NONE);
176180

177181
when(messageSerializer.deserialize(same(message1), eq(EventData.class))).thenReturn(event1);
178182
when(messageSerializer.deserialize(same(message2), eq(EventData.class))).thenReturn(event2);
@@ -238,9 +242,12 @@ void listensToShutdownSignals() throws InterruptedException {
238242
final Message message3 = mock(Message.class);
239243
final String secondOffset = "54";
240244
final String lastOffset = "65";
241-
final EventData event1 = new EventData("Foo".getBytes(), getSystemProperties("25", 14), Context.NONE);
242-
final EventData event2 = new EventData("Bar".getBytes(), getSystemProperties(secondOffset, 21), Context.NONE);
243-
final EventData event3 = new EventData("Baz".getBytes(), getSystemProperties(lastOffset, 53), Context.NONE);
245+
final EventData event1 = new EventData(BinaryData.fromBytes("Foo".getBytes()), getSystemProperties("25", 14),
246+
Context.NONE);
247+
final EventData event2 = new EventData(BinaryData.fromBytes("Bar".getBytes()), getSystemProperties(secondOffset,
248+
21), Context.NONE);
249+
final EventData event3 = new EventData(BinaryData.fromBytes("Baz".getBytes()), getSystemProperties(lastOffset,
250+
53), Context.NONE);
244251

245252
when(messageSerializer.deserialize(same(message1), eq(EventData.class))).thenReturn(event1);
246253
when(messageSerializer.deserialize(same(message2), eq(EventData.class))).thenReturn(event2);

0 commit comments

Comments
 (0)