Skip to content

Commit 3526eb4

Browse files
authored
Merge pull request #963 from rabbitmq/refactor-codec-api
Add internal AMQP message format codec
2 parents 5d1e7f6 + 4e7a976 commit 3526eb4

30 files changed

Lines changed: 3296 additions & 158 deletions
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2025 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2026 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -14,7 +14,9 @@
1414
// info@rabbitmq.com.
1515
package com.rabbitmq.stream;
1616

17-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17+
import io.netty.buffer.ByteBuf;
18+
import java.io.IOException;
19+
import java.io.OutputStream;
1820

1921
/**
2022
* Codec to encode and decode messages.
@@ -27,28 +29,16 @@ public interface Codec {
2729

2830
EncodedMessage encode(Message message);
2931

30-
Message decode(byte[] data);
32+
Message decode(ByteBuf buf, int length);
3133

3234
MessageBuilder messageBuilder();
3335

34-
class EncodedMessage {
36+
interface EncodedMessage {
3537

36-
private final int size;
37-
private final byte[] data;
38+
void writeTo(OutputStream outputStream) throws IOException;
3839

39-
@SuppressFBWarnings("EI_EXPOSE_REP2")
40-
public EncodedMessage(int size, byte[] data) {
41-
this.size = size;
42-
this.data = data;
43-
}
40+
void writeTo(ByteBuf buf);
4441

45-
@SuppressFBWarnings("EI_EXPOSE_REP")
46-
public byte[] getData() {
47-
return data;
48-
}
49-
50-
public int getSize() {
51-
return size;
52-
}
42+
int getSize();
5343
}
5444
}

src/main/java/com/rabbitmq/stream/amqp/Symbol.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2025 Broadcom. All Rights Reserved.
1+
// Copyright (c) 2020-2026 Broadcom. All Rights Reserved.
22
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33
//
44
// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
@@ -14,9 +14,12 @@
1414
// info@rabbitmq.com.
1515
package com.rabbitmq.stream.amqp;
1616

17-
public class Symbol {
17+
import java.util.concurrent.ConcurrentHashMap;
1818

19-
// FIXME maintain a cache
19+
public final class Symbol {
20+
21+
private static final int CACHE_MAX_SIZE = 2048;
22+
private static final ConcurrentHashMap<String, Symbol> CACHE = new ConcurrentHashMap<>(64);
2023

2124
private final String value;
2225

@@ -25,11 +28,40 @@ private Symbol(String value) {
2528
}
2629

2730
public static Symbol valueOf(String value) {
28-
return new Symbol(value);
31+
if (value == null) {
32+
return null;
33+
}
34+
Symbol symbol = CACHE.get(value);
35+
if (symbol == null) {
36+
symbol = new Symbol(value);
37+
if (CACHE.size() < CACHE_MAX_SIZE) {
38+
Symbol existing = CACHE.putIfAbsent(value, symbol);
39+
if (existing != null) {
40+
symbol = existing;
41+
}
42+
}
43+
}
44+
return symbol;
2945
}
3046

3147
@Override
3248
public String toString() {
3349
return value;
3450
}
51+
52+
@Override
53+
public int hashCode() {
54+
return value.hashCode();
55+
}
56+
57+
@Override
58+
public boolean equals(Object o) {
59+
if (this == o) {
60+
return true;
61+
}
62+
if (o == null || getClass() != o.getClass()) {
63+
return false;
64+
}
65+
return value.equals(((Symbol) o).value);
66+
}
3567
}

0 commit comments

Comments
 (0)