gu.sql2java.generator.ArraySupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-generator Show documentation
Show all versions of sql2java-generator Show documentation
executable jar of sql2java generator
The newest version!
package gu.sql2java.generator;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.io.BaseEncoding;
/**
* copy form sql2java-base
* @author guyadong
* @since 4.2.0
*/
public class ArraySupport {
/**
* cast byte array to HEX string
*
* @param input
* @return {@code null} if {@code input} is null
*/
public static final String toHex(byte[] input) {
if (null == input){
return null;
}
return BaseEncoding.base16().encode(input);
}
/**
* cast HEX string to byte array
* @param input
* @return {@code null} if {@code input} is null
*/
public static final byte[] fromHex(String input){
if (null == input){
return null;
}
return BaseEncoding.base16().decode(input);
}
/**
* cast HEX string to byte array,copy to this destination,same length required with {@code dest}
* @param hex
* @param dest
*/
public static void copyToByteArray(String hex, byte[] dest){
checkArgument(null != hex && null != dest,"hex or dest is null");
byte[] src = fromHex(hex);
checkArgument(src.length == dest.length,"INVALID HEX String length, %s required",dest.length);
System.arraycopy(src, 0, dest, 0, src.length);
}
/**
* return {@code true} if bit specialized by {@code index} in byte array {@code bits} is 1,otherwise {@code false}
* @param bits
* @param index
* @since 4.0.0
*/
public static final boolean bitCheck(byte[]bits,int index){
return 0 != (bits[bits.length - 1 - (index>>3)]&(byte)(1<<(index&0x07)));
}
/**
* set bit specialized by {@code index} in byte array {@code bits} to 1
* @param bits
* @param index
* @return always {@code bits}
* @since 4.0.0
*/
public static final byte[] bitSet(byte[] bits,int index){
bits[bits.length - 1 - (index>>3)] |= (byte)(1<<(index&0x07));
return bits;
}
/**
* set bit specialized by {@code index} in byte array {@code bits} to 0
* @param bits
* @param index
* @return always {@code bits}
* @since 4.0.0
*/
public static final byte[] bitReset(byte[] bits,int index){
bits[bits.length - 1 - (index>>3)] &= (byte)(~(1<<(index&0x07)));
return bits;
}
/**
* set bit specialized by {@code index} in byte array {@code bits} to 0 if {@code set1} is {@code true},otherwise set to 0
* @param bits
* @param index
* @param set1
* @return always {@code bits}
* @since 4.0.0
*/
public static final byte[] bitSet(byte[] bits,int index,boolean set1){
return set1 ? bitSet(bits, index) : bitReset(bits, index);
}
}