|
|
|
@ -31,6 +31,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter; |
|
|
|
* to any type that the {@link ConversionService} support via {@code byte[]}. |
|
|
|
* to any type that the {@link ConversionService} support via {@code byte[]}. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Phillip Webb |
|
|
|
* @author Phillip Webb |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
* @since 4.0 |
|
|
|
* @since 4.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
final class ByteBufferConverter implements ConditionalGenericConverter { |
|
|
|
final class ByteBufferConverter implements ConditionalGenericConverter { |
|
|
|
@ -40,8 +41,11 @@ final class ByteBufferConverter implements ConditionalGenericConverter { |
|
|
|
private static final TypeDescriptor BYTE_ARRAY_TYPE = TypeDescriptor.valueOf(byte[].class); |
|
|
|
private static final TypeDescriptor BYTE_ARRAY_TYPE = TypeDescriptor.valueOf(byte[].class); |
|
|
|
|
|
|
|
|
|
|
|
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS; |
|
|
|
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS; |
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
static { |
|
|
|
Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>(); |
|
|
|
Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>(4); |
|
|
|
|
|
|
|
convertiblePairs.add(new ConvertiblePair(ByteBuffer.class, byte[].class)); |
|
|
|
|
|
|
|
convertiblePairs.add(new ConvertiblePair(byte[].class, ByteBuffer.class)); |
|
|
|
convertiblePairs.add(new ConvertiblePair(ByteBuffer.class, Object.class)); |
|
|
|
convertiblePairs.add(new ConvertiblePair(ByteBuffer.class, Object.class)); |
|
|
|
convertiblePairs.add(new ConvertiblePair(Object.class, ByteBuffer.class)); |
|
|
|
convertiblePairs.add(new ConvertiblePair(Object.class, ByteBuffer.class)); |
|
|
|
CONVERTIBLE_PAIRS = Collections.unmodifiableSet(convertiblePairs); |
|
|
|
CONVERTIBLE_PAIRS = Collections.unmodifiableSet(convertiblePairs); |
|
|
|
@ -63,13 +67,11 @@ final class ByteBufferConverter implements ConditionalGenericConverter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
|
|
|
|
boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE); |
|
|
|
if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
|
|
|
if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
|
|
|
return matchesFromByteBuffer(targetType); |
|
|
|
return (byteBufferTarget || matchesFromByteBuffer(targetType)); |
|
|
|
} |
|
|
|
|
|
|
|
if (targetType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
|
|
|
|
|
|
|
return matchesToByteBuffer(sourceType); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return (byteBufferTarget && matchesToByteBuffer(sourceType)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean matchesFromByteBuffer(TypeDescriptor targetType) { |
|
|
|
private boolean matchesFromByteBuffer(TypeDescriptor targetType) { |
|
|
|
@ -84,10 +86,12 @@ final class ByteBufferConverter implements ConditionalGenericConverter { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
|
|
|
if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
|
|
|
boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE); |
|
|
|
return convertFromByteBuffer((ByteBuffer) source, targetType); |
|
|
|
if (source instanceof ByteBuffer) { |
|
|
|
|
|
|
|
ByteBuffer buffer = (ByteBuffer) source; |
|
|
|
|
|
|
|
return (byteBufferTarget ? buffer.duplicate() : convertFromByteBuffer(buffer, targetType)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (targetType.isAssignableTo(BYTE_BUFFER_TYPE)) { |
|
|
|
if (byteBufferTarget) { |
|
|
|
return convertToByteBuffer(source, sourceType); |
|
|
|
return convertToByteBuffer(source, sourceType); |
|
|
|
} |
|
|
|
} |
|
|
|
// Should not happen
|
|
|
|
// Should not happen
|
|
|
|
|