如何在骆驼路由的交换中从in读取InputStream并将OutputStream写入out?

时间:2018-11-16 10:17:56

标签: apache-camel inputstream outputstream fileinputstream

通常是这样完成的:

try (InputStream is = ...;
     OutputStream os = ...) {
  int b;
  while ((b = is.read()) != -1) {
    // do something with the byte
    os.write(b);
  }
}

RouteBuilder的{​​{1}}中,我有以下内容:

configure()

我阅读了有关from("file:...") ... to("direct:second") from("direct:second") ... .process(exchange -> { try (InputStream is1 = new BufferedInputStream(new FileInputStream(exchange.getIn().getBody(File.class)); InputStream is2 = exchange.getIn().getBody(BufferedInputStream.class); // OutputStream os = ??? ){ int b; while ((b = [is1|is2].read()) != -1) { System.out.print(b); // works // now how to obtain the OutputStream, connect it to 'out' and write to it? } } }) .to("direct:third") from("direct:third") ... getIn()消息翻译器getOut()transform()的文档,博客,教程和答案,但无济于事

更新:我还查看了src/test/java/org/apache/camel/processor,尤其是stream:。那里的StreamCachingInOutTest只会读取Processor

后续问题:

是:

InputStream

与:

相同
exchange.getIn().getBody(BufferedInputStream.class)

如果原始的new BufferedInputStream(new FileInputStream(exchange.getIn().getBody(File.class)) from(...)

更新

我尝试了以下操作:

"file:..."

结果是:

try (...;
     final OutputStream os = new ByteArrayOutputStream()
){
  while (b = is.read() ...) {
    ...
    os.write(b);
  }
  exchange.getOut().setBody(os, ByteArrayOutputStream.class);
}

更新到UPDATE

引发异常是因为我使用Caused by: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: of type: org.apache.commons.io.output.ByteArrayOutputStream on: Message[]. Caused by: No type converter available to convert from type: org.apache.commons.io.output.ByteArrayOutputStream to the required type: java.io.InputStream with value . Exchange[ID-R05377-1542620554174-0-4]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.apache.commons.io.output.ByteArrayOutputStream to the required type: java.io.InputStream with value ] at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:117) at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:333) ... 17 more Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.apache.commons.io.output.ByteArrayOutputStream to the required type: java.io.InputStream with value at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:206) at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:115) ... 18 more 1849 [Camel (camel-1) thread #2 - Multicast] ERROR org.apache.camel.processor.DefaultErrorHandler - Failed delivery for (MessageId: ID-R05377-1542620554174-0-7 on ExchangeId: ID-R05377-1542620554174-0-4). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: <... to(...) file path of direct:third here ...> 而不是org.apache.commons.io.output.ByteArrayOutputStream。它可以与后者配合使用,这似乎也可以解决该问题。

2 个答案:

答案 0 :(得分:0)

import java.io.ByteArrayOutputStream;

// DO NOT USE THIS IN CONJUNCTION WITH CAMEL!
//import org.apache.commons.io.output.ByteArrayOutputStream;

...    

    from(...)
      ... 
      .streamCaching()
      .process(exchange -> {
        ...
        try (final InputStream is = exchange.getIn().getBody(InputStream.class);
             final OutputStream os = new ByteArrayOutputStream(OUTSTREAM_BUFFER)) {
          while (b = is.read() ...) {

            // do something with the byte

            os.write(b);
          }
          exchange.getOut().setBody(os, OutputStream.class);
        }
      })
      ...

答案 1 :(得分:-1)

看看流组件(http://camel.apache.org/stream.html) 示例:

// Route messages to the standard output. 
from("direct:in")
.to("stream:out");