Skip to content

Instantly share code, notes, and snippets.

@alebianco
Last active September 23, 2016 14:46
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alebianco/4386769 to your computer and use it in GitHub Desktop.
Save alebianco/4386769 to your computer and use it in GitHub Desktop.
Work with ByteArray on an AIR Native Extension
/**
* Project: ANE-Google-Analytics
*
* Author: Alessandro Bianco
* Website: http://alessandrobianco.eu
* Twitter: @alebianco
* Created: 23/12/12 10.42
*
* Copyright © 2013 Alessandro Bianco
*/
package eu.alebianco.air.extensions.analytics.functions;
import android.util.Log;
import com.adobe.fre.FREByteArray;
import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;
import java.nio.ByteBuffer;
public class SendBytes implements FREFunction {
@Override
public FREObject call(FREContext context, FREObject[] args) {
try {
FREByteArray ba = (FREByteArray) args[0];
ba.acquire();
ByteBuffer bb = ba.getBytes();
byte[] bytes = new byte[(int) ba.getLength()];
bb.get(bytes);
String str = new String(bytes);
Log.d("ANE", "My ByteArray is long " + bytes.length + " bytes and contains '" + str + "'");
ba.release();
} catch (Exception e) {
Log.e("ANE", "An error occurred while reading the ByteArray", e);
}
FREByteArray result = null;
try {
result = FREByteArray.newByteArray();
FREObject content = FREObject.newObject("hello to you!");
FREObject[] params = new FREObject[] {content};
result.callMethod("writeUTFBytes", params);
} catch (Exception e) {
Log.e("ANE", "An error occurred while writing the ByteArray", e);
}
return result;
}
}
// After initializing the extension ...
var ba:ByteArray = new ByteArray()
ba.writeUTFBytes("hello world");
var reply:ByteArray = context.call("sendBytes", ba) as ByteArray;
trace(reply.readUTF());
@Oldes
Copy link

Oldes commented Sep 23, 2016

Thank you! It is exactly what I was searching for and could not found anywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment