Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

multibyte-character support for rosjava

Java supports multi-byte character as the Japanese, Korean or Chinese. This means that length() method of java.lang.String class returns number of the character, not number of bytes. http://rosettacode.org/wiki/String_length#Java

java.lang.String tmp1 = "robot";
System.out.println(tmp1.length());           -> 5
System.out.println(tmp1.getBytes().length);  -> 5
java.lang.String tmp2 = "ロボット";
System.out.println(tmp2.length());           -> 4
System.out.println(tmp2.getBytes().length);  ->12

Thus, using Japanese in Talker.java in rosjava_tutorial_pubsub crushes as following errors

Exception in thread "Thread-3" java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:165)
at java.nio.ByteBuffer.put(ByteBuffer.java:813)
at org.ros.message.Message$Serialization.writeString(Unknown Source)
at org.ros.message.std_msgs.String.serialize(Unknown Source)
at org.ros.message.Message.serialize(Unknown Source)

My solution is to change String.java as followings but not confident since I'm not Java programmer.

public int serializationLength() {
   int __l = 0;
   //__l += 4 + data.length();
   __l += 4 + data.getBytes().length;
   return __l;
}

multibyte-character support for rosjava

Java supports multi-byte character as the Japanese, Korean or Chinese. This means that length() method of java.lang.String class returns number of the character, not number of bytes. http://rosettacode.org/wiki/String_length#Java

java.lang.String tmp1 = "robot";
System.out.println(tmp1.length());           -> 5
System.out.println(tmp1.getBytes().length);  -> 5
java.lang.String tmp2 = "ロボット";
System.out.println(tmp2.length());           -> 4
System.out.println(tmp2.getBytes().length);  ->12

Thus, using Japanese in Talker.java in rosjava_tutorial_pubsub crushes as following errors

Exception in thread "Thread-3" java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:165)
at java.nio.ByteBuffer.put(ByteBuffer.java:813)
at org.ros.message.Message$Serialization.writeString(Unknown Source)
at org.ros.message.std_msgs.String.serialize(Unknown Source)
at org.ros.message.Message.serialize(Unknown Source)

My solution is to change String.java as followings but not confident since I'm not Java programmer.

public int serializationLength() {
   int __l = 0;
   //__l += 4 + data.length();
   __l += 4 + data.getBytes().length;
   return __l;
}

multichar_pubsub.tgz

multibyte-character support for rosjava

Java supports multi-byte character as the Japanese, Korean or Chinese. This means that length() method of java.lang.String class returns number of the character, not number of bytes. http://rosettacode.org/wiki/String_length#Java

java.lang.String tmp1 = "robot";
System.out.println(tmp1.length());           -> 5
System.out.println(tmp1.getBytes().length);  -> 5
java.lang.String tmp2 = "ロボット";
System.out.println(tmp2.length());           -> 4
System.out.println(tmp2.getBytes().length);  ->12

Thus, using Japanese in Talker.java in rosjava_tutorial_pubsub crushes as following errors

Exception in thread "Thread-3" java.nio.BufferOverflowException
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:165)
at java.nio.ByteBuffer.put(ByteBuffer.java:813)
at org.ros.message.Message$Serialization.writeString(Unknown Source)
at org.ros.message.std_msgs.String.serialize(Unknown Source)
at org.ros.message.Message.serialize(Unknown Source)

My solution is to change String.java as followings but not confident since I'm not Java programmer.

public int serializationLength() {
   int __l = 0;
   //__l += 4 + data.length();
   __l += 4 + data.getBytes().length;
   return __l;
}

multichar_pubsub.tgzdownload attached file and rename .jpg to to .tgz multichar_pubsub.jpg