|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.core.codec; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import io.netty5.buffer.api.Buffer; |
| 22 | +import org.reactivestreams.Publisher; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | + |
| 25 | +import org.springframework.core.ResolvableType; |
| 26 | +import org.springframework.core.io.buffer.DataBuffer; |
| 27 | +import org.springframework.core.io.buffer.DataBufferFactory; |
| 28 | +import org.springframework.core.io.buffer.Netty5DataBufferFactory; |
| 29 | +import org.springframework.lang.Nullable; |
| 30 | +import org.springframework.util.MimeType; |
| 31 | +import org.springframework.util.MimeTypeUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Encoder for {@link Buffer Buffers}. |
| 35 | + * |
| 36 | + * @author Violeta Georgieva |
| 37 | + * @since 6.0 |
| 38 | + */ |
| 39 | +public class Netty5BufferEncoder extends AbstractEncoder<Buffer> { |
| 40 | + |
| 41 | + public Netty5BufferEncoder() { |
| 42 | + super(MimeTypeUtils.ALL); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean canEncode(ResolvableType type, @Nullable MimeType mimeType) { |
| 48 | + Class<?> clazz = type.toClass(); |
| 49 | + return super.canEncode(type, mimeType) && Buffer.class.isAssignableFrom(clazz); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Flux<DataBuffer> encode(Publisher<? extends Buffer> inputStream, |
| 54 | + DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable MimeType mimeType, |
| 55 | + @Nullable Map<String, Object> hints) { |
| 56 | + |
| 57 | + return Flux.from(inputStream).map(byteBuffer -> |
| 58 | + encodeValue(byteBuffer, bufferFactory, elementType, mimeType, hints)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public DataBuffer encodeValue(Buffer buffer, DataBufferFactory bufferFactory, ResolvableType valueType, |
| 63 | + @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { |
| 64 | + |
| 65 | + if (logger.isDebugEnabled() && !Hints.isLoggingSuppressed(hints)) { |
| 66 | + String logPrefix = Hints.getLogPrefix(hints); |
| 67 | + logger.debug(logPrefix + "Writing " + buffer.readableBytes() + " bytes"); |
| 68 | + } |
| 69 | + if (bufferFactory instanceof Netty5DataBufferFactory netty5DataBufferFactory) { |
| 70 | + return netty5DataBufferFactory.wrap(buffer); |
| 71 | + } |
| 72 | + byte[] bytes = new byte[buffer.readableBytes()]; |
| 73 | + buffer.readBytes(bytes, 0, bytes.length); |
| 74 | + buffer.close(); |
| 75 | + return bufferFactory.wrap(bytes); |
| 76 | + } |
| 77 | +} |
0 commit comments