Removing trailing nulls in ActionScript

When reading a byte array in ActionScript (like in C) the null value is read as a string termination. When you try use the readUTFBytes method over a ByteArray if the data contain a null value (0x00) the ActionScript interpreter stop the reading and retunr you a truncated string, in order to avoid this issue you can simply parse the data before using the method readUTFBytes.
Following a small sample where the variable bytedata represents the raw data recovered from your read operation.


var ba:ByteArray = new ByteArray();

for(var i:int = 0; i < bytedata.length; i++){

if(bytedata[i] != 0x00){

ba.writeByte(bytedata[i])

}else{

ba.writeByte(0x0A)

}

}

ba.position = 0;

var data:String = ba.readUTFBytes( ba.length );

Retrive image type, width and height of png, jpg, gif using nabiro

With these few lines of code you can easly retrive type and size of an image
var imageInfo:ImageInfo = new ImageInfo(myByteArray);
imageInfo.extractType();
imageInfo.extractSize();
trace(imageInfo.type.toString());
trace(imageInfo.width.toString());
trace(imageInfo.height.toString());

Get the PNG image sizes from IHDR

Imagine to load a PNG file through an URLStream intance named stream, on the Event.COMPLETE event this script extract the IHDR information releated to the width and height of the image, more info about IHDR here.
var _width:Number = 0;
var _height:Number = 0;

var ba:ByteArray = new ByteArray();
stream.readBytes(fileData, 0, stream.bytesAvailable);

ba.position = 16
_width += this.readUnsignedByte() << 24;

ba.position = 17
_width += this.readUnsignedByte() << 16;

ba.position = 18
_width += this.readUnsignedByte() << 8;

ba.position = 19
_width += this.readUnsignedByte();

ba.position = 20
_height += this.readUnsignedByte() << 24;

ba.position = 21
_height += this.readUnsignedByte() << 16;

ba.position = 22
_height += this.readUnsignedByte() << 8;

ba.position = 23
_height += this.readUnsignedByte();

ba.position = 0;

return new Rectangle(0, 0, _width, _height);

load binary data via xml message

say you are dealing with a backend via xml messages, for some instance you need to load binary data. Flash player can load images, sounds, videos and other movies, but these are operations that go separated from the data exchange context. You can join xml messages and binary content using base64 classes.
The advantages are:
- concurrent messages with content
- you can build up your own distributed objects
- more opportunities to hardening your movies, e.g content protection, cryptography, progressive and conditioned loading
and the cons:
- messages are greater, almost 20/30%
- if you plan to getting touch with real data, some ByteArray skills needed
- some people prefers amf, faster and data driven
// these two code files show how to load a sample image using xml message generated from php


// as3, TryB64php.as

/**
* @author jaco
*
* created on 16/10/2009 16.56.51
*/
package {

import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.net.*;
import flash.geom.*;
import flash.utils.*;

// as3, got from http://dynamicflash.com/goodies/base64/
// if you're in flex/air, there are mx.utils.Base64* classes
import com.dynamicflash.util.Base64;


/**
*
*/
public class TryB64php extends MovieClip {

private static const PHP_URL:String = "get_xml_b64response.php";

private var ul:URLLoader;
private var ld:Loader;

/**
* the constructor
*/
function TryB64php(){

var ur:URLRequest = new URLRequest(PHP_URL);
ld = new Loader();
ul = new URLLoader();

addChild(ld);

ul.addEventListener(Event.COMPLETE, onULComplete);
ul.load(ur);
}

private function onULComplete(evt:Event):void {

var xml:XML = XML(ul.data);

var ba:ByteArray = Base64.decodeToByteArray(xml);
ld.loadBytes(ba);
}
}
}


// php, get_xml_b64response.php

<?php
/*
* Created on 16/ott/2009
*
* jaco_at_pixeldump
*/

define("XML_HEADER", "<?xml version=\"1.0\"?>");
define("NL", "\r\n");

define("RESPONSE_INFO", "info");
define("RESPONSE_VALUE_BASE64", "base64");

/**
* read resource from path
* @param file name
* @param directory
*
* @return xml instance with base64 encoded data
*/
function get_resource($resFN, $resDir = "./") {

$filePath = $resDir .$resFN;
$fRes = file_get_contents($filePath, FILE_BINARY);

return create_simpleXmlMessage(base64_encode($fRes), RESPONSE_VALUE_BASE64);
}

/**
* create an xml message
* @param message string
* @param response type
*
* @return formatted xml instance with given data
*/
function create_simpleXmlMessage($str, $type = RESPONSE_INFO) {

$xmlStr = XML_HEADER .NL;
$xmlStr .= create_xmlNode("response", $str, array("type" => $type, "timeStamp" => mktime()));

return $xmlStr;
}

/**
* create a simple xml node with some data population
* @param node name
* @param node content
* @param attributes
* @param CDATA flag
*
* @return formatted xml node with given data
*/
function create_xmlNode($nodeName, $nodeContent = "", $attrs = array(), $cDataFlag = true){

$xmlStr = "<" .$nodeName ." ";
$sfx1 = $cDataFlag ? "<![CDATA[" : "";
$sfx2 = $cDataFlag ? "]]>" : "";

if(count($attrs))
foreach($attrs as $k => $v) $xmlStr .= $k ."=\"" .$v ."\" " ;

if(strlen($nodeContent)){
$xmlStr .= ">" .$sfx1 .$nodeContent .$sfx2;
$xmlStr .= "</" .$nodeName .">";
}
else $xmlStr .= "/>";

return $xmlStr;
}

// the app

header("Content-type: text/xml");
echo get_resource("sample_avatar.png"); // sample image
?>