Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protected method to connect channel to streams #572

Open
wants to merge 1 commit into
base: tail
Choose a base branch
from

Conversation

alcarraz
Copy link
Contributor

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.

Sometimes, we may want to use the existing channels,
but not necessarily to connect them to a TCP/IP endpoint,
for instance, we may want to input data from a file directly as an input of the channel.

Here, there is an example I'm using to read XML messages from a file or standard input:

public class XMLInputFileChannel extends XMLChannel implements AutoCloseable{
    public XMLInputFileChannel() throws ISOException {
        this(System.in);
    }
    public XMLInputFileChannel(InputStream in) throws ISOException {
        this(in, new XMLPackager());
    }

    public XMLInputFileChannel(XMLPackager packager){
        this(System.in, packager);
    }

    public XMLInputFileChannel(InputStream in, XMLPackager packager) {
        connect(in, null);
        setPackager(packager);
        
    }

    @Override
    public void close() throws IOException {
        disconnect();
    }

    @Override
    public boolean isConnected() {
        return super.isConnected() || (usable && serverIn != null);
    }
}

@ar
Copy link
Member

ar commented Nov 28, 2023

It would be nice to have this XMLInputFileChannel as part of the PR and it wouldn't hurt to have at least a unit test so that we can make sure we don't break it in the future with changes to BaseChannel.

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 <[email protected]>
@alcarraz alcarraz force-pushed the feature/channel-connect-to-streams branch from e6272a3 to 986b144 Compare November 28, 2023 23:22
@alcarraz
Copy link
Contributor Author

Sorry, I had written a couple of tests, but somehow the test class got stashed and I didn't notice.

I don't think it is a good idea to add the XMLInputFileChannel, because it is for quite some narrow use case (where connecting in the constructor was justified). However, I understand it wouls be good to have an example usage. So, I'll add a more generic on a followup commit to this PR as soon as I can write it.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants