Warning: include() [function.include]: Unable to access /var/www/html/rogue-development/blog2/wp-content/advanced-cache.php in /var/www/html/rogue-development/blog2/wp-settings.php on line 62

Warning: include(/var/www/html/rogue-development/blog2/wp-content/advanced-cache.php) [function.include]: failed to open stream: No such file or directory in /var/www/html/rogue-development/blog2/wp-settings.php on line 62

Warning: include() [function.include]: Failed opening '/var/www/html/rogue-development/blog2/wp-content/advanced-cache.php' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/rogue-development/blog2/wp-settings.php on line 62

Notice: add_option was called with an argument that is deprecated since version 2.3 with no alternative available. in /var/www/html/rogue-development/blog2/wp-includes/functions.php on line 3468

Notice: register_sidebar_widget is deprecated since version 2.8! Use wp_register_sidebar_widget() instead. in /var/www/html/rogue-development/blog2/wp-includes/functions.php on line 3382

Notice: register_widget_control is deprecated since version 2.8! Use wp_register_widget_control() instead. in /var/www/html/rogue-development/blog2/wp-includes/functions.php on line 3382
ByteArray writeUTF vs. writeUTFBytes « Marc’s Musings

ByteArray writeUTF vs. writeUTFBytes

I just got bit by using writeUTF instead of writeUTFBytes, I knew they worked differently, but it took me a couple google searches to figure out why.

writeUTF appends a 16 bit length, and then your actual data
writeUTFBytes simply appends your data

So why does writeUTF work like that?

When you read data back in, readUTF can get how long the string to read is, and then read exactly that much in. On the other hand, readUTFBytes just reads everything back. Seems like a minor difference, but imagine these two scenarios.

This works perfectly:

  1.  
  2. myByteArray.writeUTF("Hello World");
  3. myByteArray.writeFloat(100);
  4. a = myByteArray.readUTF();
  5. b = myByteArray.readFloat();
  6.  

This next example does not work because the readUTFBytes also reads in that float we wrote, so the readFloat call has nothing to retrieve:

  1.  
  2. myByteArray.writeUTFBytes("Hello World");
  3. myByteArray.writeFloat(100);
  4. a = myByteArray.readUTFBytes(myByteArray.length);
  5. b = myByteArray.readFloat();
  6.  

On the other hand, the writeUTF method can only handle strings up to 64k float. So if you have an arbitrarily sized string it’ll eventually failed. This is what I ran into.

So here’s a couple simple rules for choosing which to use:

1) If you need to write arbitrarily sized large strings, use writeUTFBytes, but don’t write anything after it.
2) If you need to write multiple items to your byte array and read them back that way, use writeUTF, but don’t exceed 64k strings
3) If you need to create a byte array with no “header” of that leading length, use writeUTFBytes (like if you’re making a stand-alone file to inter operate with some other system)

0 Responses to “ByteArray writeUTF vs. writeUTFBytes”


Comments are currently closed.