Add byte[] encoder and decoder
This commit adds a ByteArrayDecoder and ByteArrayEncoder.
This commit is contained in:
parent
3726c6f18d
commit
08931950f4
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* Decoder for {@code byte} arrays.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ByteArrayDecoder extends AbstractDecoder<byte[]> {
|
||||
|
||||
public ByteArrayDecoder() {
|
||||
super(MimeTypeUtils.ALL);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean canDecode(ResolvableType elementType, MimeType mimeType) {
|
||||
Class<?> clazz = elementType.getRawClass();
|
||||
return (super.canDecode(elementType, mimeType) && byte[].class == clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<byte[]> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType,
|
||||
MimeType mimeType, Map<String, Object> hints) {
|
||||
|
||||
return Flux.from(inputStream).map((dataBuffer) -> {
|
||||
byte[] result = new byte[dataBuffer.readableByteCount()];
|
||||
dataBuffer.read(result);
|
||||
DataBufferUtils.release(dataBuffer);
|
||||
return result ;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
/**
|
||||
* Encoder for {@code byte} arrays.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ByteArrayEncoder extends AbstractEncoder<byte[]> {
|
||||
|
||||
public ByteArrayEncoder() {
|
||||
super(MimeTypeUtils.ALL);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean canEncode(ResolvableType elementType, MimeType mimeType) {
|
||||
Class<?> clazz = elementType.getRawClass();
|
||||
return (super.canEncode(elementType, mimeType) && byte[].class.isAssignableFrom(clazz));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<DataBuffer> encode(Publisher<? extends byte[]> inputStream,
|
||||
DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType,
|
||||
Map<String, Object> hints) {
|
||||
|
||||
return Flux.from(inputStream).map(bufferFactory::wrap);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -52,4 +52,11 @@ public interface DataBufferFactory {
|
|||
*/
|
||||
DataBuffer wrap(ByteBuffer byteBuffer);
|
||||
|
||||
/**
|
||||
* Wrap the given {@code byte} array in a {@code DataBuffer}.
|
||||
* @param bytes the byte array to wrap
|
||||
* @return the wrapped buffer
|
||||
*/
|
||||
DataBuffer wrap(byte[] bytes);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,6 +94,12 @@ public class DefaultDataBufferFactory implements DataBufferFactory {
|
|||
return new DefaultDataBuffer(sliced, 0, byteBuffer.remaining(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBuffer wrap(byte[] bytes) {
|
||||
ByteBuffer wrapper = ByteBuffer.wrap(bytes);
|
||||
return new DefaultDataBuffer(wrapper, 0, bytes.length, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultDataBufferFactory (preferDirect=" + this.preferDirect + ")";
|
||||
|
|
|
|||
|
|
@ -68,6 +68,12 @@ public class NettyDataBufferFactory implements DataBufferFactory {
|
|||
return new NettyDataBuffer(byteBuf, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBuffer wrap(byte[] bytes) {
|
||||
ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
|
||||
return new NettyDataBuffer(byteBuf, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the given Netty {@link ByteBuf} in a {@code NettyDataBuffer}.
|
||||
* @param byteBuf the Netty byte buffer to wrap
|
||||
|
|
|
|||
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.tests.TestSubscriber;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ByteArrayDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private final ByteArrayDecoder decoder = new ByteArrayDecoder();
|
||||
|
||||
@Test
|
||||
public void canDecode() {
|
||||
assertTrue(this.decoder.canDecode(ResolvableType.forClass(byte[].class),
|
||||
MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.decoder.canDecode(ResolvableType.forClass(Integer.class),
|
||||
MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.decoder.canDecode(ResolvableType.forClass(byte[].class),
|
||||
MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode() {
|
||||
DataBuffer fooBuffer = stringBuffer("foo");
|
||||
DataBuffer barBuffer = stringBuffer("bar");
|
||||
Flux<DataBuffer> source = Flux.just(fooBuffer, barBuffer);
|
||||
Flux<byte[]> output = this.decoder.decode(source,
|
||||
ResolvableType.forClassWithGenerics(Publisher.class, byte[].class),
|
||||
null, Collections.emptyMap());
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertNoError()
|
||||
.assertComplete()
|
||||
.assertValuesWith(bytes -> assertArrayEquals("foo".getBytes(), bytes),
|
||||
bytes -> assertArrayEquals("bar".getBytes(), bytes));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.codec;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.tests.TestSubscriber;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ByteArrayEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private ByteArrayEncoder encoder;
|
||||
|
||||
@Before
|
||||
public void createEncoder() {
|
||||
this.encoder = new ByteArrayEncoder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(byte[].class),
|
||||
MimeTypeUtils.TEXT_PLAIN));
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(Integer.class),
|
||||
MimeTypeUtils.TEXT_PLAIN));
|
||||
assertTrue(this.encoder.canEncode(ResolvableType.forClass(byte[].class),
|
||||
MimeTypeUtils.APPLICATION_JSON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] barBytes = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
Flux<byte[]> source = Flux.just(fooBytes, barBytes);
|
||||
|
||||
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory,
|
||||
ResolvableType.forClassWithGenerics(Publisher.class, ByteBuffer.class),
|
||||
null, Collections.emptyMap());
|
||||
TestSubscriber
|
||||
.subscribe(output)
|
||||
.assertValuesWith(b -> {
|
||||
byte[] buf = new byte[3];
|
||||
b.read(buf);
|
||||
assertArrayEquals(fooBytes, buf);
|
||||
}, b -> {
|
||||
byte[] buf = new byte[3];
|
||||
b.read(buf);
|
||||
assertArrayEquals(barBytes, buf);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -28,6 +28,8 @@ import org.springframework.beans.factory.BeanInitializationException;
|
|||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.codec.ByteArrayDecoder;
|
||||
import org.springframework.core.codec.ByteArrayEncoder;
|
||||
import org.springframework.core.codec.ByteBufferDecoder;
|
||||
import org.springframework.core.codec.ByteBufferEncoder;
|
||||
import org.springframework.core.codec.CharSequenceEncoder;
|
||||
|
|
@ -307,6 +309,7 @@ public class WebReactiveConfigurationSupport implements ApplicationContextAware
|
|||
* {@link #configureMessageReaders(List)}.
|
||||
*/
|
||||
protected final void addDefaultHttpMessageReaders(List<HttpMessageReader<?>> readers) {
|
||||
readers.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
|
||||
readers.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
|
||||
readers.add(new DecoderHttpMessageReader<>(new StringDecoder()));
|
||||
readers.add(new DecoderHttpMessageReader<>(new ResourceDecoder()));
|
||||
|
|
@ -444,6 +447,7 @@ public class WebReactiveConfigurationSupport implements ApplicationContextAware
|
|||
*/
|
||||
protected final void addDefaultHttpMessageWriters(List<HttpMessageWriter<?>> writers) {
|
||||
List<Encoder<?>> sseDataEncoders = new ArrayList<>();
|
||||
writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
|
||||
writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
|
||||
writers.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
|
||||
writers.add(new ResourceHttpMessageWriter());
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ import java.util.function.Supplier;
|
|||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.codec.ByteArrayDecoder;
|
||||
import org.springframework.core.codec.ByteArrayEncoder;
|
||||
import org.springframework.core.codec.ByteBufferDecoder;
|
||||
import org.springframework.core.codec.ByteBufferEncoder;
|
||||
import org.springframework.core.codec.CharSequenceEncoder;
|
||||
|
|
@ -65,8 +67,10 @@ class DefaultHandlerStrategiesBuilder implements HandlerStrategies.Builder {
|
|||
private final List<ViewResolver> viewResolvers = new ArrayList<>();
|
||||
|
||||
public void defaultConfiguration() {
|
||||
messageReader(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
|
||||
messageReader(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
|
||||
messageReader(new DecoderHttpMessageReader<>(new StringDecoder()));
|
||||
messageWriter(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
|
||||
messageWriter(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
|
||||
messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
|
||||
if (jaxb2Present) {
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import org.springframework.beans.factory.BeanFactoryAware;
|
|||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.codec.ByteArrayDecoder;
|
||||
import org.springframework.core.codec.ByteBufferDecoder;
|
||||
import org.springframework.core.codec.StringDecoder;
|
||||
import org.springframework.http.codec.DecoderHttpMessageReader;
|
||||
|
|
@ -75,6 +76,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, BeanFactory
|
|||
|
||||
|
||||
public RequestMappingHandlerAdapter() {
|
||||
this.messageReaders.add(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
|
||||
this.messageReaders.add(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
|
||||
this.messageReaders.add(new DecoderHttpMessageReader<>(new StringDecoder()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ public class DelegatingWebReactiveConfigurationTests {
|
|||
verify(webReactiveConfigurer).addArgumentResolvers(any());
|
||||
|
||||
assertSame(formatterRegistry.getValue(), initializerConversionService);
|
||||
assertEquals(5, readers.getValue().size());
|
||||
assertEquals(6, readers.getValue().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -145,8 +145,9 @@ public class WebReactiveConfigurationSupportTests {
|
|||
assertNotNull(adapter);
|
||||
|
||||
List<HttpMessageReader<?>> readers = adapter.getMessageReaders();
|
||||
assertEquals(5, readers.size());
|
||||
assertEquals(6, readers.size());
|
||||
|
||||
assertHasMessageReader(readers, byte[].class, APPLICATION_OCTET_STREAM);
|
||||
assertHasMessageReader(readers, ByteBuffer.class, APPLICATION_OCTET_STREAM);
|
||||
assertHasMessageReader(readers, String.class, TEXT_PLAIN);
|
||||
assertHasMessageReader(readers, Resource.class, IMAGE_PNG);
|
||||
|
|
@ -194,8 +195,9 @@ public class WebReactiveConfigurationSupportTests {
|
|||
assertEquals(0, handler.getOrder());
|
||||
|
||||
List<HttpMessageWriter<?>> writers = handler.getMessageWriters();
|
||||
assertEquals(6, writers.size());
|
||||
assertEquals(7, writers.size());
|
||||
|
||||
assertHasMessageWriter(writers, byte[].class, APPLICATION_OCTET_STREAM);
|
||||
assertHasMessageWriter(writers, ByteBuffer.class, APPLICATION_OCTET_STREAM);
|
||||
assertHasMessageWriter(writers, String.class, TEXT_PLAIN);
|
||||
assertHasMessageWriter(writers, Resource.class, IMAGE_PNG);
|
||||
|
|
@ -219,8 +221,9 @@ public class WebReactiveConfigurationSupportTests {
|
|||
assertEquals(100, handler.getOrder());
|
||||
|
||||
List<HttpMessageWriter<?>> writers = handler.getMessageWriters();
|
||||
assertEquals(6, writers.size());
|
||||
assertEquals(7, writers.size());
|
||||
|
||||
assertHasMessageWriter(writers, byte[].class, APPLICATION_OCTET_STREAM);
|
||||
assertHasMessageWriter(writers, ByteBuffer.class, APPLICATION_OCTET_STREAM);
|
||||
assertHasMessageWriter(writers, String.class, TEXT_PLAIN);
|
||||
assertHasMessageWriter(writers, Resource.class, IMAGE_PNG);
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@ import java.util.function.Supplier;
|
|||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.codec.ByteArrayDecoder;
|
||||
import org.springframework.core.codec.ByteArrayEncoder;
|
||||
import org.springframework.core.codec.ByteBufferDecoder;
|
||||
import org.springframework.core.codec.ByteBufferEncoder;
|
||||
import org.springframework.core.codec.CharSequenceEncoder;
|
||||
|
|
@ -65,8 +67,10 @@ class DefaultWebClientStrategiesBuilder implements WebClientStrategies.Builder {
|
|||
|
||||
|
||||
public void defaultConfiguration() {
|
||||
messageReader(new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
|
||||
messageReader(new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
|
||||
messageReader(new DecoderHttpMessageReader<>(new StringDecoder(false)));
|
||||
messageWriter(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
|
||||
messageWriter(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
|
||||
messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
|
||||
if (jaxb2Present) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue