Reputation: 10153
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
Upvotes: 4
Views: 6168
Reputation: 2237
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();
}
}
}
Upvotes: 7