Skip to content

Commit 1177f5c

Browse files
committed
ByteBufferConverter explicitly declares applicability to byte[]
Includes an optimization for simple ByteBuffer duplication. Issue: SPR-13056 (cherry picked from commit 792b7b9)
1 parent bd05e3a commit 1177f5c

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/ByteBufferConverter.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,10 +26,11 @@
2626
import org.springframework.core.convert.converter.ConditionalGenericConverter;
2727

2828
/**
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[]}.
3131
*
3232
* @author Phillip Webb
33+
* @author Juergen Hoeller
3334
* @since 4.0
3435
*/
3536
final class ByteBufferConverter implements ConditionalGenericConverter {
@@ -39,15 +40,18 @@ final class ByteBufferConverter implements ConditionalGenericConverter {
3940
private static final TypeDescriptor BYTE_ARRAY_TYPE = TypeDescriptor.valueOf(byte[].class);
4041

4142
private static final Set<ConvertiblePair> CONVERTIBLE_PAIRS;
43+
4244
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));
4448
convertiblePairs.add(new ConvertiblePair(ByteBuffer.class, Object.class));
4549
convertiblePairs.add(new ConvertiblePair(Object.class, ByteBuffer.class));
4650
CONVERTIBLE_PAIRS = Collections.unmodifiableSet(convertiblePairs);
4751
}
4852

4953

50-
private ConversionService conversionService;
54+
private final ConversionService conversionService;
5155

5256

5357
public ByteBufferConverter(ConversionService conversionService) {
@@ -62,32 +66,31 @@ public Set<ConvertiblePair> getConvertibleTypes() {
6266

6367
@Override
6468
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
69+
boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE);
6570
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));
7072
}
71-
return false;
73+
return (byteBufferTarget && matchesToByteBuffer(sourceType));
7274
}
7375

7476
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));
7779
}
7880

7981
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));
8284
}
8385

8486
@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));
8992
}
90-
if (targetType.isAssignableTo(BYTE_BUFFER_TYPE)) {
93+
if (byteBufferTarget) {
9194
return convertToByteBuffer(source, sourceType);
9295
}
9396
// Should not happen
@@ -97,19 +100,20 @@ public Object convert(Object source, TypeDescriptor sourceType,
97100
private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) {
98101
byte[] bytes = new byte[source.remaining()];
99102
source.get(bytes);
103+
100104
if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) {
101105
return bytes;
102106
}
103107
return this.conversionService.convert(bytes, BYTE_ARRAY_TYPE, targetType);
104108
}
105109

106110
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+
109114
ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
110115
byteBuffer.put(bytes);
111-
byteBuffer.rewind();
112-
return byteBuffer;
116+
return byteBuffer.rewind();
113117
}
114118

115119
}

spring-core/src/test/java/org/springframework/core/convert/support/ByteBufferConverterTests.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* Tests for {@link ByteBufferConverter}.
3131
*
3232
* @author Phillip Webb
33+
* @author Juergen Hoeller
3334
*/
3435
public class ByteBufferConverterTests {
3536

@@ -38,8 +39,7 @@ public class ByteBufferConverterTests {
3839

3940
@Before
4041
public void setup() {
41-
this.conversionService = new GenericConversionService();
42-
this.conversionService.addConverter(new ByteBufferConverter(conversionService));
42+
this.conversionService = new DefaultConversionService();
4343
this.conversionService.addConverter(new ByteArrayToOtherTypeConverter());
4444
this.conversionService.addConverter(new OtherTypeToByteArrayConverter());
4545
}
@@ -49,35 +49,46 @@ public void setup() {
4949
public void byteArrayToByteBuffer() throws Exception {
5050
byte[] bytes = new byte[] { 1, 2, 3 };
5151
ByteBuffer convert = this.conversionService.convert(bytes, ByteBuffer.class);
52-
assertThat(bytes, not(sameInstance(convert.array())));
53-
assertThat(bytes, equalTo(convert.array()));
52+
assertThat(convert.array(), not(sameInstance(bytes)));
53+
assertThat(convert.array(), equalTo(bytes));
5454
}
5555

5656
@Test
5757
public void byteBufferToByteArray() throws Exception {
5858
byte[] bytes = new byte[] { 1, 2, 3 };
5959
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
6060
byte[] convert = this.conversionService.convert(byteBuffer, byte[].class);
61-
assertThat(bytes, not(sameInstance(convert)));
62-
assertThat(bytes, equalTo(convert));
61+
assertThat(convert, not(sameInstance(bytes)));
62+
assertThat(convert, equalTo(bytes));
6363
}
6464

6565
@Test
6666
public void byteBufferToOtherType() throws Exception {
6767
byte[] bytes = new byte[] { 1, 2, 3 };
6868
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
6969
OtherType convert = this.conversionService.convert(byteBuffer, OtherType.class);
70-
assertThat(bytes, not(sameInstance(convert.bytes)));
71-
assertThat(bytes, equalTo(convert.bytes));
70+
assertThat(convert.bytes, not(sameInstance(bytes)));
71+
assertThat(convert.bytes, equalTo(bytes));
7272
}
7373

7474
@Test
7575
public void otherTypeToByteBuffer() throws Exception {
7676
byte[] bytes = new byte[] { 1, 2, 3 };
7777
OtherType otherType = new OtherType(bytes);
7878
ByteBuffer convert = this.conversionService.convert(otherType, ByteBuffer.class);
79-
assertThat(bytes, not(sameInstance(convert.array())));
80-
assertThat(bytes, equalTo(convert.array()));
79+
assertThat(convert.array(), not(sameInstance(bytes)));
80+
assertThat(convert.array(), equalTo(bytes));
81+
}
82+
83+
@Test
84+
public void byteBufferToByteBuffer() throws Exception {
85+
byte[] bytes = new byte[] { 1, 2, 3 };
86+
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
87+
ByteBuffer convert = this.conversionService.convert(byteBuffer, ByteBuffer.class);
88+
assertThat(convert, not(sameInstance(byteBuffer.rewind())));
89+
assertThat(convert, equalTo(byteBuffer.rewind()));
90+
assertThat(convert, equalTo(ByteBuffer.wrap(bytes)));
91+
assertThat(convert.array(), equalTo(bytes));
8192
}
8293

8394

0 commit comments

Comments
 (0)