Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp: ByteBufferSerde #604

Merged
merged 7 commits into from Oct 20, 2023
Merged

imp: ByteBufferSerde #604

merged 7 commits into from Oct 20, 2023

Conversation

import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.SuiteDisplayName;

@Suite
@ExcludeClassNamePatterns(
"io.micronaut.serde.tck.tests.bytebuffer.ByteBufferNativeTest"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since youre copying the data anyway, you can just copy it for direct buffers too. but ideally there should be overloads for encodeBinary for ByteBuffers.

@sdelamo sdelamo linked an issue Oct 16, 2023 that may be closed by this pull request

private void encodeByteBuffer(@NonNull Encoder encoder,
@NonNull ByteBuffer value) throws IOException {
if (value.hasArray()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what if not

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could take inspiration from Jackson ad do something like

diff --git a/serde-support/src/main/java/io/micronaut/serde/support/serdes/ByteBufferSerde.java b/serde-support/src/main/java/io/micronaut/serde/support/serdes/ByteBufferSerde.java
index 06e03a2f..12bc6b8d 100644
--- a/serde-support/src/main/java/io/micronaut/serde/support/serdes/ByteBufferSerde.java
+++ b/serde-support/src/main/java/io/micronaut/serde/support/serdes/ByteBufferSerde.java
@@ -58,6 +58,43 @@ public class ByteBufferSerde implements Serde<ByteBuffer> {
             byte[] copy = new byte[len];
             System.arraycopy(value.array(), offset, copy, 0, len);
             encoder.encodeBinary(copy);
+            return;
+        }
+        try (InputStream s = new ByteBufferInputStream(value.asReadOnlyBuffer().rewind())) {
+            encoder.encodeBinary(s.readAllBytes());
+        }
+    }
+
+    private static class ByteBufferInputStream extends InputStream {
+
+        private static final int EOS = -1;
+        private final ByteBuffer buffer;
+
+        private ByteBufferInputStream(ByteBuffer buffer) {
+            this.buffer = buffer;
+        }
+
+        @Override
+        public int available() {
+            return buffer.remaining();
+        }
+
+        @Override
+        public int read() {
+            if (buffer.hasRemaining()) {
+                return buffer.get() & 0xFF;
+            }
+            return EOS;
+        }
+
+        @Override
+        public int read(byte[] b, int off, int len) {
+            if (buffer.hasRemaining()) {
+                len = Math.min(len, buffer.remaining());
+                buffer.get(b, off, len);
+                return len;
+            }
+            return EOS;
         }
     }
 }

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm not sure my use of rewind is correct here... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the following commit efe6b37

@sdelamo sdelamo requested a review from yawkat October 16, 2023 16:20
@yawkat
Copy link
Member

yawkat commented Oct 16, 2023

theres easier ways, i will look at it tomorrow.

@yawkat
Copy link
Member

yawkat commented Oct 17, 2023

ive implemented a smaller copy mechanism. also i discovered a jackson-databind bug which might break the tck: FasterXML/jackson-databind#4164

Copy link
Contributor Author

@sdelamo sdelamo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timyates can you review and approve. I cannot since I created the PR

…s/serde/SerializationSerdeSuite.java

Co-authored-by: Sergio del Amo <sergio.delamo@softamo.com>
@sonarcloud
Copy link

sonarcloud bot commented Oct 18, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 5 Code Smells

2.1% 2.1% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@graemerocher graemerocher merged commit 33caa31 into master Oct 20, 2023
11 of 12 checks passed
@graemerocher graemerocher deleted the bytebuffer branch October 20, 2023 06:46
@graemerocher graemerocher added the type: enhancement New feature or request label Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Error decoding ByteBuffer
4 participants