|
20 | 20 | import java.io.StringReader;
|
21 | 21 | import java.lang.reflect.AnnotatedElement;
|
22 | 22 | import java.lang.reflect.Method;
|
| 23 | +import java.nio.ByteBuffer; |
23 | 24 | import java.nio.charset.Charset;
|
24 | 25 | import java.nio.charset.StandardCharsets;
|
25 | 26 | import java.util.ArrayList;
|
|
68 | 69 | import org.springframework.core.Ordered;
|
69 | 70 | import org.springframework.core.annotation.AnnotatedElementUtils;
|
70 | 71 | import org.springframework.core.annotation.AnnotationUtils;
|
| 72 | +import org.springframework.core.convert.TypeDescriptor; |
| 73 | +import org.springframework.core.convert.converter.ConditionalGenericConverter; |
71 | 74 | import org.springframework.core.convert.converter.Converter;
|
72 | 75 | import org.springframework.core.convert.converter.GenericConverter;
|
73 | 76 | import org.springframework.core.log.LogAccessor;
|
@@ -1081,6 +1084,7 @@ private MessageHandlerMethodFactory createDefaultMessageHandlerMethodFactory() {
|
1081 | 1084 | defaultFactory.setBeanFactory(KafkaListenerAnnotationBeanPostProcessor.this.beanFactory);
|
1082 | 1085 | this.defaultFormattingConversionService.addConverter(
|
1083 | 1086 | new BytesToStringConverter(KafkaListenerAnnotationBeanPostProcessor.this.charset));
|
| 1087 | + this.defaultFormattingConversionService.addConverter(new BytesToNumberConverter()); |
1084 | 1088 | defaultFactory.setConversionService(this.defaultFormattingConversionService);
|
1085 | 1089 | GenericMessageConverter messageConverter = new GenericMessageConverter(this.defaultFormattingConversionService);
|
1086 | 1090 | defaultFactory.setMessageConverter(messageConverter);
|
@@ -1165,4 +1169,62 @@ public interface AnnotationEnhancer extends BiFunction<Map<String, Object>, Anno
|
1165 | 1169 |
|
1166 | 1170 | }
|
1167 | 1171 |
|
| 1172 | + private final class BytesToNumberConverter implements ConditionalGenericConverter { |
| 1173 | + |
| 1174 | + BytesToNumberConverter() { |
| 1175 | + } |
| 1176 | + |
| 1177 | + @Override |
| 1178 | + @Nullable |
| 1179 | + public Set<ConvertiblePair> getConvertibleTypes() { |
| 1180 | + HashSet<ConvertiblePair> pairs = new HashSet<>(); |
| 1181 | + pairs.add(new ConvertiblePair(byte[].class, long.class)); |
| 1182 | + pairs.add(new ConvertiblePair(byte[].class, int.class)); |
| 1183 | + pairs.add(new ConvertiblePair(byte[].class, short.class)); |
| 1184 | + pairs.add(new ConvertiblePair(byte[].class, byte.class)); |
| 1185 | + pairs.add(new ConvertiblePair(byte[].class, Long.class)); |
| 1186 | + pairs.add(new ConvertiblePair(byte[].class, Integer.class)); |
| 1187 | + pairs.add(new ConvertiblePair(byte[].class, Short.class)); |
| 1188 | + pairs.add(new ConvertiblePair(byte[].class, Byte.class)); |
| 1189 | + return pairs; |
| 1190 | + } |
| 1191 | + |
| 1192 | + @Override |
| 1193 | + @Nullable |
| 1194 | + public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
| 1195 | + byte[] bytes = (byte[]) source; |
| 1196 | + if (targetType.getType().equals(long.class) || targetType.getType().equals(Long.class)) { |
| 1197 | + Assert.state(bytes.length >= 8, "At least 8 bytes needed to convert a byte[] to a long"); |
| 1198 | + return ByteBuffer.wrap(bytes).getLong(); |
| 1199 | + } |
| 1200 | + else if (targetType.getType().equals(int.class) || targetType.getType().equals(Integer.class)) { |
| 1201 | + Assert.state(bytes.length >= 4, "At least 4 bytes needed to convert a byte[] to an integer"); |
| 1202 | + return ByteBuffer.wrap(bytes).getInt(); |
| 1203 | + } |
| 1204 | + else if (targetType.getType().equals(short.class) || targetType.getType().equals(Short.class)) { |
| 1205 | + Assert.state(bytes.length >= 2, "At least 2 bytes needed to convert a byte[] to a short"); |
| 1206 | + return ByteBuffer.wrap(bytes).getShort(); |
| 1207 | + } |
| 1208 | + else if (targetType.getType().equals(byte.class) || targetType.getType().equals(Byte.class)) { |
| 1209 | + Assert.state(bytes.length >= 1, "At least 1 byte needed to convert a byte[] to a byte"); |
| 1210 | + return ByteBuffer.wrap(bytes).get(); |
| 1211 | + } |
| 1212 | + return null; |
| 1213 | + } |
| 1214 | + |
| 1215 | + @Override |
| 1216 | + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { |
| 1217 | + if (sourceType.getType().equals(byte[].class)) { |
| 1218 | + Class<?> target = targetType.getType(); |
| 1219 | + return target.equals(long.class) || target.equals(int.class) || target.equals(short.class) |
| 1220 | + || target.equals(byte.class) || target.equals(Long.class) || target.equals(Integer.class) |
| 1221 | + || target.equals(Short.class) || target.equals(Byte.class); |
| 1222 | + } |
| 1223 | + else { |
| 1224 | + return false; |
| 1225 | + } |
| 1226 | + } |
| 1227 | + |
| 1228 | + } |
| 1229 | + |
1168 | 1230 | }
|
0 commit comments