001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.core.codec;
018
019import java.util.Map;
020
021import org.reactivestreams.Publisher;
022import reactor.core.publisher.Flux;
023
024import org.springframework.core.ResolvableType;
025import org.springframework.core.io.buffer.DataBuffer;
026import org.springframework.lang.Nullable;
027import org.springframework.util.MimeType;
028import org.springframework.util.MimeTypeUtils;
029
030/**
031 * Simple pass-through decoder for {@link DataBuffer DataBuffers}.
032 *
033 * <p><strong>Note:</strong> The data buffers should be released via
034 * {@link org.springframework.core.io.buffer.DataBufferUtils#release(DataBuffer)}
035 * after they have been consumed. In addition, if using {@code Flux} or
036 * {@code Mono} operators such as flatMap, reduce, and others that prefetch,
037 * cache, and skip or filter out data items internally, please add
038 * {@code doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)} to the
039 * composition chain to ensure cached data buffers are released prior to an
040 * error or cancellation signal.
041 *
042 * @author Arjen Poutsma
043 * @author Rossen Stoyanchev
044 * @since 5.0
045 */
046public class DataBufferDecoder extends AbstractDataBufferDecoder<DataBuffer> {
047
048        public DataBufferDecoder() {
049                super(MimeTypeUtils.ALL);
050        }
051
052
053        @Override
054        public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
055                return (DataBuffer.class.isAssignableFrom(elementType.toClass()) &&
056                                super.canDecode(elementType, mimeType));
057        }
058
059        @Override
060        public Flux<DataBuffer> decode(Publisher<DataBuffer> input, ResolvableType elementType,
061                        @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
062
063                return Flux.from(input);
064        }
065
066        @Override
067        public DataBuffer decode(DataBuffer buffer, ResolvableType elementType,
068                        @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
069
070                if (logger.isDebugEnabled()) {
071                        logger.debug(Hints.getLogPrefix(hints) + "Read " + buffer.readableByteCount() + " bytes");
072                }
073                return buffer;
074        }
075
076}