Skip to content

Commit

Permalink
Add protected method to connect channel to streams
Browse files Browse the repository at this point in the history
These methods can be overridden by subclasses of existing channels
to write to/read from streams,
for example, to replay some captured stream,
or just to do some tests based on ByteArrayInputStream.

Signed-off-by: Andrés Alcarraz <alcarraz@gmail.com>
  • Loading branch information
alcarraz committed Nov 27, 2023
1 parent 98ae51d commit e6272a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions jpos/src/main/java/org/jpos/iso/BaseChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ protected void connect (Socket socket)
setChanged();
notifyObservers();
}

/**
* Allow the channel to be connected to a pair of input and output streams.
* <p>
* For instance for taking input from or outputting to a file or standard input/ouput.
*
* @param in Where to read from.
* @param out Where to write to.
*/
protected void connect(InputStream in, OutputStream out) {
usable = in != null || out != null; //at least one of them has to be not null
if (in != null) serverIn = new DataInputStream(in);
if (out != null) serverOut = new DataOutputStream(out);
}
protected void postConnectHook() throws IOException {
// do nothing
}
Expand Down
8 changes: 8 additions & 0 deletions jpos/src/main/java/org/jpos/iso/channel/XMLChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import org.jpos.iso.packager.XMLPackager;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

Expand Down Expand Up @@ -117,4 +120,9 @@ public void disconnect () throws IOException {
reader.close ();
reader = null;
}

protected void connect(InputStream in, OutputStream out) {
super.connect(in, out);
reader = new BufferedReader(new InputStreamReader(in));
}
}

0 comments on commit e6272a3

Please sign in to comment.