working on it ...

Filters

Explore Public Snippets

Sort by

Found 50 snippets matching: bytearray

    public by cghersi  92294  0  4  0

    byteArrayCompare

     public static boolean byteArrayCompare(byte[] array1, byte[] array2) {
    		if (array1.length != array2.length){
    			return false;
    		}
    		for (int i=0;i<array1.length;i++){
    			if (array1[i] != array2[i]){
    				return false;
    			}
    		}
    		return true;
     }
    

    public by cghersi  1876  0  5  0

    Java: from byteArray To Long

    Method to turn 4-byte array into a long. We can't use an int as there's no concept of unsigned in Java
    public static long byteArrayToLong (byte[] arr, int start, boolean swap) {
    	int i = 0;
    	long accum = 0;
    	
    	//fill a temp array for the computation:
    	byte[] tmp = new byte[4];
    	for (i = 3; i >= 0; i--) {
    		tmp[i] = arr[start++];
    	}
    	if (swap)
    		tmp = swap(tmp);
    	
    	//compute the returned value:
    	i = 0;
    	for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
    		accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
    		i++;
    	}
    	return accum;
    }
    

    public by zonaro  2960  1  7  0

    Easy Write Bytes to File

    This snnipet write (or overwrite) a ByteArray to file on specific path and return a FileInfo of the created file
    public static FileInfo ToFile(this byte[] ByteArray, string FilePath)
            { 
                if (File.Exists(FilePath)) { File.Delete(FilePath); }
                System.IO.FileStream fs = new System.IO.FileStream(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                fs.Write(ByteArray, 0, ByteArray.Length);
                fs.Close();
                return new FileInfo(System.IO.Path.GetFileName(FilePath)); 
            }

    public by guantalamo56  88  1  4  0

    UWP: retrieve the binary content of a file, given its URL

    This is meant to be used with images, but can be applied to any kind of binary content...
    public static async Task<byte[]> ToByteArrayImage(this Uri imageUri)
    {
    	RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(imageUri);
    	IRandomAccessStreamWithContentType streamWithContent = await streamRef.OpenReadAsync();
    	byte[] buffer = new byte[streamWithContent.Size];
    	await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
    	return buffer;
    }				

    public by cghersi  1508  1  4  0

    byteArrayToInt

    public static int byteArrayToInt (byte[] arr, int start, boolean firstIsMVB) {
    	int low = arr[start] & 0xff;
    	int high = arr[start+1] & 0xff;
    	if (firstIsMVB)
    		return (int)( high | (low << 8));
    	else
    		return (int)( high << 8 | low );
    }
    

    public by msdn  80377  0  6  0

    ByteArrayTo

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    
    public static object ByteArrayTo<T>(byte[] input)
    {
        DataContractSerializer serializer = new DataContractSerializer(typeof(T));
        using (var ms = new MemoryStream(input))
        {
            return serializer.ReadObject(ms);
        }
    }

    external by ypetya  34  0  1  0

    Serialize and deserialize java Object into a bytearray containing GZIP-ped data.

    Serialize and deserialize java Object into a bytearray containing GZIP-ped data.: GZip.java
    package tools.distribution.transmission.serialization;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.logging.Level;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    
    import tools.distribution.Factory;
    import tools.distribution.protocol.Message;
    /**
     * Basic implementation of Marshaller via java.util.GZIPStream with default settings
     * */
    public class GZip extends Serialization implements CapableToSerializeIntoAByteArray {
    
        public GZip(Factory f) {
            super(f);
        }
    
        @Override
        public byte[] marshall(Message input) {
            
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                GZIPOutputStream os = new GZIPOutputStream(bos);
                ObjectOutputStream oos = new ObjectOutputStream(os);
                oos.writeObject(input);
                oos.close();
                os.close();
                bos.close();
            } catch (IOException e) {
                getLogger().log(Level.WARNING, "could not marshal", e);
            }
            return bos.toByteArray();
        }
     
        @Override
        public Message unmarshall(byte[] input) {
            ByteArrayInputStream bis = new ByteArrayInputStream(input);
            Message ret = null;
            try {
                GZIPInputStream gis = new GZIPInputStream(bis);
                ObjectInputStream ois = new ObjectInputStream(gis);
                ret = (Message)ois.readObject();
                ois.close();
                gis.close();
                bis.close();
            } catch (Exception e) {
                getLogger().log(Level.WARNING, "could not unmarshal", e);
            }
            return ret;
        }
    
    }
    
    
    

    external by marksvdev  11  0  1  0

    dexcom share bytearray last egvpage response

    dexcom share bytearray last egvpage response: egvresponse
    byte[] array = { 0x01, 0x16, 0x02, 0x01, 0xec, 0x23, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x02, 0xf2, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] array = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9f, 0x3c, 0xfc, 0xbc, 0xc0, 0x0a, 0x6b, 0x9a, 0x1f, 0x0b };
    byte[] array = { 0x6e, 0x00, 0x14, 0xc7, 0x03, 0x28, 0xbe, 0xc0, 0x0a, 0x97, 0x9b, 0x1f, 0x0b, 0x6e, 0x00, 0x14, 0x1b, 0x2c, 0x54, 0xbf };
    byte[] array = { 0xc0, 0x0a, 0xc3, 0x9c, 0x1f, 0x0b, 0x6e, 0x00, 0x14, 0x15, 0xe8, 0x80, 0xc0, 0xc0, 0x0a, 0xef, 0x9d, 0x1f, 0x0b, 0x6e };
    byte[] array = { 0x00, 0x14, 0x1d, 0xd1, 0xac, 0xc1, 0xc0, 0x0a, 0x1b, 0x9f, 0x1f, 0x0b, 0x6e, 0x00, 0x14, 0xf5, 0x82, 0xd8, 0xc2, 0xc0 };
    byte[] array = { 0x0a, 0x47, 0xa0, 0x1f, 0x0b, 0x6e, 0x00, 0x14, 0x9c, 0x8b, 0x04, 0xc4, 0xc0, 0x0a, 0x73, 0xa1, 0x1f, 0x0b, 0x6e, 0x00 };
    byte[] array = { 0x14, 0x43, 0x4e, 0x2f, 0xc5, 0xc0, 0x0a, 0x9f, 0xa2, 0x1f, 0x0b, 0x6e, 0x00, 0x14, 0x60, 0xd5, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    byte[] array = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd0, 0xb5 };
    
    
    

    external by Mike Almond  378  0  3  0

    SharedMemory - Use with the Haxe flash.Memory API for fast (shared) ByteArray access.

    SharedMemory - Use with the Haxe flash.Memory API for fast (shared) ByteArray access.: SharedMemory.hx
    package;
    
    import flash.Memory;
    import flash.utils.ByteArray;
    import flash.utils.Endian;
    import flash.Vector;
    
    /**
     * ...
     * @author Mike Almond - https://github.com/mikedotalmond
     *
     */
    
     /**
      * SharedMemory - Use with the Haxe flash.Memory API for fast ByteArray access.
      *
      * Uses a single ByteArray and allocates chunks of it as needed.
      * @see http://haxe.org/api/flash/Memory
      *
      *
      * Note:
      *
      * SharedMemory only selects the bytearray for manipulation using Memory.select() once.
      * If you're using Memory.select elsewhere - on a different ByteArray - you'll have to
      * call selectMemory() here before reading/writing with the Memory API
      *
      * However, regularly calling Memory.select is not advised if you're concernerd about performance.
      * Have one instance of SharedMemory and use that to allocate all the bytes you need.
      *
      */
    
    @:final enum MemoryDataTypes {
    	Double;
    	Float;
    	Int32;
    	UInt16;
    	Byte;
    }
    
    @:final class SharedMemory {
    	
    	static var m:SharedMemory = null;
    	
    	/**
    	 * @param	size - The size in Bytes of the underlying SharedMemory ByteArray. This is the maximum size that can allocated in total, by calls to .alloc()
    	 * @param	grow - Can the SharedMemory grow as needed [true], or should it never exceed the size you specify [false]
    	 * @throws [ReferenceError]
    	 * @return The SharedMemory instance
    	 */
    	public static function initialise(size:Int = Size_1K, grow:Bool=false):SharedMemory {
    		if (m == null) m = new SharedMemory(size, grow);
    		else throw '[InitialisationError] The static SharedMemory.instance has already been initialised:\n${m.toString()}';
    		return m;
    	}
    	
    	
    	/**
    	 * Access the static SharedMemory instance
    	 * @throws [ReferenceError] if not initialised via SharedMemory.initialise beforhand.
    	 */
    	public static var instance(get_instance, null):SharedMemory;
    	static function get_instance() {
    		#if debug
    		if (m == null) throw '[ReferenceError] Oops! You need to initialise the static SharedMemory instance first! Call SharedMemory.initialise before trying to access SharedMemory.instance';
    		#end
    		return m;
    	}
    	
    	
    	/**
    	 * Release everything. You will need to call initialise again to use SharedMemory
    	 */
    	public static function dispose() {
    		if (m != null) {
    			
    			m.active 		= null;
    			m.free 			= null;
    			m.occupancy 	= 0;
    		
    			// deselect
    			Memory.select(null);
    			
    			if (m.bytes != null) {
    				m.bytes.length = 0;
    				m.bytes = null;
    			}
    			
    			m = null;
    		}
    	}
    	
    	
    	// -----------
    	
    	
    	static var BlockId					:UInt = 0; // unique id for each MemoryBlock, keeps incrementing as blocks are allocated
    	
    	public static inline var Size_1K	:Int = 1 << 10; // This is the minimum size -- 0x400 (1024)
    	public static inline var Size_2K	:Int = 2 << 10;
    	public static inline var Size_4K	:Int = 4 << 10;
    	public static inline var Size_8K	:Int = 8 << 10;
    	public static inline var Size_16K	:Int = 16 << 10;
    	public static inline var Size_32K	:Int = 32 << 10;
    	public static inline var Size_64K	:Int = 64 << 10;
    	public static inline var Size_128K	:Int = 128 << 10;
    	public static inline var Size_256K	:Int = 256 << 10;
    	public static inline var Size_512K	:Int = 512 << 10;
    	public static inline var Size_1024K	:Int = 1024 << 10;
    	
    	var active	:Map<Int, MemoryBlock>;
    	var free	:Map<Int, MemoryBlock>;
    	
    	public var grow(default, null)		:Bool;
    	public var bytes(default, null)		:ByteArray;
    	public var occupancy(default, null)	:Int;
    	
    	public var waste(get_waste, null)	:Int;
    	function get_waste() { return size - occupancy; }
    	
    	public var size(get_size, null)		:UInt;
    	function get_size() { return bytes==null ? 0 : bytes.length; }
    	
    	public function toString() {
    		return '> [SharedMemory]' + ((m != null)
    		? (	'\n>> size:${size}, grow:${grow}' +
    			'\n>> occupancy:${occupancy}, waste:${waste}' +
    			'\n>> activeBlockCount:${Lambda.count(active)}, freeBlockCount:${Lambda.count(free)}'
    		) : 'Uninitialised') ;
    	}
    	
    	
    	/**
    	 * @private
    	 * @param	size 	- Number of bytes to allocate initially. The minimum size is 1K (1024 bytes)
    	 * @param	grow	- Allows the bytearray to grow if needed.
    	 */
    	function new(size:Int = Size_1K, grow:Bool = false) {
    		
    		if (size < Size_1K) {
    			throw "Minimum allocation size is 1K - 1024 bytes";
    			size = Size_1K;
    		}
    		
    		bytes 			= new ByteArray();
    		bytes.endian 	= Endian.LITTLE_ENDIAN; // Note: "The endian property of the selected ByteArray does not affect the[se] Memory functions. They behave as if it is set to Endian.LITTLE_ENDIAN."
    		bytes.length 	= size;
    		
    		occupancy		= 0;
    		
    		active			= new Map<Int, MemoryBlock>();
    		free			= new Map<Int, MemoryBlock>();
    		
    		this.grow 		=  grow;
    		
    		selectMemory();
    	}
    	
    	
    	/**
    	 * You only need to call this before reading/writing if using Memory.select(someOtherByteArray); elsewhere in your code....
    	 * That's not ideal though. If possible try to only use one ByteArray and manage it here.
    	 */
    	public inline function selectMemory() {
    		Memory.select(bytes);
    	}
    	
    	
    	/**
    	 * Get an allocated MemoryBlock
    	 * MemoryBlock contains information about a section of the shared bytearray (id, startIndex, length)
    	 *
    	 * @param	id
    	 * @return	MemoryBlock
    	 */
    	public function get(id:Int):MemoryBlock {
    		if (active.exists(id)) return active.get(id);
    		else return null;
    	}
    	
    	
    	/**
    	 *
    	 * @param	size	- Size, in bytes, to allocate
    	 * @return	id		- The ID for the block of memory allocated, or -1 if there wasn't enough space left to allocate the requested size (when grow==false)
    	 */
    	public function alloc(size:Int):Int {
    		
    		var start = getStartIndex(size);
    		if (start == -1) return -1; // no space, and no grow?
    		
    		occupancy += size;
    		
    		var block = new MemoryBlock(start, size);
    		active.set(block.id, block);
    		
    		return block.id;
    	}
    	
    	
    	// shortcuts
    	public inline function allocDouble(count:UInt=1):Int return allocType(MemoryDataTypes.Double, count);
    	public inline function allocFloat(count:UInt=1):Int return allocType(MemoryDataTypes.Float, count);
    	public inline function allocInt32(count:UInt=1):Int return allocType(MemoryDataTypes.Int32, count);
    	public inline function allocUInt16(count:UInt=1):Int return allocType(MemoryDataTypes.UInt16, count);
    	
    	
    	
    	/**
    	 * Helper that allocates the space needed for <code>count</code> number of <code>type</code> datas
    	 * @param	type	Type of data to allocate
    	 * @param	count	Number of items to allocate space for
    	 * @return	id		The ID for the block of memory allocated, or -1 if there wasn't enough space left to allocate the requested size (when grow==false)
    	 */
    	function allocType(type:MemoryDataTypes, count:UInt):Int {
    		switch(type) {
    			
    			case MemoryDataTypes.Byte	: // ... 1 byte/value
    				return alloc(count);
    			
    			case MemoryDataTypes.UInt16	:
    				return alloc(count << 1); 	// 2 bytes/value
    			
    			case MemoryDataTypes.Int32,
    				 MemoryDataTypes.Float	:
    				return alloc(count << 2); 	// 4 bytes/value
    			
    			case MemoryDataTypes.Double	:
    				return alloc(count << 3); 	// 8 bytes/value
    		}
    		
    		return -1;
    	}
    	
    	
    	
    	/**
    	 * Release a block of Memory for re-use
    	 * @param	id
    	 * @return true if released, or false if that id doesn't exist
    	 */
    	public function release(id:Int):Bool {
    		
    		if (active.exists(id)) {
    			
    			var block = active.get(id);
    			
    			active.remove(id);
    			free.set(block.id, block);
    			
    			occupancy -= block.size;
    			
    			return true;
    		}
    		
    		return false;
    	}
    	
    	
    	 /**
    	  * Move all unallocated blocks to the end of the available memory, making a single contiguous 'free' space at the end of the bytearray.
    	  * It's probably not that speedy so don't call too regularly.
    	  *
    	  * @param	?reduceSize - when true any freespace is trimmed, so size==ocupancy and waste==0
    	  */
    	public function defrag(?reduceSize:Bool=false) {
    		
    		if (!Lambda.empty(free)) { // got something to defrag?
    			
    			var b = new ByteArray(); // temporary copy-buffer
    			
    			var allBlocks:Vector<MemoryBlock> = Vector.ofArray(Lambda.array(Lambda.concat(free, active)));
    			allBlocks.sort(sortBlocksByIndex);
    			allBlocks.fixed = true;
    			
    			var freeBlockIndex:Int = nextBlockIndex(allBlocks, 0, false); // first free block
    			var dataBlockIndex:Int = nextBlockIndex(allBlocks, freeBlockIndex, true);
    			
    			while (freeBlockIndex != -1 && dataBlockIndex != -1 && freeBlockIndex!=dataBlockIndex) {
    				
    				//trace('freeBlockIndex:${freeBlockIndex} dataBlockIndex:${dataBlockIndex}');
    				
    				var freeBlock 	= allBlocks[freeBlockIndex];
    				var dataBlock	= allBlocks[dataBlockIndex];
    					
    				b.length = 0;
    				b.position = 0;
    				
    				// read bytes from the active block into temporary buffer
    				bytes.position = dataBlock.index; // move bytes pointer to dataBlock start
    				bytes.readBytes(b, 0, dataBlock.size);
    				
    				// b should now contain dataBlock
    				
    				b.position = 0;
    				// read the data block back into bytes at the start position of the free-block
    				b.readBytes(bytes, freeBlock.index, dataBlock.size);
    				
    				// update allocated data start index
    				dataBlock.index = freeBlock.index;
    				
    				// update free block start and size
    				freeBlock.index += dataBlock.size;
    				freeBlock.size  -= dataBlock.size;
    				
    				if (freeBlock.size == 0) {
    					free.remove(freeBlock.id);
    					allBlocks[freeBlockIndex] = null;
    				}
    					
    				while (freeBlockIndex < allBlocks.length - 1 && free.exists(allBlocks[freeBlockIndex + 1].id)) {
    					// while next block is also free, consolidate the free-space into a single block
    					
    					var nextFreeBlock 	= allBlocks[freeBlockIndex + 1];
    					
    					freeBlock.size 		+= nextFreeBlock.size;
    					
    					free.remove(nextFreeBlock.id);
    					allBlocks[freeBlockIndex + 1] = null;
    					
    					freeBlockIndex++;
    				}
    				
    				freeBlockIndex 	= nextBlockIndex(allBlocks, freeBlockIndex, false);
    				dataBlockIndex 	= nextBlockIndex(allBlocks, freeBlockIndex, true); // next allocated block after free block
    			}
    			
    			
    			// all freespace is now at the end of the bytearray
    			if (reduceSize) { // remove unused bytes. 1K is the minimum allowed size.
    				if (bytes.length - waste >= Size_1K) {
    					bytes.length -= waste;
    				} else {
    					bytes.length = Size_1K;
    				}
    			}
    		}
    	}
    	
    	
    	/**
    	 *
    	 * @param	items
    	 * @param	start
    	 * @param	allocated
    	 * @return
    	 */
    	function nextBlockIndex(items:Vector<MemoryBlock>, start:Int, allocated:Bool):Int {
    		
    		if (start != -1) {
    			
    			for (i in start...items.length) {
    				var item = items[i];
    				if (item != null && item.size > 0) {
    					if (allocated && active.exists(item.id)) {
    						return i;
    					} else if (!allocated && free.exists(item.id)) {
    						return i;
    					}
    				}
    			}
    		}
    		
    		return -1;
    	}
    	
    	
    	
    	/**
    	 *
    	 * @param	size
    	 * @return	free-block start index, -1 if no free blocks
    	 */
    	function getStartIndex(size:Int):Int {
    		var index 	= occupancy;
    		var end 	= index + size;
    		
    		if (end > cast bytes.length) {
    			// have space to fill?
    			if (hasFreespace(size)) {
    				index = fillFreespace(size);
    			} else if (grow) {
    				// can grow?
    				index = bytes.length;
    				bytes.length = end;
    			} else {
    				// no space, can't alloc
    				index = -1;
    			}
    		}
    		
    		return index;
    	}
    	
    	
    	/**
    	 *
    	 * @param	size
    	 * @return
    	 */
    	function hasFreespace(size:Int):Bool {
    		for (b in free) { if (b.size >= size) return true;  }
    		return false;
    	}
    	
    	
    	/**
    	 *
    	 * @param	size
    	 * @return
    	 */
    	function fillFreespace(size:Int):Int {
    		
    		for (b in free) {
    			if (b.size >= size) {
    				
    				var index = b.index;
    				
    				b.index += size;
    				b.size 	-= size;
    				
    				if (b.size == 0) free.remove(b.id);
    				
    				return index;
    			}
    		}
    		
    		return -1;
    	}
    	
    	static inline function sortBlocksByIndex(a, b) {
    		return (a.index < b.index) ? -1 : (a.index == b.index ? 0 : 1);
    	}
    }
    
    
    @:access(mikedotalmond.labs.util.SharedMemory) // get access to SharedMemory privates
    @:final class MemoryBlock {
    	
    	public var id			(default, null):UInt;   // unique id
    	public var index		(default, default):Int; // address start index - MemoryBlock indices can change after a call to defrag, so keep a reference to the block (or it's ID) for later if needed.
    	public var size			(default, default):Int;
    	
    	public var end			(get_end, null):Int;
    	inline function get_end() { return index + size; }
    	
    	
    	public function new(index:Int, size:Int) {
    		id 			= SharedMemory.BlockId;
    		this.index 	= index;
    		this.size 	= size;
    		SharedMemory.BlockId++;
    	}
    	
    	public function toString() {
    		return '[MemoryBlock] ${id} - start:${index}, size:${size}, end:${end}';
    	}
    }
    
    

    external by cornetto  214  0  3  0

    ByteArray

    ByteArray: byteArray.java
    package com.test.bytearray;
    
    
    public class byteArrayTest {
    
    	private static Boolean isBitSet(byte b, int bit) {
    		return (b & (1 << bit)) != 0;
    	}
    
    	public static byte[] integerToByteArray(int data) {
    		byte[] toReturn = new byte[] { (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), };
    		for (byte b : toReturn) {
    			System.out.format("0x%x ", b);
    		}
    		return toReturn;
    
    	}
    
    	public static byte[] valueToByteArray(short data) {
    		byte[] toReturn = new byte[] { (byte) ((data >> 0) & 0xff) };
    
    		for (byte b : toReturn) {
    			System.out.format("0x%x ", b);
    		}
    		return toReturn;
    
    	}
    
    	public static byte[] shortToByteArray(short data) {
    		byte[] toReturn = new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), };
    
    		for (byte b : toReturn) {
    			System.out.format("0x%x ", b);
    		}
    		return toReturn;
    	}
    
    	public static short byteArrayToValue(byte[] data) {
    		if (data == null || data.length != 1)
    			return 0x0;
    		return (short) ((0xff & data[0]) << 0);
    	}
    
    	public static short byteArrayToShort(byte[] data) {
    		if (data == null || data.length != 2)
    			return 0x0;
    
    		return (short) ((0xff & data[0]) << 8 | (0xff & data[1]) << 0);
    	}
    
    	public static int byteArrayToInteger(byte[] data) {
    		if (data == null || data.length != 4)
    			return 0x0;
    
    		return (int) ((0xff & data[0]) << 24 | (0xff & data[1]) << 16 | (0xff & data[2]) << 8 | (0xff & data[3]) << 0);
    	}
    
    	public static void main(String[] args) {
    
    		integerToByteArray(1412);
    	 
    	}
    
    }
    
    
    
    • Public Snippets
    • Channels Snippets