简体   繁体   English

Java流的奇怪行为

[英]Strange behavior of java streaming

I've the necessity to share a streaming of data between two instances as below: 我必须在两个实例之间共享数据流,如下所示:

// get EClasses which should be connected
    final uk.man.xman.xcore.Parameter source = getParameter(sourceAnchor);
    final uk.man.xman.xcore.Parameter target = getParameter(targetAnchor);

    // Set data channels

    //Output stream
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream));
    source.setOutputStream(dataOutputStream);

    //Input stream
    DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
    target.setInputStream(inpuDataStream);

Everything works ok if I write, during those lines of code. 如果我在这些代码行中编写,一切正常。 Strangely, when I need to use the data channel to write something in another class, like here: 奇怪的是,当我需要使用数据通道在另一个类中编写内容时,例如:

    DataOutputStream dataOutputStream = (DataOutputStream) inputParameter.getOutputStream();
            System.out.println("WRITE:" + attributes.getValue("value"));
            dataOutputStream.writeUTF(attributes.getValue("value"));
            dataOutputStream.flush();

I am not able to read, and I really do not know why. 我看不懂,而且我真的不知道为什么。 Am I missing something? 我想念什么吗?

Thanks for your time 谢谢你的时间

Not sure if that's what you're asking, but you're creating an InputStream that reads from an empty byte array. 不知道这是否是您要的内容,但是您正在创建一个从空字节数组读取的InputStream。 That doesn't make much sense: 那没有多大意义:

// create an Output stream that will write in memory
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
...

// transform what has been written to the output stream into a byte array. 
// Since othing has been written yet, outputStream.toByteArray() returns 
// an empty array
DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM