1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
import org .springframework .core .convert .converter .ConditionalGenericConverter ;
27
27
28
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[]}.
29
+ * Converts a {@link ByteBuffer} directly to and from {@code byte[]}s and indirectly
30
+ * to any type that the {@link ConversionService} support via {@code byte[]}.
31
31
*
32
32
* @author Phillip Webb
33
+ * @author Juergen Hoeller
33
34
* @since 4.0
34
35
*/
35
36
final class ByteBufferConverter implements ConditionalGenericConverter {
@@ -39,15 +40,18 @@ final class ByteBufferConverter implements ConditionalGenericConverter {
39
40
private static final TypeDescriptor BYTE_ARRAY_TYPE = TypeDescriptor .valueOf (byte [].class );
40
41
41
42
private static final Set <ConvertiblePair > CONVERTIBLE_PAIRS ;
43
+
42
44
static {
43
- Set <ConvertiblePair > convertiblePairs = new HashSet <ConvertiblePair >();
45
+ Set <ConvertiblePair > convertiblePairs = new HashSet <ConvertiblePair >(4 );
46
+ convertiblePairs .add (new ConvertiblePair (ByteBuffer .class , byte [].class ));
47
+ convertiblePairs .add (new ConvertiblePair (byte [].class , ByteBuffer .class ));
44
48
convertiblePairs .add (new ConvertiblePair (ByteBuffer .class , Object .class ));
45
49
convertiblePairs .add (new ConvertiblePair (Object .class , ByteBuffer .class ));
46
50
CONVERTIBLE_PAIRS = Collections .unmodifiableSet (convertiblePairs );
47
51
}
48
52
49
53
50
- private ConversionService conversionService ;
54
+ private final ConversionService conversionService ;
51
55
52
56
53
57
public ByteBufferConverter (ConversionService conversionService ) {
@@ -62,32 +66,31 @@ public Set<ConvertiblePair> getConvertibleTypes() {
62
66
63
67
@ Override
64
68
public boolean matches (TypeDescriptor sourceType , TypeDescriptor targetType ) {
69
+ boolean byteBufferTarget = targetType .isAssignableTo (BYTE_BUFFER_TYPE );
65
70
if (sourceType .isAssignableTo (BYTE_BUFFER_TYPE )) {
66
- return matchesFromByteBuffer (targetType );
67
- }
68
- if (targetType .isAssignableTo (BYTE_BUFFER_TYPE )) {
69
- return matchesToByteBuffer (sourceType );
71
+ return (byteBufferTarget || matchesFromByteBuffer (targetType ));
70
72
}
71
- return false ;
73
+ return ( byteBufferTarget && matchesToByteBuffer ( sourceType )) ;
72
74
}
73
75
74
76
private boolean matchesFromByteBuffer (TypeDescriptor targetType ) {
75
- return (targetType .isAssignableTo (BYTE_ARRAY_TYPE ) || this . conversionService . canConvert (
76
- BYTE_ARRAY_TYPE , targetType ));
77
+ return (targetType .isAssignableTo (BYTE_ARRAY_TYPE ) ||
78
+ this . conversionService . canConvert ( BYTE_ARRAY_TYPE , targetType ));
77
79
}
78
80
79
81
private boolean matchesToByteBuffer (TypeDescriptor sourceType ) {
80
- return (sourceType .isAssignableTo (BYTE_ARRAY_TYPE ) || this . conversionService . canConvert (
81
- sourceType , BYTE_ARRAY_TYPE ));
82
+ return (sourceType .isAssignableTo (BYTE_ARRAY_TYPE ) ||
83
+ this . conversionService . canConvert ( sourceType , BYTE_ARRAY_TYPE ));
82
84
}
83
85
84
86
@ Override
85
- public Object convert (Object source , TypeDescriptor sourceType ,
86
- TypeDescriptor targetType ) {
87
- if (sourceType .isAssignableTo (BYTE_BUFFER_TYPE )) {
88
- return convertFromByteBuffer ((ByteBuffer ) source , targetType );
87
+ public Object convert (Object source , TypeDescriptor sourceType , TypeDescriptor targetType ) {
88
+ boolean byteBufferTarget = targetType .isAssignableTo (BYTE_BUFFER_TYPE );
89
+ if (source instanceof ByteBuffer ) {
90
+ ByteBuffer buffer = (ByteBuffer ) source ;
91
+ return (byteBufferTarget ? buffer .duplicate () : convertFromByteBuffer (buffer , targetType ));
89
92
}
90
- if (targetType . isAssignableTo ( BYTE_BUFFER_TYPE ) ) {
93
+ if (byteBufferTarget ) {
91
94
return convertToByteBuffer (source , sourceType );
92
95
}
93
96
// Should not happen
@@ -97,19 +100,20 @@ public Object convert(Object source, TypeDescriptor sourceType,
97
100
private Object convertFromByteBuffer (ByteBuffer source , TypeDescriptor targetType ) {
98
101
byte [] bytes = new byte [source .remaining ()];
99
102
source .get (bytes );
103
+
100
104
if (targetType .isAssignableTo (BYTE_ARRAY_TYPE )) {
101
105
return bytes ;
102
106
}
103
107
return this .conversionService .convert (bytes , BYTE_ARRAY_TYPE , targetType );
104
108
}
105
109
106
110
private Object convertToByteBuffer (Object source , TypeDescriptor sourceType ) {
107
- byte [] bytes = (byte []) (source instanceof byte [] ? source
108
- : this .conversionService .convert (source , sourceType , BYTE_ARRAY_TYPE ));
111
+ byte [] bytes = (byte []) (source instanceof byte [] ? source :
112
+ this .conversionService .convert (source , sourceType , BYTE_ARRAY_TYPE ));
113
+
109
114
ByteBuffer byteBuffer = ByteBuffer .allocate (bytes .length );
110
115
byteBuffer .put (bytes );
111
- byteBuffer .rewind ();
112
- return byteBuffer ;
116
+ return byteBuffer .rewind ();
113
117
}
114
118
115
119
}
0 commit comments