001/* 002 * Copyright 2002-2020 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.List; 020import java.util.Map; 021 022import org.reactivestreams.Publisher; 023import reactor.core.publisher.Flux; 024import reactor.core.publisher.Mono; 025 026import org.springframework.core.ResolvableType; 027import org.springframework.core.io.buffer.DataBuffer; 028import org.springframework.core.io.buffer.DataBufferFactory; 029import org.springframework.lang.Nullable; 030import org.springframework.util.MimeType; 031 032/** 033 * Strategy to encode a stream of Objects of type {@code <T>} into an output 034 * stream of bytes. 035 * 036 * @author Sebastien Deleuze 037 * @author Rossen Stoyanchev 038 * @since 5.0 039 * @param <T> the type of elements in the input stream 040 */ 041public interface Encoder<T> { 042 043 /** 044 * Whether the encoder supports the given source element type and the MIME 045 * type for the output stream. 046 * @param elementType the type of elements in the source stream 047 * @param mimeType the MIME type for the output stream 048 * (can be {@code null} if not specified) 049 * @return {@code true} if supported, {@code false} otherwise 050 */ 051 boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType); 052 053 /** 054 * Encode a stream of Objects of type {@code T} into a {@link DataBuffer} 055 * output stream. 056 * @param inputStream the input stream of Objects to encode. If the input should be 057 * encoded as a single value rather than as a stream of elements, an instance of 058 * {@link Mono} should be used. 059 * @param bufferFactory for creating output stream {@code DataBuffer}'s 060 * @param elementType the expected type of elements in the input stream; 061 * this type must have been previously passed to the {@link #canEncode} 062 * method and it must have returned {@code true}. 063 * @param mimeType the MIME type for the output content (optional) 064 * @param hints additional information about how to encode 065 * @return the output stream 066 */ 067 Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory, 068 ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints); 069 070 /** 071 * Encode an Object of type T to a data buffer. This is useful for scenarios, 072 * that distinct messages (or events) are encoded and handled individually, 073 * in fully aggregated form. 074 * <p>By default this method raises {@link UnsupportedOperationException} 075 * and it is expected that some encoders cannot produce a single buffer or 076 * cannot do so synchronously (e.g. encoding a {@code Resource}). 077 * @param value the value to be encoded 078 * @param bufferFactory for creating the output {@code DataBuffer} 079 * @param valueType the type for the value being encoded 080 * @param mimeType the MIME type for the output content (optional) 081 * @param hints additional information about how to encode 082 * @return the encoded content 083 * @since 5.2 084 */ 085 default DataBuffer encodeValue(T value, DataBufferFactory bufferFactory, 086 ResolvableType valueType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { 087 088 // It may not be possible to produce a single DataBuffer synchronously 089 throw new UnsupportedOperationException(); 090 } 091 092 /** 093 * Return the list of mime types this encoder supports. 094 */ 095 List<MimeType> getEncodableMimeTypes(); 096 097}