12 Replies Latest reply: Jul 26, 2007 12:28 PM by 807605 RSS

    Write Methods in OutputStream Class

    807605
      The "Write" methods in the OutputStream class accept only arrays of type byte or int. I would however like to send other types such as String. Any idea how I can do that?
      I was told that maybe I could cast using wrapper classes but I'm not sure how it can be done.

      Also, Is there any way that I could use ArrayLists, etc to send data instead of an Array? I tried, it didn't work but I thought maybe there's a way to go around it.

      Your help is very much appreciated.
        • 1. Re: Write Methods in OutputStream Class
          807605
          Depending on what you are trying to do exactly, look into using Writers for you output, like BufferedWriter, OutputStreamWriter, PrintWriter, FileWriter, etc.

          They will accept a string as a param.

          ~Tim
          • 2. Re: Write Methods in OutputStream Class
            807605
            I have two applications: one on a server (PC) and another on a client (mobile phone). The server sends data to the client via Bluetooth. Everything works perfectly but the data is stored as bytes in an array of arrays (Each element of an array is an array itself). The only thing is I need to send data as String now.

            For some reason I didn't think of BufferedWriter. I'll try using that instead & see what happens.

            Thank you very much.
            • 3. Re: Write Methods in OutputStream Class
              807605
              If you can't use a Writer, for whatever reason, you could:

              1. Transform a String into a byte[] using one of String's getBytes methods.
              or
              2. Use the writeChars or writeUTF methods of interface DataOutput,
              implemented by DataOutputStream, among others.
              • 4. Re: Write Methods in OutputStream Class
                807605
                Because of one of the methods in the external library I couldn't use BufferedWriter but I used the getByte method from the String class & it seems to be working. I can do the reverse to get the String back at the receiver.

                Thank you both very much for your help.
                • 5. Re: Write Methods in OutputStream Class
                  807605
                  Just as I posted my last message, I realized that although I could change the data from a String to byte[] & send it over, I still have one problem.

                  I'd like to display what is being sent in a JTextField. The problem is that if I leave the data as String, I cannot use the method write() to send the data over because write() can only send int & byte. If I do change it to byte using getByte() method of the String class, I get an error when I want to display the data in my JTextField because the setText() only accpets Strings.

                  Using BufferedWriter sounds good but the thing is that I'm using a method from an external library to open a bluetooth connection between the server (PC) & the client (Monile phone) & hence the line:
                  OutputStream out = conn.openOutputStream();

                  If I change the variable "out" to bufferedwriter, bufferedoutputstream or anything else besides what it is now, I get incompatible types error "conn.openOutputStream();" which is logical. There is no other method in the library I'm using that I could replace openOutputStream() with.

                  It's a catch 22 situation...

                  Does anyone know how to go around this?
                  • 6. Re: Write Methods in OutputStream Class
                    807605
                    Just as I posted my last message, I realized that
                    although I could change the data from a String to
                    byte[] & send it over, I still have one problem.

                    I'd like to display what is being sent in a
                    JTextField. The problem is that if I leave the data
                    as String, I cannot use the method write() to send
                    the data over because write() can only send int &
                    byte. If I do change it to byte using getByte()
                    method of the String class, I get an error when I
                    want to display the data in my JTextField because the
                    setText() only accpets Strings.
                    You can convert a String to a byte[] and a byte[] to a String, as BigDaddyLoveHandles indicated. Look at the String.getBytes(String charsetname) method, and the String(byte[], String charset) constructor. Be sure to specify the same encoding for both ends of the conversion. UTF-8 is usually the best choice.
                    Using BufferedWriter sounds good but the thing is
                    that I'm using a method from an external library to
                    open a bluetooth connection between the server (PC) &
                    the client (Monile phone) & hence the line:
                    OutputStream out = conn.openOutputStream();

                    If I change the variable "out" to bufferedwriter,
                    bufferedoutputstream or anything else besides what it
                    is now, I get incompatible types error
                    "conn.openOutputStream();" which is logical. There is
                    no other method in the library I'm using that I could
                    replace openOutputStream() with.
                    You don't need to change the OutputStream to a BufferedWriter, you wrap the OutputStream in a BufferedWriter. Here's a sample:
                    Writer writer = new BufferedWriter(new OutputStreamWriter(conn.openOutputStream()));
                    Now you can use the Writer methods to write Strings to the stream. You can use the input versions of those same classes to do the reading the same way (BufferedReader, InputStreamReader, etc.)
                    It's a catch 22 situation...

                    Does anyone know how to go around this?
                    • 7. Re: Write Methods in OutputStream Class
                      807605
                      Thank you Hunter. I will try that now.
                      • 8. Re: Write Methods in OutputStream Class
                        807605
                        I used the line Hunter told me, changed a few things around & It seems to be working now. Really appreciate it!!

                        As I think I mentioned before, I'd like to display the values in a JTextField for instance as they're being sent or received, the thing is that some of my data are of type byte or something besides String.
                        Would you please suggest a way to change them to String? (In case you're wondering, I need to change them to String so that I'd be able to use setText() to display them in the JTextfield). If I use the toString() method, the output would be an object & not a String. I think I knew a way to handle that some time ago but unfortunately cannot remmeber now.

                        Thanks a lot.
                        • 9. Re: Write Methods in OutputStream Class
                          800383
                          What is that you need transformed into a string? If you have a byte[], you can get a srting by doing new String(byte[]), as you\ve already been told, for other things you'll probably have to make your own way of transforming them. If you have int[] that actually holds bytes, turn it into byte[]. If you have Object, see how you want it stringified and code it.
                          • 10. Re: Write Methods in OutputStream Class
                            807605
                            Whenever you convert between characters and bytes in java you use a specific character encoding scheme. Most encoding schemes can only handle 256 different characters, whereas java strings can handle all 2^16 UNICODE characters.

                            UTF-8 is a scheme which handles all UNICODE characters, but one character may be encoded as up to three bytes.

                            There's a constructor in String which takes a byte array and the name of the encoding scheme (which complements the getBytes() method).

                            Normally you'd use a Reader/Writer to do the encoding/decoding of text.

                            In a case where you're using a mixed stream with some String and some binary data, if the format isn't specified by the project specs, I'd suggest either using a DataInputStream/DataOutputStream and the getUTF/writeUTF methods for text (it outputs the length, followed by UTF-8 bytes) or serialization.

                            If you are going to do your own byte - character conversions then your project needs to specify what encoding scheme you are using. For English ISO-8859-1 might be a good choice.
                            • 11. Re: Write Methods in OutputStream Class
                              807605
                              @OP:
                              Here's a great article that explains how to use character encodings:
                              The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
                              http://www.joelonsoftware.com/articles/Unicode.html

                              @malcolm:
                              <nitpick>Unicode defines 1,114,112 code points, and utf-16 encodes them in either 2 or 4 bytes. Utf-8 uses 1-3 bytes for the BMP, but can use up to 6 bytes (there are no code points defined in unicode that need more than 4 bytes, but who hasn't needed to encode non-existent characters?) </nitpick>

                              Not trying to be a jerk, it just feels like a pedantic Tuesday :)
                              • 12. Re: Write Methods in OutputStream Class
                                807605
                                Thank you all for your help. I figured out how to do it.
                                All the best