Last active
September 23, 2016 14:46
-
-
Save alebianco/4386769 to your computer and use it in GitHub Desktop.
Work with ByteArray on an AIR Native Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! It is exactly what I was searching for and could not found anywhere.