001/*
002 * Copyright 2002-2017 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.messaging.converter;
018
019import org.springframework.lang.Nullable;
020import org.springframework.messaging.Message;
021import org.springframework.messaging.MessageHeaders;
022import org.springframework.util.MimeTypeUtils;
023
024/**
025 * A {@link MessageConverter} that supports MIME type "application/octet-stream" with the
026 * payload converted to and from a byte[].
027 *
028 * @author Rossen Stoyanchev
029 * @since 4.0
030 */
031public class ByteArrayMessageConverter extends AbstractMessageConverter {
032
033        public ByteArrayMessageConverter() {
034                super(MimeTypeUtils.APPLICATION_OCTET_STREAM);
035        }
036
037
038        @Override
039        protected boolean supports(Class<?> clazz) {
040                return (byte[].class == clazz);
041        }
042
043        @Override
044        @Nullable
045        protected Object convertFromInternal(
046                        Message<?> message, @Nullable Class<?> targetClass, @Nullable Object conversionHint) {
047
048                return message.getPayload();
049        }
050
051        @Override
052        @Nullable
053        protected Object convertToInternal(
054                        Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
055
056                return payload;
057        }
058
059}