es.tid.protocol.commons.ByteHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of network-protocols Show documentation
Show all versions of network-protocols Show documentation
BGP-LS,OSPF-TE,PCEP and RSVP-TE protocol encodings.
The newest version!
package es.tid.protocol.commons;
import java.nio.ByteBuffer;
import java.util.BitSet;
public class ByteHandler {
/*
*
*/
/**
* Returns an int from the bytes between begin and end.
* and end position too.
* @param begin position of the start
* @param end position of the end
* @param bytes Bytes to copy
* @return int number
*/
public static int easyCopy(int begin,int end,byte ... bytes)
{
int dest = 0;
if ((begin<0)||(begin>end)||(bytes.length*8<=end))
{
//throw new Exception();
}
byte[] bCleared = new byte[bytes.length];
for (int i=0;iend/8))
{
bCleared[i]=0;
}
else
{
bCleared[i]=bytes[i];
}
}
byte forClearing = 0;
if (begin/8 == end/8)
{
for (int i = 0; i < end-begin+1;i++)
{
forClearing = (byte)(((0xFF & forClearing) << 1) | 0x01) ;
}
forClearing = (byte)((0xFF & forClearing) << (8-(end%8)-1));
bCleared[end/8] = (byte)((0xFF & bCleared[end/8]) & (0xFF & forClearing));
}
else
{
for (int i = 0; i < 8-begin%8;i++)
{
forClearing = (byte)(((0xFF & forClearing) << 1) | 0x01) ;
}
bCleared[begin/8] = (byte)((0xFF & bCleared[begin/8]) & (0xFF & forClearing));
forClearing = 0;
for (int i = 0; i < end%8+1;i++)
{
forClearing = (byte)(((0xFF & forClearing) << 1) | (byte)0x01) ;
}
forClearing = (byte)(forClearing << (8-end%8-1));
bCleared[end/8] = (byte) (0xFF & ((0xFF & (bCleared[end/8]) & (0xFF & forClearing))));
}
if(begin/8 == end/8)
{
dest = (0xFF & ((0xFF & bCleared[end/8]) >> (8 - end%8 -1)));
}
else
{
dest = bCleared[begin/8] & 0xFF;
for(int i = begin/8+1;i < end/8;i++)
{
dest = dest << 8 | (0xFF & bCleared[i]);
}
dest = dest << (end%8+1) | (0xFF & ((0xFF & bCleared[end/8]) >> (8 - end%8-1)));
}
return dest;
}
/*
*
*/
/**
* Returns a long from the bytes between begin and end.
* @param begin position of the start
* @param end position of the end
* @param bytes Bytes to take the number
* @return long number
*/
public static long easyCopyL(int begin,int end,byte ... bytes) //throws Exception
{
long dest = 0;
if ((begin<0)||(begin>end)||(bytes.length*8<=end)||(end-begin>63))
{
//throw new Exception();
//Throw excepcion in futuru, -1 is a valid return value!!
return -1;
}
byte[] bCleared = new byte[bytes.length];
for (int i=0;iend/8))
{
bCleared[i]=0;
}
else
{
bCleared[i]=bytes[i];
}
}
byte forClearing = 0;
if (begin/8 == end/8)
{
for (int i = 0; i < end-begin+1;i++)
{
forClearing = (byte)(((0xFF & forClearing) << 1) | 0x01) ;
}
forClearing = (byte)((0xFF & forClearing) << (8-end-1));
bCleared[end/8] = (byte)((0xFF & bCleared[end/8]) & (0xFF & forClearing));
}
else
{
for (int i = 0; i < 8-begin%8;i++)
{
forClearing = (byte)(((0xFF & forClearing) << 1) | 0x01) ;
}
bCleared[begin/8] = (byte)((0xFF & bCleared[begin/8]) & (0xFF & forClearing));
forClearing = 0;
for (int i = 0; i < end%8+1;i++)
{
forClearing = (byte)(((0xFF & forClearing) << 1) | (byte)0x01) ;
}
forClearing = (byte)(forClearing << (8-end%8-1));
bCleared[end/8] = (byte) (0xFF & ((0xFF & (bCleared[end/8]) & (0xFF & forClearing))));
}
if(begin/8 == end/8)
{
dest = (0xFF & ((0xFF & bCleared[end/8]) >> (8 - end%8 -1)));
}
else
{
dest = bCleared[begin/8] & 0xFF;
for(int i = begin/8+1;i < end/8;i++)
{
dest = dest << 8 | (0xFF & bCleared[i]);
}
dest = dest << (end%8+1) | (0xFF & ((0xFF & bCleared[end/8]) >> (8 - end%8-1)));
}
return dest;
}
public static byte[] toByteArray(BitSet bits, int length) {
byte[] bytes = new byte[(length + 7) / 8];
for (int i=0; i 0)
sb.append(':');
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static String ByteDataPathToString(byte[] mac)
{
StringBuilder sb = new StringBuilder(16);
for (byte b : mac) {
if (sb.length() > 0)
sb.append(':');
sb.append(String.format("%02x", b));
}
return sb.toString();
}
/**
* macAddress = "AA:BB:CC:DD:EE:FF";
* @param macAddress MAC address in String format
* @return byte array
*/
public static byte[] MACFormatStringtoByteArray(String macAddress)
{
String[] macAddressParts = macAddress.split(":");
// convert hex string to byte values
byte[] macAddressBytes = new byte[macAddressParts.length];
for(int i=0; i>> 24);
offset+=1;
bytes[offset]=(byte)(number_int >> 16 & 0xff);
offset+=1;
bytes[offset]=(byte)(number_int >> 8 & 0xff);
offset+=1;
bytes[offset]=(byte)(number_int & 0xff);
}
public static void encode4bytesLong(long number, byte[] bytes, int offset ){
bytes[offset]=(byte)((number>>24) & 0xFF);
bytes[offset+1]=(byte)((number>>16) & 0xFF);
bytes[offset+2]=(byte)((number>>8) & 0xFF);
bytes[offset+3]=(byte)(number & 0xFF);
}
public static void encode1byteInteger(int number, byte[] bytes, int offset ){
bytes[offset]=(byte)(number & 0xFF);
}
public static void encode2bytesInteger(int number, byte[] bytes, int offset ){
bytes[offset]=(byte)((number>>8) & 0xFF);
bytes[offset+1]=(byte)(number & 0xFF);
}
public static void encode3bytesInteger(int number, byte[] bytes, int offset ){
bytes[offset]=(byte)((number>>16) & 0xFF);
bytes[offset+1]=(byte)((number>>8) & 0xFF);
bytes[offset+2]=(byte)(number & 0xFF);
}
public static float decode1Float(byte[]bytes,int offset) {
float number = 0;
int number_int = 0;
for (int k = 0; k < 4; k++) {
number_int = (number_int << 8) | (bytes[k+offset] & 0xff);
}
number=Float.intBitsToFloat(number_int);
return number;
}
public static long decode4bytesLong( byte[] bytes, int offset) {
long number=0;
number=( (((long)bytes[offset]&(long)0xFF)<<24) | (((long)bytes[offset+1]&(long)0xFF)<<16) |( ((long)bytes[offset+2]&(long)0xFF)<<8) | ((long)bytes[offset+3]& (long)0xFF) );
return number;
}
/**
* Get an Integer value from 2 bytes of a byte array
* @param bytes bytes to examine
* @param offset starting position of the 2 byte integer
* @return the integer value
*/
public static int decode2bytesInteger( byte[] bytes, int offset) {
int number=0;
number=( ( ((int)bytes[offset]&(int)0xFF)<<8) | ((int)bytes[offset+1]& (int)0xFF) );
return number;
}
/**
* Get an Integer value from 1 byte of a byte array
* @param bytes bytes to examine
* @param offset offset starting position of the 1 byte integer
* @return the integer value
*/
public static int decode3bytesInteger( byte[] bytes, int offset) {
int number=0;
number=((((int)bytes[offset]&(int)0xFF)<<16) |(((int)bytes[offset+1]&(int)0xFF)<<8) | ((int)bytes[offset+2]& (int)0xFF) );
return number;
}
/**
* Get an Integer value from 1 byte of a byte array
* @param bytes bytes to examine
* @param offset offset starting position of the 1 byte integer
* @return the integer value
*/
public static int decode1byteInteger( byte[] bytes, int offset) {
int number=0;
number=((int)bytes[offset]&(int)0xFF) ;
return number;
}
}