App fails unless input is written to System.out

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

App fails unless input is written to System.out

Postby Plastriq » Sat Oct 10, 2009 1:30 pm

Sorry for the cryptic title :)

I have an app that reads bytes from a socket using a BufferedInputStream. These bytes are stored in a byte[] buffer.
Now, unless I output the bytes I read to the System.out stream with my debug() method after reading them , my app simply does not work, I get ArrayIndexOutOfBounds exceptions while reading into the buffer, etc.

The connect, readBytes, debug and part of the run() methods are shown below. The bytes are read this way because of the way the packets are sent (2 bytes for packet size, then rest of packet).

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void connect(String username, String password) {
  2.  
  3.         this.username = username;
  4.  
  5.         this.password = password;
  6.  
  7.         try {
  8.  
  9.                 if (!runThread)
  10.  
  11.                         return;
  12.  
  13.                 Socket s = new Socket("cs.xfire.com", 25999);
  14.  
  15.                 in = new BufferedInputStream(s.getInputStream(), 16384);
  16.  
  17.                 out = new DataOutputStream(s.getOutputStream());
  18.  
  19.                 login();
  20.  
  21.         } catch (IOException ioe) {
  22.  
  23.                 disconnect();
  24.  
  25.         }
  26.  
  27. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private void readBytes() {
  2.  
  3.         try {
  4.  
  5.                 int low = in.read(), high = in.read();
  6.  
  7.                 int len = (0x00 | low | (high << <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" />) - 2;
  8.  
  9.                
  10.  
  11.                 buffer = new byte[len];
  12.  
  13.                 in.read(buffer, 0, len);
  14.  
  15.         } catch (IOException ioe) {
  16.  
  17.                 ioe.printStackTrace();
  18.  
  19.                 disconnect();
  20.  
  21.         } catch (NegativeArraySizeException nase) {
  22.  
  23.                 buffer = new byte[] { 0 };
  24.  
  25.         }
  26.  
  27. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static void debug(byte[] bs) {
  2.  
  3.         for (byte b : bs) {
  4.  
  5.                 System.out.print(String.format("%02x", b) + " ");
  6.  
  7.         }
  8.  
  9.         System.out.println();
  10.  
  11. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void run() {
  2.  
  3.         setName("Xfire Reader Thread");
  4.  
  5.        
  6.  
  7.         while(runThread) {
  8.  
  9.                 readBytes();
  10.  
  11.                 debug(buffer);
  12.  
  13.                
  14.  
  15.                 switch(buffer[0] & 0xFF) {
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Commenting out 'debug(buffer);' in the last code snippet will cause the app to malfunction.
Plastriq
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 05, 2009 3:27 pm
Location: The Netherlands

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests