Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java, value of streams not updating?

Options
  • 18-12-2012 11:57am
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭


    Overview:

    I am trying to connect to a terminal emulator with this library. https://github.com/jackpal/Android-Terminal-Emulator This will connect to a serial device and should show me sent/received data via another library for serial communication. To attach to a terminal session I need to provide an inputStream to setTermIn(InputStream) and an outputStream to setTermOut(OutputStream) according to the docs. However I send and receive byte arrays with the serial library, not streams, so I need to use ByteArrayInputStream to convert the data to a stream. I initialize and attach some streams like so , these are just initial streams and are not attached to the data I want to be sending/receiving.

    private OutputStream bos;
    private InputStream bis;
    
    ...
    
    byte[] a = new byte[4096];
    bis = new ByteArrayInputStream(a);
    bos = new ByteArrayOutputStream();
    session.setTermIn(bis);
    session.setTermOut(bos);
    /* Attach the TermSession to the EmulatorView. */
    mEmulatorView.attachSession(session);
    

    I can't seem to get anything displaying on the terminal session at all apart from the original value of the inputstream. Write doesnt work?


    The problem occurs here, although I can initialize and set the streams to the terminal emulator inputs, they never get updated. So below the terminal will come up with hello written in it, nothing else will be written, I can neither use the write function that comes with streams, nor the overridden write function that comes with the library, nothing will appear. If I change the value of bis after setTermIn(bis), nothing is reflected, if I write to bos that will not appear on screen either.
    TermSession session = new TermSession();
    
            byte[] a = new byte[]{'h','e', 'l', 'l', 'o'};
            bis = new ByteArrayInputStream(a);
            bos = new ByteArrayOutputStream();
            session.write("test one");
            session.setTermIn(bis);
            session.setTermOut(bos);
            session.write("test two");
    
            /* Attach the TermSession to the EmulatorView. */
            mEmulatorView.attachSession(session);
            mSession = session;
            try {
                bos.write(a);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            session.write("test three");
    

    The overridden write method is generally used by the library developers to append information to the terminal.
    write
    
    public void write(byte[] data,
                      int offset,
                      int count)
    Write data to the terminal output. The written data will be consumed by the emulation client as input.
    write itself runs on the main thread. The default implementation writes the data into a circular buffer and signals the writer thread to copy it from there to the OutputStream.
    
    Subclasses may override this method to modify the output before writing it to the stream, but implementations in derived classes should call through to this method to do the actual writing.
    


Comments

  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    Havent used that library but perhaps you need to be flushing the stream, depending on the size of the data you are writing to it?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    I've tried closing and flushing etc but it doesn't appear to be the problem. I have an example that works over telnet and whenever I call these write commands they work fine. So it has to be something with initializing the terminal I think. setTermIn etc. I don't understand at all why I set the streams and can't write to them.


  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    Do you get errors or just nothing out?
    I think you are going to need to debug this to determine if you are writing to the stream and just not seeing it, or something worse...


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    No errors in the log, just nothing out. Hmm, I've just tried changing the known working example and the same thing happens, so it definitely is something I'm doing wrong when initializing.

    bos.write() doesn't even work in the working example so I can rule using that out. Their write function does work in their example and not in mine, so I think I have to figure out why. As usual the docs don't say much!


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]




  • Advertisement
  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Hey thanks for having a look. That is the sample I am using to learn about the library, I'm not using a local program so it doesn't apply to me. The telnet example is more applicable. If I take that working example and change the streams I can in fact write on the screen still so I really don't know why mine doesn't work. i may have to try and amalgamate the programs, but it's annoying. The developers are the ones that said to set these streams, it doesn't work and now they don't reply :D


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Did you get this sorted Tar?


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Yes I did the same thing but I passed the streams to another class which had a constructor that called setTermIn and setTermOut. That works, no idea why. I can use the methods in the original class etc now.

    public SerialSession(InputStream termIn, OutputStream termOut) 
                {
    	        setTermIn(termIn);
    	        setTermOut(termOut);
    	    }
    


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    Yes I did the same thing but I passed the streams to another class which had a constructor that called setTermIn and setTermOut. That works, no idea why. I can use the methods in the original class etc now.

    public SerialSession(InputStream termIn, OutputStream termOut) 
                {
    	        setTermIn(termIn);
    	        setTermOut(termOut);
    	    }
    

    I had a similar issue with multi-threading. It needed a new class with a run() in order for it to work adequately. Good to know!


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Yeah jsut weird, it makes absolutely no sense to me! Like I'm still creating the same streams...passing the same thing to the methods. Meh, it works. On to other problems.


  • Advertisement
Advertisement