Skip to content

Commit 93ce67f

Browse files
author
Paras Jain
committed
adding serialization support for InetSocketAddress
Signed-off-by: Paras Jain <parasjaz@amazon.com>
1 parent 7f5a378 commit 93ce67f

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1010
- Allow mmap to use new JDK-19 preview APIs in Apache Lucene 9.4+ ([#5151](https://github.com/opensearch-project/OpenSearch/pull/5151))
1111
- Add events correlation engine plugin ([#6854](https://github.com/opensearch-project/OpenSearch/issues/6854))
1212
- Add support for ignoring missing Javadoc on generated code using annotation ([#7604](https://github.com/opensearch-project/OpenSearch/pull/7604))
13+
- Add support for serializing `InetSocketAddress` in StreamOutput/StreamInput
1314

1415
### Dependencies
1516
- Bump `log4j-core` from 2.18.0 to 2.19.0

server/src/main/java/org/opensearch/common/io/stream/StreamInput.java

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import java.io.FilterInputStream;
6464
import java.io.IOException;
6565
import java.math.BigInteger;
66+
import java.net.InetAddress;
67+
import java.net.InetSocketAddress;
6668
import java.nio.file.AccessDeniedException;
6769
import java.nio.file.AtomicMoveNotSupportedException;
6870
import java.nio.file.DirectoryNotEmptyException;
@@ -346,6 +348,14 @@ public BigInteger readBigInteger() throws IOException {
346348
return new BigInteger(readString());
347349
}
348350

351+
public InetSocketAddress readInetSocketAddress() throws IOException {
352+
String host = this.readString();
353+
byte[] addressBytes = this.readByteArray();
354+
InetAddress addr = InetAddress.getByAddress(host, addressBytes);
355+
int port = this.readInt();
356+
return new InetSocketAddress(addr, port);
357+
}
358+
349359
@Nullable
350360
public Text readOptionalText() throws IOException {
351361
int length = readInt();
@@ -742,6 +752,8 @@ public Object readGenericValue() throws IOException {
742752
return readCollection(StreamInput::readGenericValue, HashSet::new, Collections.emptySet());
743753
case 26:
744754
return readBigInteger();
755+
case 27:
756+
return readInetSocketAddress();
745757
default:
746758
throw new IOException("Can't read unknown type [" + type + "]");
747759
}

server/src/main/java/org/opensearch/common/io/stream/StreamOutput.java

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import java.io.FileNotFoundException;
6464
import java.io.IOException;
6565
import java.math.BigInteger;
66+
import java.net.InetSocketAddress;
6667
import java.nio.file.AccessDeniedException;
6768
import java.nio.file.AtomicMoveNotSupportedException;
6869
import java.nio.file.DirectoryNotEmptyException;
@@ -791,6 +792,13 @@ public final void writeOptionalInstant(@Nullable Instant instant) throws IOExcep
791792
o.writeByte((byte) 26);
792793
o.writeString(v.toString());
793794
});
795+
writers.put(InetSocketAddress.class, (o, v) -> {
796+
final InetSocketAddress inetSocketAddress = (InetSocketAddress) v;
797+
o.writeByte((byte) 27);
798+
o.writeString(inetSocketAddress.getHostName());
799+
o.writeByteArray(inetSocketAddress.getAddress().getAddress());
800+
o.writeInt(inetSocketAddress.getPort());
801+
});
794802
WRITERS = Collections.unmodifiableMap(writers);
795803
}
796804

server/src/test/java/org/opensearch/common/io/stream/BaseStreamTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import java.io.ByteArrayInputStream;
4747
import java.io.EOFException;
4848
import java.io.IOException;
49+
import java.net.InetAddress;
50+
import java.net.InetSocketAddress;
4951
import java.time.Instant;
5052
import java.time.ZoneOffset;
5153
import java.util.ArrayList;
@@ -513,6 +515,14 @@ public void testMapIsWriteable() throws IOException {
513515
assertNotWriteable(Collections.singletonMap("a", new Unwriteable()), Unwriteable.class);
514516
}
515517

518+
public void testInetSocketAddress() throws IOException {
519+
InetSocketAddress toWrite = new InetSocketAddress(InetAddress.getLocalHost(), 80);
520+
BytesStreamOutput out = new BytesStreamOutput();
521+
out.writeGenericValue(toWrite);
522+
assertEquals(toWrite, getStreamInput(out.bytes()).readGenericValue());
523+
out.close();
524+
}
525+
516526
public void testObjectArrayIsWriteable() throws IOException {
517527
StreamOutput.checkWriteable(new Object[] { "a", "b" });
518528
assertNotWriteable(new Object[] { new Unwriteable() }, Unwriteable.class);

server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
import java.io.EOFException;
5050
import java.io.IOException;
51+
import java.net.InetAddress;
52+
import java.net.InetSocketAddress;
5153
import java.time.ZoneId;
5254
import java.util.ArrayList;
5355
import java.util.Collections;
@@ -311,6 +313,7 @@ public void testSimpleStreams() throws Exception {
311313
float[] floatArray = { 1.1f, 2.2f, 3.3f };
312314
out.writeGenericValue(floatArray);
313315
double[] doubleArray = { 1.1, 2.2, 3.3 };
316+
InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost(), 80);
314317
out.writeGenericValue(doubleArray);
315318
out.writeString("hello");
316319
out.writeString("goodbye");
@@ -324,6 +327,7 @@ public void testSimpleStreams() throws Exception {
324327
out.writeOptionalTimeZone(DateTimeZone.getDefault());
325328
out.writeOptionalTimeZone(null);
326329
out.writeGenericValue(new DateTime(123456, DateTimeZone.forID("America/Los_Angeles")));
330+
out.writeGenericValue(inetSocketAddress);
327331
final byte[] bytes = BytesReference.toBytes(out.bytes());
328332
StreamInput in = StreamInput.wrap(BytesReference.toBytes(out.bytes()));
329333
assertEquals(in.available(), bytes.length);
@@ -361,6 +365,7 @@ public void testSimpleStreams() throws Exception {
361365
JodaCompatibleZonedDateTime jdt = (JodaCompatibleZonedDateTime) dt;
362366
assertThat(jdt.getZonedDateTime().toInstant().toEpochMilli(), equalTo(123456L));
363367
assertThat(jdt.getZonedDateTime().getZone(), equalTo(ZoneId.of("America/Los_Angeles")));
368+
assertEquals(inetSocketAddress, in.readGenericValue());
364369
assertEquals(0, in.available());
365370
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> out.writeGenericValue(new Object() {
366371
@Override

0 commit comments

Comments
 (0)