Skip to content

Commit

Permalink
add ByteBufferBackedInputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Oct 16, 2023
1 parent e492cc7 commit efe6b37
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.serde.support.serdes;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
* Fork from `com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream` licensed with Apache 2.0. License.
* Simple {@link InputStream} implementation that exposes currently
* available content of a {@link ByteBuffer}.
*/
public class ByteBufferBackedInputStream extends InputStream {
protected final ByteBuffer _b;

public ByteBufferBackedInputStream(ByteBuffer buf) { _b = buf; }

@Override public int available() { return _b.remaining(); }

@Override
public int read() throws IOException { return _b.hasRemaining() ? (_b.get() & 0xFF) : -1; }

@Override
public int read(byte[] bytes, int off, int len) throws IOException {
if (!_b.hasRemaining()) return -1;
len = Math.min(len, _b.remaining());
_b.get(bytes, off, len);
return len;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ public void serialize(@NonNull Encoder encoder,
@NonNull EncoderContext context,
@NonNull Argument<? extends ByteBuffer> type,
@NonNull ByteBuffer value) throws IOException {
encodeByteBuffer(encoder, value);
}

private void encodeByteBuffer(@NonNull Encoder encoder,
@NonNull ByteBuffer value) throws IOException {
if (value.hasArray()) {
final int pos = value.position();
int len = value.limit() - pos;
int offset = value.arrayOffset() + pos;
byte[] copy = new byte[len];
System.arraycopy(value.array(), offset, copy, 0, len);
encoder.encodeBinary(copy);
return;
}
// the other case is more complicated however. Best to handle with InputStream wrapper.
// But should we rewind it; and/or make a copy?
ByteBuffer copy = value.asReadOnlyBuffer();
if (copy.position() > 0) {
copy.rewind();
}
try (InputStream s = new ByteBufferBackedInputStream(copy)) {
encoder.encodeBinary(s.readAllBytes());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import org.junit.platform.suite.api.SuiteDisplayName;

@Suite
@ExcludeClassNamePatterns(
"io.micronaut.serde.tck.tests.bytebuffer.ByteBufferNativeTest" // https://github.com/FasterXML/jackson-databind/blob/2.16/src/main/java/com/fasterxml/jackson/databind/ser/std/ByteBufferSerializer.java#L27-L35
)
@SelectPackages("io.micronaut.serde.tck.tests")
@SuiteDisplayName("Serialization TCK Serde")
public class SerializationSerdeSuite {
Expand Down

0 comments on commit efe6b37

Please sign in to comment.