Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String with null bytes  RSS feed

 
tony hutcheson
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IBM's MQSeries is being used in the following:
MQMessage msg;
int len = 5;
String str = msg.readString(len);
The underlying class for MQMessage is a byte[]. It creates ByteArrayOutputStreams to read from.
What happens is str ends up with a value of null bytes and I get false with the following comparisons:
str.equals(null) results in false
str == null results in false
str.equals( new String() ) results in false
It doesn't appear to be specific to MQSeries but I do not know. Does anyone know of how to solve this in Java so that I can determine if str actually read something in?
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your saying that 'msg.readString(len)' should not return null, then you need to provide the code for MQMessage.readString(int) so that we can debug it.
 
tony hutcheson
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry. I only have the class files. However, I do not have a problem with it returning null. The problem occurs when it returns a string of 'len' length, but filled with null bytes. So if you get the length of the string it will give you whatever the value of 'len' is, but it will contain all null values. I did not know if this is something about the way that java handles ByteArrayOutputStreams. The only solution that I can think of is creating a string :
nullStr = new String( new byte[len] )
and comparing it thus:
str.equals( nullStr )
although I am not sure that this will work.
 
Mark Savory
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that your trick will work!

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!