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

Change byte[] to inputstream java

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


    Hey, I think my last question was too broad, got no answers. So focusing on my main problem. I'm using two libraries in an android app I'm trying to make. New to android. The app is for connecting to serial devices and controlling their console via a terminal.

    One library is for setting up a serial connection, setting baud rate etc and can also write read/data over serial.. The other is for creating a terminal session. My problem lies in incorporating both of these together to have a terminal that is connected to a serial device.

    In the terminal library I need to supply an InputStream and OutputStream to provide input and output to the terminal. So I have to call setTermIn(java.io.InputStream) and setTermOut(java.io.OutputStream) to connect the input and output streams to the emulator.

    In the serial library however there are two methods for sending and receiving and these deal with arrays of bytes.
    sendData(byte[] data) for sending data and a dataListener for receiving data. I have to implement this and code the method onDataReceived(int id, byte[] data) with id being the name of the device.

    i don't have source code for the function that sends an array of bytes over serial, so how do I make the array of bytes into a stream to send to my terminal?


Comments

  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    There's two Java classes, ByteArrayInputStream and ByteArrayOutputStream which allow you to convert between/wrap byte arrays in input/output streams.

    ByteArrayInputStream

    ByteArrayOutputStream

    So I imagine you want to do something like this....

    public void onDataReceived(int id, byte[] data)
    {
       ByteArrayInputStream bis = new ByteArrayInputStream(data);
       
       setTermIn(bis);
    }
    
    
    public void sendData(byte[] data)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);		
       	
        bos.write(data, 0, data.length);
    	
        setTermOut(bos);
    }
    


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


    Thanks a lot, for onDataReceived() I think I can implement it like that, as I get to implement it myself, it is declared in an interface and I get to decide what happens to the data when it is received as a byte array.

    However for sendData, when I am overriding it, I can't see the code for that method in the library, so if I am overriding it I would need to know that code? As in, it might have code to choose to write over a particular serial if more than one is connected? Or that does something else I'm not aware of? All it says is "sends data over the connected adapter" in the documentation. Is there anyway to just include the code for the method for a library and append something to it?! That's probably nonsense as it would be sending stuff twice.

    I could just assume nothing fancy is going on and it just sends out an array of bytes and see does it work, thanks!

    What I am confused about is how does "write" know where it is writing to? Is it writing over serial?
    or would I have to call super.sendData?
    /google


  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    Okay, maybe I have got sendData() backwards.
    I didn't quite follow what calls what.

    What method of yours gets called when the terminal library wants to send something? What's the full parameter list ?

    If you have a ByteArrayOutputStream already, then it would be something like
    byte[] myBytesToSend = bos.toByteArray();
    
    sendData(myBytesToSend);
    

    Where "bos" is the ByteArrayOutputStream you passed to setTermOut();

    But without the full code it's hard to follow what calls what.
    But those examples should show you how to convert byte arrays to and from Input/Output streams.


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


    Sorry that I'm not being clear! It's because I am using two libraries. One will actually send the data over serial(library 1), and the other just makes the terminal for visual use(library 2), I just want the commands I send using library 1 to be echoed onto the terminal screen of library 2, and the data sent back to be echoed also.

    When I want to send something over serial I call sendData(byte[]) from library 1, this sends a byte[] over serial and receives data back via byte[] in onDataReceived(byte[]). So that works fine, I can send data over and back. However it is not displayed on screen due to the terminal(library 2) expecting bytes in the form of a stream.

    I think your example is great, what I think I need is something like this, I am wondering does it sound reasonable that it would work:


    Override sendData:
    public void sendData(byte[] data)
    {
        [B]//this should echo what I send to the terminal in the correct format[/B]
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);		
       	
        bos.write(data, 0, data.length);
    	
        setTermOut(bos);
      
      [B]//send data over serial using original sendData() method
        super.sendData(data);[/B]
    }
    


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


    Something I don't understand is I need to have a 'serialadapter' to call sendData on.
    USB2SerialAdapter is the main object used to communicate with the usb to serial adapter. The AdapterConnectListener given in SlickUSB2Serial.autoConnect method will be sent back an instance of USB2SerialAdapter in the onAdapterConnected method. Use this object to send/receive data and get/set connection settings.

    But how do I call the super version then? Super gives me an error.


    The bold line gives me an error:
    private USB2SerialAdapter mSelectedAdapter;
    
    ...
    
    public void sendData(byte[] data)
    {
        //this should echo what I send to the terminal in the correct format
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);		
       	
        bos.write(data, 0, data.length);
    	
        setTermOut(bos);
      
      //send data over serial using original sendData() method
        [B]super.mSelectedAdapter.sendData(data);[/B]
    }
    
    
    


    Multiple markers at this line
    	- The method sendData(byte[]) is undefined for the type 
    	 Activity
    	- mSelectedAdapter cannot be resolved or is not a field
    

    mSelectedAdapter.sendData(data); works fine but that is just calling sendData from that class and not the super version?


    If I just call super.sendData(data) it gives the error "The method sendData(byte[]) is undefined for the type Activity"


  • Advertisement
  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Because of context, it's trying to access the Activity parent class rather than the other super class you want. Disambiguate your super() call.


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


    Thanks srsly78, always a great help, owe you a pint! Will try to do that now.

    And of course thank you CreepingDeath.


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


    Wait, my error was that I was using super at all. Correct:
    mSelectedAdapter.sendData(data);
    

    I'm not sure what it is called but when I declared a class at the top
    private USB2SerialAdapter mSelectedAdapter;
    
    I could just use it instead of super, not with super. Got confused about syntax.


    What's it called, a containment class?


Advertisement