Encoder.java 2.6 KB
Newer Older
S
Sebastien Deleuze 已提交
1
/*
2
 * Copyright 2002-2016 the original author or authors.
S
Sebastien Deleuze 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
 *
 *      http://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.
 */

17
package org.springframework.core.codec;
S
Sebastien Deleuze 已提交
18 19

import java.util.List;
20
import java.util.Map;
S
Sebastien Deleuze 已提交
21 22

import org.reactivestreams.Publisher;
23
import reactor.core.publisher.Flux;
S
Sebastien Deleuze 已提交
24
import reactor.core.publisher.Mono;
S
Sebastien Deleuze 已提交
25 26

import org.springframework.core.ResolvableType;
27
import org.springframework.core.io.buffer.DataBuffer;
28
import org.springframework.core.io.buffer.DataBufferFactory;
S
Sebastien Deleuze 已提交
29 30 31
import org.springframework.util.MimeType;

/**
R
Rossen Stoyanchev 已提交
32 33
 * Strategy to encode a stream of Objects of type {@code <T>} into an output
 * stream of bytes.
S
Sebastien Deleuze 已提交
34 35
 *
 * @author Sebastien Deleuze
R
Rossen Stoyanchev 已提交
36
 * @author Rossen Stoyanchev
37
 * @since 5.0
38
 * @param <T> the type of elements in the input stream
S
Sebastien Deleuze 已提交
39 40 41 42
 */
public interface Encoder<T> {

	/**
R
Rossen Stoyanchev 已提交
43 44 45 46 47
	 * Whether the encoder supports the given source element type and the MIME
	 * type for the output stream.
	 * @param elementType the type of elements in the source stream
	 * @param mimeType the MIME type for the output stream
	 * @return {@code true} if supported, {@code false} otherwise
S
Sebastien Deleuze 已提交
48
	 */
49
	boolean canEncode(ResolvableType elementType, MimeType mimeType);
S
Sebastien Deleuze 已提交
50 51

	/**
R
Rossen Stoyanchev 已提交
52 53
	 * Encode a stream of Objects of type {@code T} into a {@link DataBuffer}
	 * output stream.
S
Sebastien Deleuze 已提交
54 55 56
	 * @param inputStream the input stream of Objects to encode. If the input should be
	 * encoded as a single value rather than as a stream of elements, an instance of
	 * {@link Mono} should be used.
R
Rossen Stoyanchev 已提交
57 58 59 60 61
	 * @param bufferFactory for creating output stream {@code DataBuffer}'s
	 * @param elementType the expected type of elements in the input stream;
	 * this type must have been previously passed to the {@link #canEncode}
	 * method and it must have returned {@code true}.
	 * @param mimeType the MIME type for the output stream
62
	 * @param hints additional information about how to do encode
S
Sebastien Deleuze 已提交
63 64
	 * @return the output stream
	 */
R
Rossen Stoyanchev 已提交
65
	Flux<DataBuffer> encode(Publisher<? extends T> inputStream, DataBufferFactory bufferFactory,
66
			ResolvableType elementType, MimeType mimeType, Map<String, Object> hints);
S
Sebastien Deleuze 已提交
67 68

	/**
R
Polish  
Rossen Stoyanchev 已提交
69
	 * Return the list of mime types this encoder supports.
S
Sebastien Deleuze 已提交
70
	 */
71
	List<MimeType> getEncodableMimeTypes();
S
Sebastien Deleuze 已提交
72 73

}