diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/PollingSourceAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/PollingSourceAdapter.java index a91a6e2a2e..2a63965c81 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/adapter/PollingSourceAdapter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/adapter/PollingSourceAdapter.java @@ -47,6 +47,10 @@ public class PollingSourceAdapter extends AbstractSourceAdapter implements this.setConsumerPolicy(ConsumerPolicy.newPollingPolicy(DEFAULT_PERIOD)); } + protected PollableSource getSource() { + return this.source; + } + public boolean isRunning() { return this.running; } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/stream/ByteStreamSource.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/stream/ByteStreamSource.java new file mode 100644 index 0000000000..855e2f0428 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/adapter/stream/ByteStreamSource.java @@ -0,0 +1,87 @@ +/* + * Copyright 2002-2007 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.integration.adapter.stream; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.springframework.integration.MessageDeliveryException; +import org.springframework.integration.adapter.PollableSource; + +/** + * A pollable source for receiving bytes from an {@link InputStream}. + * + * @author Mark Fisher + */ +public class ByteStreamSource implements PollableSource { + + private BufferedInputStream stream; + + private int bytesPerMessage = 1024; + + private boolean shouldTruncate = true; + + + public ByteStreamSource(InputStream stream) { + if (stream instanceof BufferedInputStream) { + this.stream = (BufferedInputStream) stream; + } + else { + this.stream = new BufferedInputStream(stream); + } + } + + + public void setBytesPerMessage(int bytesPerMessage) { + this.bytesPerMessage = bytesPerMessage; + } + + public void setShouldTruncate(boolean shouldTruncate) { + this.shouldTruncate = shouldTruncate; + } + + public Collection poll(int limit) { + List results = new ArrayList(); + while (results.size() < limit) { + try { + int bytesAvailable = stream.available(); + if (bytesAvailable == 0) { + return results; + } + byte[] bytes = new byte[bytesPerMessage]; + int bytesRead = stream.read(bytes, 0, bytes.length); + if (!this.shouldTruncate) { + results.add(bytes); + } + else { + byte[] result = new byte[bytesRead]; + System.arraycopy(bytes, 0, result, 0, result.length); + results.add(result); + } + } + catch (IOException e) { + throw new MessageDeliveryException("IO failure occurred in adapter", e); + } + } + return results; + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/adapter/stream/ByteStreamSourceAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/adapter/stream/ByteStreamSourceAdapter.java new file mode 100644 index 0000000000..97fa46fcfe --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/adapter/stream/ByteStreamSourceAdapter.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2007 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.integration.adapter.stream; + +import java.io.InputStream; + +import org.springframework.integration.adapter.PollingSourceAdapter; + +/** + * A polling source adapter that wraps a {@link ByteStreamSource}. + * + * @author Mark Fisher + */ +public class ByteStreamSourceAdapter extends PollingSourceAdapter { + + public ByteStreamSourceAdapter(InputStream stream) { + super(new ByteStreamSource(stream)); + } + + + public void setBytesPerMessage(int bytesPerMessage) { + ((ByteStreamSource) this.getSource()).setBytesPerMessage(bytesPerMessage); + } + + public void setShouldTruncate(boolean shouldTruncate) { + ((ByteStreamSource) this.getSource()).setShouldTruncate(shouldTruncate); + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/stream/ByteStreamSourceAdapterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/adapter/stream/ByteStreamSourceAdapterTests.java new file mode 100644 index 0000000000..c5dbfb70c3 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/stream/ByteStreamSourceAdapterTests.java @@ -0,0 +1,169 @@ +/* + * Copyright 2002-2007 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.integration.adapter.stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.io.ByteArrayInputStream; + +import org.junit.Test; + +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.channel.SimpleChannel; +import org.springframework.integration.message.Message; + +/** + * @author Mark Fisher + */ +public class ByteStreamSourceAdapterTests { + + @Test + public void testEndOfStream() { + byte[] bytes = new byte[] {1,2,3}; + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); + MessageChannel channel = new SimpleChannel(); + ByteStreamSourceAdapter adapter = new ByteStreamSourceAdapter(stream); + adapter.setChannel(channel); + adapter.start(); + int count = adapter.dispatch(); + assertEquals(1, count); + Message message1 = channel.receive(0); + byte[] payload = (byte[]) message1.getPayload(); + assertEquals(3, payload.length); + assertEquals(1, payload[0]); + assertEquals(2, payload[1]); + assertEquals(3, payload[2]); + Message message2 = channel.receive(0); + assertNull(message2); + adapter.dispatch(); + Message message3 = channel.receive(0); + assertNull(message3); + } + + @Test + public void testEndOfStreamWithMaxMessagesPerTask() throws Exception { + byte[] bytes = new byte[] {0,1,2,3,4,5,6,7}; + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); + MessageChannel channel = new SimpleChannel(); + ByteStreamSourceAdapter adapter = new ByteStreamSourceAdapter(stream); + adapter.setChannel(channel); + adapter.setBytesPerMessage(8); + adapter.setMaxMessagesPerTask(5); + adapter.start(); + int count = adapter.dispatch(); + assertEquals(1, count); + Message message1 = channel.receive(0); + assertEquals(8, ((byte[]) message1.getPayload()).length); + Message message2 = channel.receive(0); + assertNull(message2); + } + + @Test + public void testMultipleMessagesWithSingleMessagePerTask() { + byte[] bytes = new byte[] {0,1,2,3,4,5,6,7}; + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); + MessageChannel channel = new SimpleChannel(); + ByteStreamSourceAdapter adapter = new ByteStreamSourceAdapter(stream); + adapter.setBytesPerMessage(4); + adapter.setMaxMessagesPerTask(1); + adapter.setChannel(channel); + adapter.start(); + int count = adapter.dispatch(); + assertEquals(1, count); + Message message1 = channel.receive(0); + byte[] bytes1 = (byte[]) message1.getPayload(); + assertEquals(4, bytes1.length); + assertEquals(0, bytes1[0]); + Message message2 = channel.receive(0); + assertNull(message2); + adapter.dispatch(); + Message message3 = channel.receive(0); + byte[] bytes3 = (byte[]) message3.getPayload(); + assertEquals(4, bytes3.length); + assertEquals(4, bytes3[0]); + } + + @Test + public void testLessThanMaxMessagesAvailable() { + byte[] bytes = new byte[] {0,1,2,3,4,5,6,7}; + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); + MessageChannel channel = new SimpleChannel(); + ByteStreamSourceAdapter adapter = new ByteStreamSourceAdapter(stream); + adapter.setChannel(channel); + adapter.setBytesPerMessage(4); + adapter.setMaxMessagesPerTask(5); + adapter.start(); + int count = adapter.dispatch(); + assertEquals(2, count); + Message message1 = channel.receive(0); + byte[] bytes1 = (byte[]) message1.getPayload(); + assertEquals(4, bytes1.length); + assertEquals(0, bytes1[0]); + Message message2 = channel.receive(0); + byte[] bytes2 = (byte[]) message2.getPayload(); + assertEquals(4, bytes2.length); + assertEquals(4, bytes2[0]); + Message message3 = channel.receive(0); + assertNull(message3); + } + + @Test + public void testByteArrayIsTruncated() { + byte[] bytes = new byte[] {0,1,2,3,4,5}; + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); + MessageChannel channel = new SimpleChannel(); + ByteStreamSourceAdapter adapter = new ByteStreamSourceAdapter(stream); + adapter.setBytesPerMessage(4); + adapter.setMaxMessagesPerTask(1); + adapter.setChannel(channel); + adapter.start(); + int count = adapter.dispatch(); + assertEquals(1, count); + Message message1 = channel.receive(0); + assertEquals(4, ((byte[]) message1.getPayload()).length); + Message message2 = channel.receive(0); + assertNull(message2); + adapter.dispatch(); + Message message3 = channel.receive(0); + assertEquals(2, ((byte[]) message3.getPayload()).length); + } + + @Test + public void testByteArrayIsNotTruncated() { + byte[] bytes = new byte[] {0,1,2,3,4,5}; + ByteArrayInputStream stream = new ByteArrayInputStream(bytes); + MessageChannel channel = new SimpleChannel(); + ByteStreamSourceAdapter adapter = new ByteStreamSourceAdapter(stream); + adapter.setBytesPerMessage(4); + adapter.setShouldTruncate(false); + adapter.setMaxMessagesPerTask(1); + adapter.setChannel(channel); + adapter.start(); + int count = adapter.dispatch(); + assertEquals(1, count); + Message message1 = channel.receive(0); + assertEquals(4, ((byte[]) message1.getPayload()).length); + Message message2 = channel.receive(0); + assertNull(message2); + adapter.dispatch(); + Message message3 = channel.receive(0); + assertEquals(4, ((byte[]) message3.getPayload()).length); + assertEquals(0, ((byte[]) message3.getPayload())[3]); + } + +}