|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 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 | + * http://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.convert.support; |
| 18 | + |
| 19 | +import java.nio.ByteBuffer; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.springframework.core.convert.ConversionService; |
| 25 | +import org.springframework.core.convert.TypeDescriptor; |
| 26 | +import org.springframework.core.convert.converter.ConditionalGenericConverter; |
| 27 | + |
| 28 | +/** |
| 29 | + * Converts a {@link ByteBuffer} directly to and from {@code byte[]}s and indirectly to |
| 30 | + * any type that the {@link ConversionService} support via {@code byte[]}. |
| 31 | + * |
| 32 | + * @author Phillip Webb |
| 33 | + */ |
| 34 | +public class ByteBufferConverter implements ConditionalGenericConverter { |
| 35 | + |
| 36 | +private static final TypeDescriptor BYTE_BUFFER_TYPE = TypeDescriptor.valueOf(ByteBuffer.class); |
| 37 | + |
| 38 | +private static final TypeDescriptor BYTE_ARRAY_TYPE = TypeDescriptor.valueOf(byte[].class); |
| 39 | + |
| 40 | +private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS; |
| 41 | +static { |
| 42 | +Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>(); |
| 43 | +convertiblePairs.add(new ConvertiblePair(ByteBuffer.class, Object.class)); |
| 44 | +convertiblePairs.add(new ConvertiblePair(Object.class, ByteBuffer.class)); |
| 45 | +CONVERTIBLE_PAIRS = Collections.unmodifiableSet(convertiblePairs); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +private ConversionService conversionService; |
| 50 | + |
| 51 | + |
| 52 | +public ByteBufferConverter(ConversionService conversionService) { |
| 53 | +this.conversionService = conversionService; |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +@Override |
| 58 | +public Set<ConvertiblePair> getConvertibleTypes() { |
| 59 | +return CONVERTIBLE_PAIRS; |
| 60 | +} |
| 61 | + |
| 62 | +@Override |
| 63 | +public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { |
| 64 | +if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
| 65 | +return matchesFromByteBuffer(targetType); |
| 66 | +} |
| 67 | +if (targetType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
| 68 | +return matchesToByteBuffer(sourceType); |
| 69 | +} |
| 70 | +return false; |
| 71 | +} |
| 72 | + |
| 73 | +private boolean matchesFromByteBuffer(TypeDescriptor targetType) { |
| 74 | +return (targetType.isAssignableTo(BYTE_ARRAY_TYPE) || this.conversionService.canConvert( |
| 75 | +BYTE_ARRAY_TYPE, targetType)); |
| 76 | +} |
| 77 | + |
| 78 | +private boolean matchesToByteBuffer(TypeDescriptor sourceType) { |
| 79 | +return (sourceType.isAssignableTo(BYTE_ARRAY_TYPE) || this.conversionService.canConvert( |
| 80 | +sourceType, BYTE_ARRAY_TYPE)); |
| 81 | +} |
| 82 | + |
| 83 | +@Override |
| 84 | +public Object convert(Object source, TypeDescriptor sourceType, |
| 85 | +TypeDescriptor targetType) { |
| 86 | +if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
| 87 | +return convertFromByteBuffer((ByteBuffer) source, targetType); |
| 88 | +} |
| 89 | +if (targetType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
| 90 | +return convertToByteBuffer(source, sourceType); |
| 91 | +} |
| 92 | +// Should not happen |
| 93 | +throw new IllegalStateException("Unexpected source/target types"); |
| 94 | +} |
| 95 | + |
| 96 | +private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) { |
| 97 | +byte[] bytes = new byte[source.remaining()]; |
| 98 | +source.get(bytes); |
| 99 | +if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) { |
| 100 | +return bytes; |
| 101 | +} |
| 102 | +return this.conversionService.convert(bytes, BYTE_ARRAY_TYPE, targetType); |
| 103 | +} |
| 104 | + |
| 105 | +private Object convertToByteBuffer(Object source, TypeDescriptor sourceType) { |
| 106 | +byte[] bytes = (byte[]) (source instanceof byte[] ? source |
| 107 | +: this.conversionService.convert(source, sourceType, BYTE_ARRAY_TYPE)); |
| 108 | +ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length); |
| 109 | +byteBuffer.put(bytes); |
| 110 | +byteBuffer.rewind(); |
| 111 | +return byteBuffer; |
| 112 | +} |
| 113 | + |
| 114 | +} |
0 commit comments