Question

Ok, so I'm designing an Android MUD client as part of my school project. I'm having an issue, however, while implementing ANSI color parsing. I read in the data on a byte-by-byte basis. I've tried setting the character "hex" as '\033', '27', and '0x1B' but I can never seem to get it to detect the escape character. Is there anything you guys can see wrong with my checking of it? Also, the line "char check = String.valueOf(j).charAt(0);" is temporary, I was originally trying to check the character variable "hex" against the byte "j". Is there possibly a better way of checking for the character?

                    while(isConnected) {

                    int j = 0;

                    try {
                            int i = arrayOfByte.length;
                            j = streamInput.read(arrayOfByte, 0, i);

                            char check = String.valueOf(j).charAt(0);

                            Log.d("Console","Char is - " + check);
                            if (j == -1)
                            {

                                Log.d("Console","j = -1");
                                throw new Exception("Error while reading socket.");
                            } else if (j == 0) {

                                Log.d("Console","Continuing");
                                continue;
                            }  else if (check == hex)  {
                                Log.d("Console","Yo, daddio!");
                            } else {

                                final String strData = new String(arrayOfByte, 0, j).replace("\r", "");


                                runOnUiThread(new Runnable() {
                                     public void run() {


                                         textContent.append(strData);
                                         scrollToBottom();

                                    }
                                });

                            }
                    } catch (Exception e) {

                        Handler handlerException = GameWindow.this.mHandler;
                        String strException = e.getMessage();
                        final String strMessage = "Error while receiving from server:\r\nConnection terminated";

                        Runnable rExceptionThread = new Runnable()
                        {
                            public void run()
                            {
                                Toast.makeText(context, strMessage, 3000).show();
                            }
                        };

                        handlerException.post(rExceptionThread);

                        if(strException.indexOf("reset") != -1 || strException.indexOf("rejected") != -1)
                        {
                            isConnected = false;
                            try 
                            {
                                connectionSocket.close();
                            }
                            catch (IOException e1) 
                            {
                                e1.printStackTrace();
                            }

                        }

                        isConnected = false;
                    }

                }   
Was it helpful?

Solution

Well, you're checking the number of bytes read instead of each individual byte.

j = streamInput.read(arrayOfByte, 0, i);

returns the number of bytes read and put in arrayOfByte those bytes.

Therefore you need to do the following:

for (int n=0; n < j; n++)
{
    if (arrayOfByte[n]  == hex) Log.d("Console", "Yo, daddio!");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top