Use ByteBuffer instead of byte[]
This commit is contained in:
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.reactive.io;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
import reactor.rx.Stream;
|
||||
import reactor.rx.Streams;
|
||||
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ByteArrayPublisherInputStreamTests {
|
||||
|
||||
|
||||
private ByteArrayPublisherInputStream is;
|
||||
|
||||
@Before
|
||||
public void createStream() {
|
||||
Stream<byte[]> stream =
|
||||
Streams.just(new byte[]{'a', 'b', 'c'}, new byte[]{'d', 'e'});
|
||||
|
||||
is = new ByteArrayPublisherInputStream(stream);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reactor() throws Exception {
|
||||
assertEquals(3, is.available());
|
||||
|
||||
int ch = is.read();
|
||||
assertEquals('a', ch);
|
||||
ch = is.read();
|
||||
assertEquals('b', ch);
|
||||
ch = is.read();
|
||||
assertEquals('c', ch);
|
||||
|
||||
assertEquals(2, is.available());
|
||||
ch = is.read();
|
||||
assertEquals('d', ch);
|
||||
ch = is.read();
|
||||
assertEquals('e', ch);
|
||||
|
||||
ch = is.read();
|
||||
assertEquals(-1, ch);
|
||||
|
||||
assertEquals(0, is.available());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copy() throws Exception {
|
||||
ByteArrayPublisherOutputStream os = new ByteArrayPublisherOutputStream();
|
||||
|
||||
FileCopyUtils.copy(is, os);
|
||||
|
||||
Publisher<byte[]> publisher = os.toByteArrayPublisher();
|
||||
List<byte[]> result = new ArrayList<>();
|
||||
AtomicBoolean complete = new AtomicBoolean();
|
||||
|
||||
publisher.subscribe(new Subscriber<byte[]>() {
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription s) {
|
||||
s.request(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(byte[] bytes) {
|
||||
result.add(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
fail(t.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
complete.set(true);
|
||||
}
|
||||
});
|
||||
|
||||
while (!complete.get()) {
|
||||
|
||||
}
|
||||
assertArrayEquals(result.get(0), new byte[]{'a', 'b', 'c'});
|
||||
assertArrayEquals(result.get(1), new byte[]{'d', 'e'});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.rx.Streams;
|
||||
|
||||
import org.springframework.http.RequestEntity;
|
||||
@@ -97,7 +98,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
|
||||
|
||||
@Override
|
||||
public Publisher<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
return response.writeWith(Streams.just("foo".getBytes(UTF_8)));
|
||||
return response.writeWith(Streams.just(Buffer.wrap("foo").byteBuffer()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,7 +106,7 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
|
||||
|
||||
@Override
|
||||
public Publisher<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
return response.writeWith(Streams.just("bar".getBytes(UTF_8)));
|
||||
return response.writeWith(Streams.just(Buffer.wrap("bar").byteBuffer()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.reactive.web.http;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Random;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -23,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.reactivestreams.Subscriber;
|
||||
import org.reactivestreams.Subscription;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.rx.Streams;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -41,7 +43,7 @@ public class RandomHandler implements HttpHandler {
|
||||
@Override
|
||||
public Publisher<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
|
||||
request.getBody().subscribe(new Subscriber<byte[]>() {
|
||||
request.getBody().subscribe(new Subscriber<ByteBuffer>() {
|
||||
private Subscription s;
|
||||
|
||||
private int requestSize = 0;
|
||||
@@ -53,8 +55,8 @@ public class RandomHandler implements HttpHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(byte[] bytes) {
|
||||
requestSize += bytes.length;
|
||||
public void onNext(ByteBuffer bytes) {
|
||||
requestSize += new Buffer(bytes).limit();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,7 +73,7 @@ public class RandomHandler implements HttpHandler {
|
||||
});
|
||||
|
||||
response.getHeaders().setContentLength(RESPONSE_SIZE);
|
||||
return response.writeWith(Streams.<byte[]>just(randomBytes()));
|
||||
return response.writeWith(Streams.just(ByteBuffer.wrap(randomBytes())));
|
||||
}
|
||||
|
||||
private byte[] randomBytes() {
|
||||
|
||||
@@ -23,13 +23,14 @@ import javax.xml.bind.Unmarshaller;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.rx.Streams;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.reactive.io.ByteArrayPublisherInputStream;
|
||||
import org.springframework.reactive.io.ByteArrayPublisherOutputStream;
|
||||
import org.springframework.reactive.io.BufferOutputStream;
|
||||
import org.springframework.reactive.io.ByteBufferPublisherInputStream;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import reactor.io.buffer.Buffer;
|
||||
import reactor.rx.Streams;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -48,7 +49,7 @@ public class XmlHandler implements HttpHandler {
|
||||
|
||||
Runnable r = () -> {
|
||||
try {
|
||||
ByteArrayPublisherInputStream bis = new ByteArrayPublisherInputStream(request.getBody());
|
||||
ByteBufferPublisherInputStream bis = new ByteBufferPublisherInputStream(request.getBody());
|
||||
|
||||
XmlHandlerIntegrationTests.Person johnDoe =
|
||||
(XmlHandlerIntegrationTests.Person) unmarshaller.unmarshal(bis);
|
||||
@@ -66,11 +67,13 @@ public class XmlHandler implements HttpHandler {
|
||||
|
||||
response.getHeaders().setContentType(MediaType.APPLICATION_XML);
|
||||
XmlHandlerIntegrationTests.Person janeDoe = new XmlHandlerIntegrationTests.Person("Jane Doe");
|
||||
ByteArrayPublisherOutputStream bos = new ByteArrayPublisherOutputStream();
|
||||
Buffer buffer = new Buffer();
|
||||
BufferOutputStream bos = new BufferOutputStream(buffer);
|
||||
marshaller.marshal(janeDoe, bos);
|
||||
bos.close();
|
||||
buffer.flip();
|
||||
|
||||
return response.writeWith(bos.toByteArrayPublisher());
|
||||
return response.writeWith(Streams.just(buffer.byteBuffer()));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.error(ex, ex);
|
||||
|
||||
Reference in New Issue
Block a user