4

I have an outputstream, to which the client A is writing , I need to forward it in byte chuncks to client B.

I'd like to connect the output stream of client A with the output stream of client B. Is that possible? What are ways to do that? I don't need to fork/clone I rather need to take some of the data from stream A and move it to stream B(i.e the data don't stay in stream A)

Note:A and B are processes and outputstream of client A can't be directly supplied to client B. Constraint:Limited memory

8
  • Well... Client B has an input stream. Which, I think, is what you are mistaking for Client A's output stream Commented Oct 8, 2015 at 11:10
  • @user2651804 no both of them output
    – YAKOVM
    Commented Oct 8, 2015 at 11:11
  • 2
    Provide stackoverflow.com/help/mcve to get quick answer
    – vels4j
    Commented Oct 8, 2015 at 11:11
  • What do you mean by client A and B. Are they separate threads, processes or just objects in your program? Commented Oct 8, 2015 at 11:41
  • A and B are processes
    – YAKOVM
    Commented Oct 8, 2015 at 11:53

1 Answer 1

7

Try this approach; it transfers bytes ("Hello world") written to 'out' to 'out2' without use of an InputStream:

import java.io.ByteArrayOutputStream;

public class OutputStreamEx {

 public static void main(String[] args) {
    String content = "Hello world";
    byte[] bytes = content.getBytes();
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(bytes, 0, bytes.length);
        ByteArrayOutputStream out2 = new ByteArrayOutputStream();
        out.writeTo(out2);
        System.out.println(out2.toString());
     } catch (Exception ex) {
        ex.printStackTrace();
     }
  }
 }

Not the answer you're looking for? Browse other questions tagged or ask your own question.