5

I'm having a strange problem comparing strings. I send a string to my server (as bytes using getBytes()) from the client. I've ensured that encoding is the same on the client and server by starting both of them with -Dfile.encoding=UTF-8.

I noticed the problem when I was trying to perform a valueOf on the string I receive from the client, to convert it into an enum. When I print out the strings, they look exactly the same. But when I perform a compareTo, I get a non-zero number and equals returns false.

I'm assuming that it is an encoding problem. I'm not really sure though -- I'm still a bit of a novice when it comes to client-server programming with sockets.

This is what I get:

Waiting for connections on port 9090
Connected to client: 127.0.0.1
received command: GetAllItems
The value is |GetAllItems| (from client)
The value is |GetAllItems| (from enum)
equals: false

What am I doing wrong?

UPDATE

Here is how I'm reconstituting the string from the stream. Perhaps this is where I'm doing something wrong?

byte[] commandBytes = new byte[1024];
in.read(commandBytes); //in is a BufferedInputReader
String command = new String(commandBytes);
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • Can you run in a debugger, put a break point at the comparison, and then look deeper at the client vs. enum strings? – Rob Di Marco Oct 01 '10 at 17:18
  • How are you reparsing your string? – Tim Yates Oct 01 '10 at 17:19
  • @Rob - I would like to and that turns into another question :) This is a swing app. Do you have any pointers on how I can run a debugger on it? I've mostly worked on web-based apps where I debug by connecting to tomcat. – Vivin Paliath Oct 01 '10 at 17:20
  • If you're not developing your app inside of Eclipse or NetBeans already, then download Eclipse or NetBeans and copy your source file into a new project. Then run it by clicking the "Debug" button rather than the normal "Run" button. I understand not wanting to use a full IDE for writing code... but to not use one in debugging? That's crazy-talk! :) – Steve Perkins Oct 01 '10 at 17:23
  • @Steve I use IntelliJ Idea :) There you have to set up a debugger by connecting to a port (or something of that nature). I'm still trying to figure it out :) – Vivin Paliath Oct 01 '10 at 17:24
  • @Vivin Paliath - FYI: setting file.encoding on the command line may result in undefined behaviour: http://bugs.sun.com/view_bug.do?bug_id=4163515 It is better to provide the encoding explicitly. – McDowell Oct 29 '10 at 20:04

3 Answers3

4

My guess is that since your buffer is bigger than your string, there are added nulls in the reconsituted string. It is legal for nulls to be embedded inside strings in Java (unlike C and company), although Java handles them differently than standard UTF-8.

Try recording the length read, and pass that length to the string constructor:

int bytesRead = in.read(commandBytes);
String command = new String(commandBytes, 0, bytesRead);
Tim Yates
  • 5,151
  • 2
  • 29
  • 29
3

Your problem is in how you are constructing the string. You are reading in the bytes into a buffer length 1024, but you are not telling the String constructor to only look at the relevant points. So your code should be...

byte[] commandBytes = new byte[1024];
int length = in.read(commandBytes); //in is a BufferedInputReader
String command = new String(commandBytes, 0, length);
Rob Di Marco
  • 43,054
  • 9
  • 66
  • 56
1

Use java.text.Collator to compare strings.

oers
  • 18,436
  • 13
  • 66
  • 75
kamikaze
  • 19
  • 1