Android Open Source - lifx-wear-test L F X Byte Utils






From Project

Back to project page lifx-wear-test.

License

The source code is released under:

Apache License

If you think the Android project lifx-wear-test listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//
//  LFXByteUtils.java
//  LIFX/*w w w . j  ava  2  s .c o m*/
//
//  Created by Jarrod Boyes on 24/03/14.
//  Copyright (c) 2014 LIFX Labs. All rights reserved.
//

package lifx.java.android.util;

public class LFXByteUtils 
{  
  public static boolean areByteArraysEqual( byte[] byteArray0, byte[] byteArray1)
  {
    if( byteArray0 == null && byteArray1 != null)
    {
      return false;
    }
    else if( byteArray0 != null && byteArray1 == null)
    {
      return false;
    }
    else if( byteArray0 == null && byteArray1 == null)
    {
      return true;
    }
    
    if( byteArray0.length != byteArray1.length)
    {
      return false;
    }
    
    for( int i = 0; i < byteArray0.length; i++)
    {
      if( byteArray0[i] != byteArray1[i])
      {
        return false;
      }
    }
    
    return true;
  }
  
  public static byte[] hexStringToByteArray(String s) 
  {
        int len = s.length();
        
        byte[] data = new byte[len/2];

        for( int i = 0; i < len; i+=2)
        {
            data[i/2] = (byte) (( Character.digit( s.charAt(i), 16) << 4) + Character.digit( s.charAt(i+1), 16));
        }

        return data;
    }

  final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  
  public static String byteArrayToHexString( byte[] bytes) 
  {
      char[] hexChars = new char[bytes.length*2];
      int v;
  
      for( int j = 0; j < bytes.length; j++) 
      {
          v = bytes[j] & 0xFF;
          hexChars[ j * 2 ] = hexArray[ v>>>4 ];
          hexChars[ j * 2 + 1 ] = hexArray[ v & 0x0F ];
      }
  
      return new String(hexChars);
  }
  
  public static void copyBytesIntoByteArrayAtOffset( byte[] dest, byte[] src, int offset)
  {
    for( int i = 0; i < src.length; i++)
    {
      dest[offset + i] = src[i];
    }
  }
  
  public static void copyBytesIntoByteArray( byte[] dest, byte[] src)
  {
    copyBytesIntoByteArrayAtOffset( dest, src, 0);
  }
  
  public static void copyBytesIntoByteArrayUpToLength( byte[] dest, byte[] src, int offset)
  {
    for( int i = 0; i < offset; i++)
    {
      dest[i] = src[i];
    }
  }
  
  public static void clearByteArray( byte[] bytes)
  {
    for( int i = 0; i < bytes.length; i++)
    {
      bytes[i] = 0;
    }
  }
  
  public static byte[] bitwiseOrByteArrays( byte[] arr0, byte[] arr1)
  {
    byte[] returnArray = new byte[arr0.length];
    
    for( int i = 0; i < arr0.length; i++)
    {
      returnArray[i] = (byte)( arr0[i] | arr1[i]);
    }
    
    return returnArray;
  }
  
  public static byte[] bitwiseAndByteArrays( byte[] arr0, byte[] arr1)
  {
    byte[] returnArray = new byte[arr0.length];
    
    for( int i = 0; i < arr0.length; i++)
    {
      returnArray[i] = (byte)( arr0[i] & arr1[i]);
    }
    
    return returnArray;
  }
  
  public static byte[] inverseByteArrayBits( byte[] arr0)
  {
    byte[] returnArray = new byte[arr0.length];
    
    for( int i = 0; i < arr0.length; i++)
    {
      returnArray[i] = (byte) ~arr0[i];
    }
    
    return returnArray;
  }
  
  public static boolean isByteArrayEmpty( byte[] bytes)
  {
    for( int i = 0; i < bytes.length; i++)
    {
      if( bytes[i] != 0x00)
      {
        return false;
      }
    }
    
    return true;
  }
  
  public static boolean isBitSet( byte[] arr, int bit) 
  {
      int index = bit / 8;        // Get the index of the array for the byte with this bit
      int bitPosition = bit % 8;    // Position of this bit in a byte

      return (arr[index] >> bitPosition & 1) == 1;
  }
  
  public static void setBit( byte[] arr, int bit) 
  {
      int index = bit / 8;        // Get the index of the array for the byte with this bit
      int bitPosition = bit % 8;    // Position of this bit in a byte

      arr[index] = (byte) (arr[index] | (1 << bitPosition));
  }
}




Java Source Code List

de.dp.lifxtest.app.LightsFragment.java
de.dp.lifxtest.app.MasterActivity.java
lifx.java.android.client.LFXClient.java
lifx.java.android.constant.LFXSDKConstants.java
lifx.java.android.entities.LFXHSBKColor.java
lifx.java.android.entities.LFXLightTarget.java
lifx.java.android.entities.LFXTypes.java
lifx.java.android.entities.internal.LFXBinaryPath.java
lifx.java.android.entities.internal.LFXBinaryTargetID.java
lifx.java.android.entities.internal.LFXBinaryTypes.java
lifx.java.android.entities.internal.LFXDeviceMapping.java
lifx.java.android.entities.internal.LFXGatewayDescriptor.java
lifx.java.android.entities.internal.LFXMessageObservationDescriptor.java
lifx.java.android.entities.internal.LFXMessage.java
lifx.java.android.entities.internal.LFXSiteID.java
lifx.java.android.entities.internal.LFXTagMapping.java
lifx.java.android.entities.internal.LFXTarget.java
lifx.java.android.entities.internal.structle.LxProtocolDevice.java
lifx.java.android.entities.internal.structle.LxProtocolLight.java
lifx.java.android.entities.internal.structle.LxProtocolSensor.java
lifx.java.android.entities.internal.structle.LxProtocolWan.java
lifx.java.android.entities.internal.structle.LxProtocolWifi.java
lifx.java.android.entities.internal.structle.LxProtocol.java
lifx.java.android.entities.internal.structle.Lx.java
lifx.java.android.entities.internal.structle.StructleTypes.java
lifx.java.android.internal.LFXWiFiObserver.java
lifx.java.android.light.LFXLightCollection.java
lifx.java.android.light.LFXLight.java
lifx.java.android.light.LFXTaggedLightCollection.java
lifx.java.android.light.internal.LFXAllLightsCollection.java
lifx.java.android.network_context.LFXNetworkContext.java
lifx.java.android.network_context.internal.routing_table.LFXRoutingTable.java
lifx.java.android.network_context.internal.transport_manager.LFXTransportManager.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXGatewayConnection.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXSocketGeneric.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXSocketTCP.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXSocketUDP.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXTCPGatewayConnection.java
lifx.java.android.network_context.internal.transport_manager.gateway_connection.LFXUDPGatewayConnection.java
lifx.java.android.network_context.internal.transport_manager.lan.LFXLANTransportManager.java
lifx.java.android.network_context.internal.transport_manager.lan.gateway_discovery.LFXGatewayDiscoveryController.java
lifx.java.android.network_context.internal.transport_manager.lan.gateway_discovery.LFXGatewayDiscoveryTableEntry.java
lifx.java.android.util.LFXByteUtils.java
lifx.java.android.util.LFXLog.java
lifx.java.android.util.LFXNetworkUtils.java
lifx.java.android.util.LFXTimerUtils.java