java strings read and write

Discussion in 'Ruby' started by Karsten Meier, Aug 11, 2005.

  1. Hello Ruby Fans

    I want to prepare some data so that a java program on a mobile phone can
    read them most easily. Currently my java program reads it char by char
    in a line oriented way. This works, but is slow on some phones, and also
    I need to handle newlines in the data in a special way.
    Now I like to create a file with "native java strings", in this case
    strings that can be read with DataInputStream.readUTF() method.
    I hope that this simplifies the code and improves the speed.

    I don't need a complete Ruby/Java Integration, I just want to write
    these java strings to a file (or to stdout as a cgi-script).
    Are there any libraries for this?

    Regards

    Karsten Meier
     
    Karsten Meier, Aug 11, 2005
    #1
    1. Advertising

  2. Karsten Meier wrote:
    > Hello Ruby Fans
    >
    > I want to prepare some data so that a java program on a mobile phone
    > can read them most easily. Currently my java program reads it char by
    > char in a line oriented way. This works, but is slow on some phones,
    > and also I need to handle newlines in the data in a special way.
    > Now I like to create a file with "native java strings", in this case
    > strings that can be read with DataInputStream.readUTF() method.
    > I hope that this simplifies the code and improves the speed.


    Can't you just use BufferedReader? That's present since JDK 1.1 so I
    assume it will be available on your phone.
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html

    > I don't need a complete Ruby/Java Integration, I just want to write
    > these java strings to a file (or to stdout as a cgi-script).
    > Are there any libraries for this?


    That's standard functionality:

    File.open("foo.txt", "w") do |io|
    io.puts "first line"
    io.print "second line\n"
    end

    As long as you stick with ASCII chars it's quite easy. If you want to
    write UTF8 or something it starts getting complicated.

    Kind regards

    robert
     
    Robert Klemme, Aug 11, 2005
    #2
    1. Advertising

  3. Karsten Meier

    Dave Burt Guest

    Karsten Meier wrote...
    > Now I like to create a file with "native java strings", in this case
    > strings that can be read with DataInputStream.readUTF() method.
    > I hope that this simplifies the code and improves the speed.
    >
    > I don't need a complete Ruby/Java Integration, I just want to write
    > these java strings to a file (or to stdout as a cgi-script).
    > Are there any libraries for this?


    The way I read the Java API, the format is simply the UTF-8-encoded string
    preceded by its length in bytes as a 16-bit integer.

    Except that (from
    http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInput.html):
    * The null byte '\u0000' is encoded in 2-byte format rather than 1-byte, so
    that the encoded strings never have embedded nulls.
    * Only the 1-byte, 2-byte, and 3-byte formats are used.
    * Supplementary characters are represented in the form of surrogate pairs.

    So, in Ruby:
    # untested code (I'm on Windows with no iconv)
    require 'iconv'
    class String
    def to_java_utf
    # Convert the string to UTF-8
    result = Iconv.new('iso-8859-1', 'utf-8').iconv(self)
    # Re-encode null characters in two-byte format
    result.gsub!("\0", "\300\200")
    # The string's size must fit in 2 bytes, so the string must be less than
    64kb
    fail if result.size >= 65536
    # Prepend string length as short integer in network (big-endian) byte
    order
    result.insert(0, [converted_text.size].pack("n"))
    end
    end
    text = "Test string\n\0with embedded newline and 0"
    p text.to_java_utf #=> "\000(Test string\n\300\200with embedded newline and
    0"

    Cheers,
    Dave
     
    Dave Burt, Aug 11, 2005
    #3
  4. Robert Klemme wrote:

    >
    > Can't you just use BufferedReader? That's present since JDK 1.1 so I
    > assume it will be available on your phone.
    > http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html


    No, it is not part of the CLDC/MIDP specification, used in the majority
    of all phones available today. The IO capabilities are quite low.

    > As long as you stick with ASCII chars it's quite easy. If you want to
    > write UTF8 or something it starts getting complicated.

    I want to do this, because of german umlauts, and I also like to include
    newlines in my strings, so currently I decode them, but the decoding
    make it slower again.

    Regards

    Karsten
     
    Karsten Meier, Aug 12, 2005
    #4
  5. Karsten Meier wrote:
    > Robert Klemme wrote:
    >
    >>
    >> Can't you just use BufferedReader? That's present since JDK 1.1 so I
    >> assume it will be available on your phone.
    >> http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html

    >
    > No, it is not part of the CLDC/MIDP specification, used in the
    > majority of all phones available today. The IO capabilities are quite
    > low.


    Ah, ok. Didn't know that.

    >> As long as you stick with ASCII chars it's quite easy. If you want
    >> to write UTF8 or something it starts getting complicated.

    > I want to do this, because of german umlauts, and I also like to
    > include newlines in my strings, so currently I decode them, but the
    > decoding make it slower again.


    In that case ISO-8859-something should be sufficient IMHO. Newlines can
    be used with ASCII all right.

    Kind regards

    robert
     
    Robert Klemme, Aug 12, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Isaac
    Replies:
    0
    Views:
    2,477
    Isaac
    Jul 10, 2003
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    802
    Malcolm
    Jun 24, 2006
  3. sahm
    Replies:
    4
    Views:
    708
  4. jennifermackles
    Replies:
    0
    Views:
    580
    jennifermackles
    Nov 13, 2012
  5. Tim Chase
    Replies:
    0
    Views:
    103
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page