Remove Netty 5 support
Closes gh-34345
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or 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 org.springframework.core.codec;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.netty5.buffer.Buffer;
|
||||
import io.netty5.buffer.DefaultBufferAllocators;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.Netty5DataBuffer;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* Decoder for {@link Buffer Buffers}.
|
||||
*
|
||||
* @author Violeta Georgieva
|
||||
* @since 6.0
|
||||
*/
|
||||
public class Netty5BufferDecoder extends AbstractDataBufferDecoder<Buffer> {
|
||||
|
||||
public Netty5BufferDecoder() {
|
||||
super(MimeTypeUtils.ALL);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
|
||||
return (Buffer.class.isAssignableFrom(elementType.toClass()) &&
|
||||
super.canDecode(elementType, mimeType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Buffer decode(DataBuffer dataBuffer, ResolvableType elementType,
|
||||
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(Hints.getLogPrefix(hints) + "Read " + dataBuffer.readableByteCount() + " bytes");
|
||||
}
|
||||
if (dataBuffer instanceof Netty5DataBuffer netty5DataBuffer) {
|
||||
return netty5DataBuffer.getNativeBuffer();
|
||||
}
|
||||
byte[] bytes = new byte[dataBuffer.readableByteCount()];
|
||||
dataBuffer.read(bytes);
|
||||
Buffer buffer = DefaultBufferAllocators.preferredAllocator().copyOf(bytes);
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or 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 org.springframework.core.codec;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import io.netty5.buffer.Buffer;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.Netty5DataBufferFactory;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* Encoder for {@link Buffer Buffers}.
|
||||
*
|
||||
* @author Violeta Georgieva
|
||||
* @since 6.0
|
||||
*/
|
||||
public class Netty5BufferEncoder extends AbstractEncoder<Buffer> {
|
||||
|
||||
public Netty5BufferEncoder() {
|
||||
super(MimeTypeUtils.ALL);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean canEncode(ResolvableType type, @Nullable MimeType mimeType) {
|
||||
Class<?> clazz = type.toClass();
|
||||
return super.canEncode(type, mimeType) && Buffer.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<DataBuffer> encode(Publisher<? extends Buffer> inputStream,
|
||||
DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType,
|
||||
@Nullable Map<String, Object> hints) {
|
||||
|
||||
return Flux.from(inputStream).map(byteBuffer ->
|
||||
encodeValue(byteBuffer, bufferFactory, elementType, mimeType, hints));
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBuffer encodeValue(Buffer buffer, DataBufferFactory bufferFactory, ResolvableType valueType,
|
||||
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
|
||||
|
||||
if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) {
|
||||
String logPrefix = Hints.getLogPrefix(hints);
|
||||
logger.debug(logPrefix + "Writing " + buffer.readableBytes() + " bytes");
|
||||
}
|
||||
if (bufferFactory instanceof Netty5DataBufferFactory netty5DataBufferFactory) {
|
||||
return netty5DataBufferFactory.wrap(buffer);
|
||||
}
|
||||
byte[] bytes = new byte[buffer.readableBytes()];
|
||||
buffer.readBytes(bytes, 0, bytes.length);
|
||||
buffer.close();
|
||||
return bufferFactory.wrap(bytes);
|
||||
}
|
||||
}
|
||||
@@ -1,401 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or 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 org.springframework.core.io.buffer;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
import io.netty5.buffer.Buffer;
|
||||
import io.netty5.buffer.BufferComponent;
|
||||
import io.netty5.buffer.ComponentIterator;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Implementation of the {@code DataBuffer} interface that wraps a Netty 5
|
||||
* {@link Buffer}. Typically constructed with {@link Netty5DataBufferFactory}.
|
||||
*
|
||||
* @author Violeta Georgieva
|
||||
* @author Arjen Poutsma
|
||||
* @since 6.0
|
||||
*/
|
||||
public final class Netty5DataBuffer implements CloseableDataBuffer, TouchableDataBuffer {
|
||||
|
||||
private final Buffer buffer;
|
||||
|
||||
private final Netty5DataBufferFactory dataBufferFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new {@code Netty5DataBuffer} based on the given {@code Buffer}.
|
||||
* @param buffer the buffer to base this buffer on
|
||||
*/
|
||||
Netty5DataBuffer(Buffer buffer, Netty5DataBufferFactory dataBufferFactory) {
|
||||
Assert.notNull(buffer, "Buffer must not be null");
|
||||
Assert.notNull(dataBufferFactory, "Netty5DataBufferFactory must not be null");
|
||||
this.buffer = buffer;
|
||||
this.dataBufferFactory = dataBufferFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Directly exposes the native {@code Buffer} that this buffer is based on.
|
||||
* @return the wrapped buffer
|
||||
*/
|
||||
public Buffer getNativeBuffer() {
|
||||
return this.buffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBufferFactory factory() {
|
||||
return this.dataBufferFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int indexOf(IntPredicate predicate, int fromIndex) {
|
||||
Assert.notNull(predicate, "IntPredicate must not be null");
|
||||
if (fromIndex < 0) {
|
||||
fromIndex = 0;
|
||||
}
|
||||
else if (fromIndex >= this.buffer.writerOffset()) {
|
||||
return -1;
|
||||
}
|
||||
int length = this.buffer.writerOffset() - fromIndex;
|
||||
int bytes = this.buffer.openCursor(fromIndex, length).process(predicate.negate()::test);
|
||||
return bytes == -1 ? -1 : fromIndex + bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lastIndexOf(IntPredicate predicate, int fromIndex) {
|
||||
Assert.notNull(predicate, "IntPredicate must not be null");
|
||||
if (fromIndex < 0) {
|
||||
return -1;
|
||||
}
|
||||
fromIndex = Math.min(fromIndex, this.buffer.writerOffset() - 1);
|
||||
return this.buffer.openCursor(0, fromIndex + 1).process(predicate.negate()::test);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readableByteCount() {
|
||||
return this.buffer.readableBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int writableByteCount() {
|
||||
return this.buffer.writableBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readPosition() {
|
||||
return this.buffer.readerOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer readPosition(int readPosition) {
|
||||
this.buffer.readerOffset(readPosition);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int writePosition() {
|
||||
return this.buffer.writerOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer writePosition(int writePosition) {
|
||||
this.buffer.writerOffset(writePosition);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getByte(int index) {
|
||||
return this.buffer.getByte(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int capacity() {
|
||||
return this.buffer.capacity();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Netty5DataBuffer capacity(int capacity) {
|
||||
if (capacity <= 0) {
|
||||
throw new IllegalArgumentException(String.format("'newCapacity' %d must be higher than 0", capacity));
|
||||
}
|
||||
int diff = capacity - capacity();
|
||||
if (diff > 0) {
|
||||
this.buffer.ensureWritable(this.buffer.writableBytes() + diff);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBuffer ensureWritable(int capacity) {
|
||||
Assert.isTrue(capacity >= 0, "Capacity must be >= 0");
|
||||
this.buffer.ensureWritable(capacity);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte read() {
|
||||
return this.buffer.readByte();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer read(byte[] destination) {
|
||||
return read(destination, 0, destination.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer read(byte[] destination, int offset, int length) {
|
||||
this.buffer.readBytes(destination, offset, length);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer write(byte b) {
|
||||
this.buffer.writeByte(b);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer write(byte[] source) {
|
||||
this.buffer.writeBytes(source);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer write(byte[] source, int offset, int length) {
|
||||
this.buffer.writeBytes(source, offset, length);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer write(DataBuffer... dataBuffers) {
|
||||
if (!ObjectUtils.isEmpty(dataBuffers)) {
|
||||
if (hasNetty5DataBuffers(dataBuffers)) {
|
||||
Buffer[] nativeBuffers = new Buffer[dataBuffers.length];
|
||||
for (int i = 0; i < dataBuffers.length; i++) {
|
||||
nativeBuffers[i] = ((Netty5DataBuffer) dataBuffers[i]).getNativeBuffer();
|
||||
}
|
||||
return write(nativeBuffers);
|
||||
}
|
||||
else {
|
||||
ByteBuffer[] byteBuffers = new ByteBuffer[dataBuffers.length];
|
||||
for (int i = 0; i < dataBuffers.length; i++) {
|
||||
byteBuffers[i] = ByteBuffer.allocate(dataBuffers[i].readableByteCount());
|
||||
dataBuffers[i].toByteBuffer(byteBuffers[i]);
|
||||
}
|
||||
return write(byteBuffers);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private static boolean hasNetty5DataBuffers(DataBuffer[] buffers) {
|
||||
for (DataBuffer buffer : buffers) {
|
||||
if (!(buffer instanceof Netty5DataBuffer)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer write(ByteBuffer... buffers) {
|
||||
if (!ObjectUtils.isEmpty(buffers)) {
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
this.buffer.writeBytes(buffer);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes one or more Netty 5 {@link Buffer Buffers} to this buffer,
|
||||
* starting at the current writing position.
|
||||
* @param buffers the buffers to write into this buffer
|
||||
* @return this buffer
|
||||
*/
|
||||
public Netty5DataBuffer write(Buffer... buffers) {
|
||||
if (!ObjectUtils.isEmpty(buffers)) {
|
||||
for (Buffer buffer : buffers) {
|
||||
this.buffer.writeBytes(buffer);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBuffer write(CharSequence charSequence, Charset charset) {
|
||||
Assert.notNull(charSequence, "CharSequence must not be null");
|
||||
Assert.notNull(charset, "Charset must not be null");
|
||||
|
||||
this.buffer.writeCharSequence(charSequence, charset);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p><strong>Note</strong> that due to the lack of a {@code slice} method
|
||||
* in Netty 5's {@link Buffer}, this implementation returns a copy that
|
||||
* does <strong>not</strong> share its contents with this buffer.
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public DataBuffer slice(int index, int length) {
|
||||
Buffer copy = this.buffer.copy(index, length);
|
||||
return new Netty5DataBuffer(copy, this.dataBufferFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBuffer split(int index) {
|
||||
Buffer split = this.buffer.split(index);
|
||||
return new Netty5DataBuffer(split, this.dataBufferFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public ByteBuffer asByteBuffer() {
|
||||
return toByteBuffer();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public ByteBuffer asByteBuffer(int index, int length) {
|
||||
return toByteBuffer(index, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public ByteBuffer toByteBuffer(int index, int length) {
|
||||
ByteBuffer copy = this.buffer.isDirect() ?
|
||||
ByteBuffer.allocateDirect(length) :
|
||||
ByteBuffer.allocate(length);
|
||||
|
||||
this.buffer.copyInto(index, copy, 0, length);
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length) {
|
||||
this.buffer.copyInto(srcPos, dest, destPos, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBufferIterator readableByteBuffers() {
|
||||
return new BufferComponentIterator<>(this.buffer.forEachComponent(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBufferIterator writableByteBuffers() {
|
||||
return new BufferComponentIterator<>(this.buffer.forEachComponent(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Charset charset) {
|
||||
Assert.notNull(charset, "Charset must not be null");
|
||||
return this.buffer.toString(charset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(int index, int length, Charset charset) {
|
||||
Assert.notNull(charset, "Charset must not be null");
|
||||
byte[] data = new byte[length];
|
||||
this.buffer.copyInto(index, data, 0, length);
|
||||
return new String(data, 0, length, charset);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer touch(Object hint) {
|
||||
this.buffer.touch(hint);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
this.buffer.close();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof Netty5DataBuffer that && this.buffer.equals(that.buffer)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.buffer.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
private static final class BufferComponentIterator<T extends BufferComponent & ComponentIterator.Next>
|
||||
implements ByteBufferIterator {
|
||||
|
||||
private final ComponentIterator<T> delegate;
|
||||
|
||||
private final boolean readable;
|
||||
|
||||
private @Nullable T next;
|
||||
|
||||
public BufferComponentIterator(ComponentIterator<T> delegate, boolean readable) {
|
||||
Assert.notNull(delegate, "Delegate must not be null");
|
||||
this.delegate = delegate;
|
||||
this.readable = readable;
|
||||
this.next = readable ? this.delegate.firstReadable() : this.delegate.firstWritable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return this.next != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer next() {
|
||||
if (this.next != null) {
|
||||
ByteBuffer result;
|
||||
if (this.readable) {
|
||||
result = this.next.readableBuffer();
|
||||
this.next = this.next.nextReadable();
|
||||
}
|
||||
else {
|
||||
result = this.next.writableBuffer();
|
||||
this.next = this.next.nextWritable();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
this.delegate.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or 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 org.springframework.core.io.buffer;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
import io.netty5.buffer.Buffer;
|
||||
import io.netty5.buffer.BufferAllocator;
|
||||
import io.netty5.buffer.CompositeBuffer;
|
||||
import io.netty5.buffer.DefaultBufferAllocators;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation of the {@code DataBufferFactory} interface based on a
|
||||
* Netty 5 {@link BufferAllocator}.
|
||||
*
|
||||
* @author Violeta Georgieva
|
||||
* @author Arjen Poutsma
|
||||
* @since 6.0
|
||||
*/
|
||||
public class Netty5DataBufferFactory implements DataBufferFactory {
|
||||
|
||||
private final BufferAllocator bufferAllocator;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new {@code Netty5DataBufferFactory} based on the given factory.
|
||||
* @param bufferAllocator the factory to use
|
||||
*/
|
||||
public Netty5DataBufferFactory(BufferAllocator bufferAllocator) {
|
||||
Assert.notNull(bufferAllocator, "BufferAllocator must not be null");
|
||||
this.bufferAllocator = bufferAllocator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the {@code BufferAllocator} used by this factory.
|
||||
*/
|
||||
public BufferAllocator getBufferAllocator() {
|
||||
return this.bufferAllocator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public Netty5DataBuffer allocateBuffer() {
|
||||
Buffer buffer = this.bufferAllocator.allocate(256);
|
||||
return new Netty5DataBuffer(buffer, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer allocateBuffer(int initialCapacity) {
|
||||
Buffer buffer = this.bufferAllocator.allocate(initialCapacity);
|
||||
return new Netty5DataBuffer(buffer, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer wrap(ByteBuffer byteBuffer) {
|
||||
Buffer buffer = this.bufferAllocator.copyOf(byteBuffer);
|
||||
return new Netty5DataBuffer(buffer, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Netty5DataBuffer wrap(byte[] bytes) {
|
||||
Buffer buffer = this.bufferAllocator.copyOf(bytes);
|
||||
return new Netty5DataBuffer(buffer, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the given Netty {@link Buffer} in a {@code Netty5DataBuffer}.
|
||||
* @param buffer the Netty buffer to wrap
|
||||
* @return the wrapped buffer
|
||||
*/
|
||||
public Netty5DataBuffer wrap(Buffer buffer) {
|
||||
buffer.touch("Wrap buffer");
|
||||
return new Netty5DataBuffer(buffer, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p>This implementation uses Netty's {@link CompositeBuffer}.
|
||||
*/
|
||||
@Override
|
||||
public DataBuffer join(List<? extends DataBuffer> dataBuffers) {
|
||||
Assert.notEmpty(dataBuffers, "DataBuffer List must not be empty");
|
||||
if (dataBuffers.size() == 1) {
|
||||
return dataBuffers.get(0);
|
||||
}
|
||||
CompositeBuffer composite = this.bufferAllocator.compose();
|
||||
for (DataBuffer dataBuffer : dataBuffers) {
|
||||
Assert.isInstanceOf(Netty5DataBuffer.class, dataBuffer);
|
||||
composite.extendWith(((Netty5DataBuffer) dataBuffer).getNativeBuffer().send());
|
||||
}
|
||||
return new Netty5DataBuffer(composite, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirect() {
|
||||
return this.bufferAllocator.getAllocationType().isDirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the given Netty {@link DataBuffer} as a {@link Buffer}.
|
||||
* <p>Returns the {@linkplain Netty5DataBuffer#getNativeBuffer() native buffer}
|
||||
* if {@code buffer} is a {@link Netty5DataBuffer}; returns
|
||||
* {@link BufferAllocator#copyOf(ByteBuffer)} otherwise.
|
||||
* @param buffer the {@code DataBuffer} to return a {@code Buffer} for
|
||||
* @return the netty {@code Buffer}
|
||||
*/
|
||||
public static Buffer toBuffer(DataBuffer buffer) {
|
||||
if (buffer instanceof Netty5DataBuffer netty5DataBuffer) {
|
||||
return netty5DataBuffer.getNativeBuffer();
|
||||
}
|
||||
else {
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(buffer.readableByteCount());
|
||||
buffer.toByteBuffer(byteBuffer);
|
||||
return DefaultBufferAllocators.preferredAllocator().copyOf(byteBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Netty5DataBufferFactory (" + this.bufferAllocator + ")";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user