Java strings read and write

(Karsten Meier) #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

(Robert) #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

(Dave Burt) #3

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

(Karsten Meier) #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

(Robert) #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