List of usage examples for java.util BitSet valueOf
public static BitSet valueOf(ByteBuffer bb)
From source file:com.google.uzaygezen.core.BitSetBackedBitVector.java
@Override public void copyFromBigEndian(byte[] array) { int len = MathUtils.bitCountToByteCount(size); Preconditions.checkArgument(array.length == len, "Length must be %s.", len); if (len == 0) { return;/*from ww w.j av a 2s . c o m*/ } Preconditions.checkArgument(MathUtils.numberOfLeadingZeros(array[0]) >= (len << 3) - size, "Some bit positions are too high."); BitSet bs; ArrayUtils.reverse(array); try { bs = BitSet.valueOf(array); } finally { ArrayUtils.reverse(array); } bitset.clear(); bitset.or(bs); }
From source file:cn.annoreg.mc.s11n.SerializationManager.java
private void initInternalSerializers() { //First part: java internal class. {//from w ww . ja va2 s. co m InstanceSerializer ser = new InstanceSerializer<Enum>() { @Override public Enum readInstance(NBTBase nbt) throws Exception { NBTTagCompound tag = (NBTTagCompound) nbt; try { Class enumClass = Class.forName(tag.getString("class")); Object[] objs = (Object[]) enumClass.getMethod("values").invoke(null); return (Enum) objs[tag.getInteger("ordinal")]; } catch (Exception e) { ARModContainer.log.error("Failed in enum deserialization. Class: {}.", tag.getString("class")); e.printStackTrace(); return null; } } @Override public NBTBase writeInstance(Enum obj) throws Exception { NBTTagCompound ret = new NBTTagCompound(); ret.setString("class", obj.getClass().getName()); ret.setByte("ordinal", (byte) ((Enum) obj).ordinal()); return ret; } }; setInstanceSerializerFor(Enum.class, ser); } { DataSerializer ser = new DataSerializer<Byte>() { @Override public Byte readData(NBTBase nbt, Byte obj) throws Exception { return ((NBTTagByte) nbt).func_150290_f(); } @Override public NBTBase writeData(Byte obj) throws Exception { return new NBTTagByte(obj); } }; setDataSerializerFor(Byte.TYPE, ser); setDataSerializerFor(Byte.class, ser); } { DataSerializer ser = new DataSerializer<Byte[]>() { @Override public Byte[] readData(NBTBase nbt, Byte[] obj) throws Exception { return ArrayUtils.toObject(((NBTTagByteArray) nbt).func_150292_c()); } @Override public NBTBase writeData(Byte[] obj) throws Exception { return new NBTTagByteArray(ArrayUtils.toPrimitive(obj)); } }; setDataSerializerFor(Byte[].class, ser); } { DataSerializer ser = new DataSerializer<byte[]>() { @Override public byte[] readData(NBTBase nbt, byte[] obj) throws Exception { return ((NBTTagByteArray) nbt).func_150292_c(); } @Override public NBTBase writeData(byte[] obj) throws Exception { return new NBTTagByteArray(obj); } }; setDataSerializerFor(byte[].class, ser); } { DataSerializer ser = new DataSerializer<Double>() { @Override public Double readData(NBTBase nbt, Double obj) throws Exception { return ((NBTTagDouble) nbt).func_150286_g(); } @Override public NBTBase writeData(Double obj) throws Exception { return new NBTTagDouble(obj); } }; setDataSerializerFor(Double.TYPE, ser); setDataSerializerFor(Double.class, ser); } { DataSerializer ser = new DataSerializer<Float>() { @Override public Float readData(NBTBase nbt, Float obj) throws Exception { return ((NBTTagFloat) nbt).func_150288_h(); } @Override public NBTBase writeData(Float obj) throws Exception { return new NBTTagFloat(obj); } }; setDataSerializerFor(Float.TYPE, ser); setDataSerializerFor(Float.class, ser); } { DataSerializer ser = new DataSerializer<Integer>() { @Override public Integer readData(NBTBase nbt, Integer obj) throws Exception { return ((NBTTagInt) nbt).func_150287_d(); } @Override public NBTBase writeData(Integer obj) throws Exception { return new NBTTagInt(obj); } }; setDataSerializerFor(Integer.TYPE, ser); setDataSerializerFor(Integer.class, ser); } { DataSerializer ser = new DataSerializer<Integer[]>() { @Override public Integer[] readData(NBTBase nbt, Integer[] obj) throws Exception { return ArrayUtils.toObject(((NBTTagIntArray) nbt).func_150302_c()); } @Override public NBTBase writeData(Integer[] obj) throws Exception { return new NBTTagIntArray(ArrayUtils.toPrimitive(obj)); } }; setDataSerializerFor(Integer[].class, ser); } { DataSerializer ser = new DataSerializer<int[]>() { @Override public int[] readData(NBTBase nbt, int[] obj) throws Exception { return ((NBTTagIntArray) nbt).func_150302_c(); } @Override public NBTBase writeData(int[] obj) throws Exception { return new NBTTagIntArray(obj); } }; setDataSerializerFor(int[].class, ser); } { DataSerializer ser = new DataSerializer<Long>() { @Override public Long readData(NBTBase nbt, Long obj) throws Exception { return ((NBTTagLong) nbt).func_150291_c(); } @Override public NBTBase writeData(Long obj) throws Exception { return new NBTTagLong(obj); } }; setDataSerializerFor(Long.TYPE, ser); setDataSerializerFor(Long.class, ser); } { DataSerializer ser = new DataSerializer<Short>() { @Override public Short readData(NBTBase nbt, Short obj) throws Exception { return ((NBTTagShort) nbt).func_150289_e(); } @Override public NBTBase writeData(Short obj) throws Exception { return new NBTTagShort(obj); } }; setDataSerializerFor(Short.TYPE, ser); setDataSerializerFor(Short.class, ser); } { DataSerializer ser = new DataSerializer<String>() { @Override public String readData(NBTBase nbt, String obj) throws Exception { return ((NBTTagString) nbt).func_150285_a_(); } @Override public NBTBase writeData(String obj) throws Exception { return new NBTTagString(obj); } }; setDataSerializerFor(String.class, ser); } { //TODO: Maybe there is a more data-friendly method? DataSerializer ser = new DataSerializer<Boolean>() { @Override public Boolean readData(NBTBase nbt, Boolean obj) throws Exception { return ((NBTTagCompound) nbt).getBoolean("v"); } @Override public NBTBase writeData(Boolean obj) throws Exception { NBTTagCompound tag = new NBTTagCompound(); tag.setBoolean("v", obj); return tag; } }; setDataSerializerFor(Boolean.class, ser); setDataSerializerFor(Boolean.TYPE, ser); } //Second part: Minecraft objects. { DataSerializer ser = new DataSerializer<NBTTagCompound>() { @Override public NBTTagCompound readData(NBTBase nbt, NBTTagCompound obj) throws Exception { return (NBTTagCompound) nbt; } @Override public NBTBase writeData(NBTTagCompound obj) throws Exception { return obj; } }; setDataSerializerFor(NBTTagCompound.class, ser); } { InstanceSerializer ser = new InstanceSerializer<Entity>() { @Override public Entity readInstance(NBTBase nbt) throws Exception { int[] ids = ((NBTTagIntArray) nbt).func_150302_c(); World world = SideHelper.getWorld(ids[0]); if (world != null) { return world.getEntityByID(ids[1]); } return null; } @Override public NBTBase writeInstance(Entity obj) throws Exception { return new NBTTagIntArray(new int[] { obj.dimension, obj.getEntityId() }); } }; setInstanceSerializerFor(Entity.class, ser); } { InstanceSerializer ser = new InstanceSerializer<TileEntity>() { @Override public TileEntity readInstance(NBTBase nbt) throws Exception { int[] ids = ((NBTTagIntArray) nbt).func_150302_c(); World world = SideHelper.getWorld(ids[0]); if (world != null) { return world.getTileEntity(ids[1], ids[2], ids[3]); } return null; } @Override public NBTBase writeInstance(TileEntity obj) throws Exception { return new NBTTagIntArray(new int[] { obj.getWorldObj().provider.dimensionId, obj.xCoord, obj.yCoord, obj.zCoord }); } }; setInstanceSerializerFor(TileEntity.class, ser); } { //TODO this implementation can not be used to serialize player's inventory container. InstanceSerializer ser = new InstanceSerializer<Container>() { @Override public Container readInstance(NBTBase nbt) throws Exception { int[] ids = ((NBTTagIntArray) nbt).func_150302_c(); World world = SideHelper.getWorld(ids[0]); if (world != null) { Entity entity = world.getEntityByID(ids[1]); if (entity instanceof EntityPlayer) { return SideHelper.getPlayerContainer((EntityPlayer) entity, ids[2]); } } return SideHelper.getPlayerContainer(null, ids[2]); } @Override public NBTBase writeInstance(Container obj) throws Exception { EntityPlayer player = SideHelper.getThePlayer(); if (player != null) { //This is on client. The server needs player to get the Container. return new NBTTagIntArray(new int[] { player.worldObj.provider.dimensionId, player.getEntityId(), obj.windowId }); } else { //This is on server. The client doesn't need player (just use thePlayer), use MAX_VALUE here. return new NBTTagIntArray(new int[] { Integer.MAX_VALUE, 0, obj.windowId }); } } }; setInstanceSerializerFor(Container.class, ser); } { InstanceSerializer ser = new InstanceSerializer<World>() { @Override public World readInstance(NBTBase nbt) throws Exception { return SideHelper.getWorld(((NBTTagInt) nbt).func_150287_d()); } @Override public NBTBase writeInstance(World obj) throws Exception { return new NBTTagInt(obj.provider.dimensionId); } }; setInstanceSerializerFor(World.class, ser); } { DataSerializer ser = new DataSerializer<ItemStack>() { @Override public ItemStack readData(NBTBase nbt, ItemStack obj) throws Exception { if (obj == null) { return ItemStack.loadItemStackFromNBT((NBTTagCompound) nbt); } else { obj.readFromNBT((NBTTagCompound) nbt); return obj; } } @Override public NBTBase writeData(ItemStack obj) throws Exception { NBTTagCompound nbt = new NBTTagCompound(); obj.writeToNBT(nbt); return nbt; } }; setDataSerializerFor(ItemStack.class, ser); } { DataSerializer ser = new DataSerializer<Vec3>() { @Override public Vec3 readData(NBTBase nbt, Vec3 obj) throws Exception { NBTTagCompound tag = (NBTTagCompound) nbt; return Vec3.createVectorHelper(tag.getFloat("x"), tag.getFloat("y"), tag.getFloat("z")); } @Override public NBTBase writeData(Vec3 obj) throws Exception { NBTTagCompound nbt = new NBTTagCompound(); nbt.setFloat("x", (float) obj.xCoord); nbt.setFloat("y", (float) obj.yCoord); nbt.setFloat("z", (float) obj.zCoord); return nbt; } }; setDataSerializerFor(Vec3.class, ser); } //network part { DataSerializer ser = new DataSerializer<NetworkTerminal>() { @Override public NetworkTerminal readData(NBTBase nbt, NetworkTerminal obj) throws Exception { return NetworkTerminal.fromNBT(nbt); } @Override public NBTBase writeData(NetworkTerminal obj) throws Exception { return obj.toNBT(); } }; setDataSerializerFor(NetworkTerminal.class, ser); } { Future.FutureSerializer ser = new Future.FutureSerializer(); setDataSerializerFor(Future.class, ser); setInstanceSerializerFor(Future.class, ser); } //misc { DataSerializer ser = new DataSerializer<BitSet>() { @Override public BitSet readData(NBTBase nbt, BitSet obj) throws Exception { NBTTagCompound tag = (NBTTagCompound) nbt; BitSet ret = BitSet.valueOf(tag.getByteArray("l")); return ret; } @Override public NBTBase writeData(BitSet obj) throws Exception { NBTTagCompound tag = new NBTTagCompound(); byte[] barray = obj.toByteArray(); tag.setByteArray("l", barray); return tag; } }; setDataSerializerFor(BitSet.class, ser); } }
From source file:com.google.uzaygezen.core.BitVectorTest.java
private void checkToBigEndianByteArrayAndCopyFromBigEndianAreInverse(Function<Integer, BitVector> factory) { Random random = new Random(TestUtils.SEED); for (int sizeUpperLimit = 0; sizeUpperLimit < 128; ++sizeUpperLimit) { byte[] array = new byte[sizeUpperLimit]; random.nextBytes(array);//from w w w. j a va 2 s.c o m BitSet bs = BitSet.valueOf(array); ArrayUtils.reverse(array); int logicalSize = bs.length(); int n = MathUtils.bitCountToByteCount(logicalSize); for (int i = 0; i < array.length - n; ++i) { assert array[i] == 0; } byte[] bigEndian = Arrays.copyOfRange(array, array.length - n, array.length); for (int size = logicalSize; size <= n << 3; ++size) { BitVector bv = factory.apply(size); bv.copyFromBigEndian(bigEndian); byte[] actual = bv.toBigEndianByteArray(); assertArrayEquals(bigEndian, actual); } } }
From source file:org.apache.eagle.alert.engine.serialization.PartitionedEventSerializerTest.java
@Test public void testBitSet() { BitSet bitSet = new BitSet(); bitSet.set(0, true); // 1 bitSet.set(1, false); // 0 bitSet.set(2, true); // 1 LOG.info("Bit Set Size: {}", bitSet.size()); LOG.info("Bit Set Byte[]: {}", bitSet.toByteArray()); LOG.info("Bit Set Byte[]: {}", bitSet.toLongArray()); LOG.info("BitSet[0]: {}", bitSet.get(0)); LOG.info("BitSet[1]: {}", bitSet.get(1)); LOG.info("BitSet[1]: {}", bitSet.get(2)); byte[] bytes = bitSet.toByteArray(); BitSet bitSet2 = BitSet.valueOf(bytes); LOG.info("Bit Set Size: {}", bitSet2.size()); LOG.info("Bit Set Byte[]: {}", bitSet2.toByteArray()); LOG.info("Bit Set Byte[]: {}", bitSet2.toLongArray()); LOG.info("BitSet[0]: {}", bitSet2.get(0)); LOG.info("BitSet[1]: {}", bitSet2.get(1)); LOG.info("BitSet[1]: {}", bitSet2.get(2)); BitSet bitSet3 = new BitSet(); bitSet3.set(0, true);//from w ww .j a v a 2s.c om Assert.assertEquals(1, bitSet3.length()); BitSet bitSet4 = new BitSet(); bitSet4.set(0, false); Assert.assertEquals(0, bitSet4.length()); Assert.assertFalse(bitSet4.get(1)); Assert.assertFalse(bitSet4.get(2)); }
From source file:org.apache.hadoop.hive.serde2.compression.SnappyCompDe.java
/** * Decompress a set of columns from a ByteBuffer and update the position of the buffer. * * @param input A ByteBuffer with `position` indicating the starting point of the compressed chunk. * @param chunkSize The length of the compressed chunk to be decompressed from the input buffer. * * @return The set of columns.//from w w w . j a va 2 s . co m */ @Override public ColumnBuffer[] decompress(ByteBuffer input, int chunkSize) { int startPos = input.position(); try { // Read the footer. int footerSize = input.getInt(startPos + chunkSize - 4); Iterator<Integer> compressedSize = Arrays .asList(ArrayUtils.toObject(Snappy.uncompressIntArray(input.array(), input.arrayOffset() + startPos + chunkSize - Integer.SIZE / Byte.SIZE - footerSize, footerSize))) .iterator(); // Read the header. int[] dataType = readIntegers(compressedSize.next(), input); int numOfCols = dataType.length; // Read the columns. ColumnBuffer[] outputCols = new ColumnBuffer[numOfCols]; for (int colNum = 0; colNum < numOfCols; colNum++) { byte[] nulls = readBytes(compressedSize.next(), input); switch (TTypeId.findByValue(dataType[colNum])) { case BOOLEAN_TYPE: { int numRows = input.getInt(); byte[] vals = readBytes(compressedSize.next(), input); BitSet bsBools = BitSet.valueOf(vals); boolean[] bools = new boolean[numRows]; for (int rowNum = 0; rowNum < numRows; rowNum++) { bools[rowNum] = bsBools.get(rowNum); } TBoolColumn column = new TBoolColumn(Arrays.asList(ArrayUtils.toObject(bools)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.boolVal(column)); break; } case TINYINT_TYPE: { byte[] vals = readBytes(compressedSize.next(), input); TByteColumn column = new TByteColumn(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.byteVal(column)); break; } case SMALLINT_TYPE: { short[] vals = readShorts(compressedSize.next(), input); TI16Column column = new TI16Column(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.i16Val(column)); break; } case INT_TYPE: { int[] vals = readIntegers(compressedSize.next(), input); TI32Column column = new TI32Column(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.i32Val(column)); break; } case BIGINT_TYPE: { long[] vals = readLongs(compressedSize.next(), input); TI64Column column = new TI64Column(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.i64Val(column)); break; } case DOUBLE_TYPE: { double[] vals = readDoubles(compressedSize.next(), input); TDoubleColumn column = new TDoubleColumn(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.doubleVal(column)); break; } case BINARY_TYPE: { int[] rowSize = readIntegers(compressedSize.next(), input); ByteBuffer flattenedData = ByteBuffer.wrap(readBytes(compressedSize.next(), input)); ByteBuffer[] vals = new ByteBuffer[rowSize.length]; for (int rowNum = 0; rowNum < rowSize.length; rowNum++) { vals[rowNum] = ByteBuffer.wrap(flattenedData.array(), flattenedData.position(), rowSize[rowNum]); flattenedData.position(flattenedData.position() + rowSize[rowNum]); } TBinaryColumn column = new TBinaryColumn(Arrays.asList(vals), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.binaryVal(column)); break; } case STRING_TYPE: { int[] rowSize = readIntegers(compressedSize.next(), input); ByteBuffer flattenedData = ByteBuffer.wrap(readBytes(compressedSize.next(), input)); String[] vals = new String[rowSize.length]; for (int rowNum = 0; rowNum < rowSize.length; rowNum++) { vals[rowNum] = new String(flattenedData.array(), flattenedData.position(), rowSize[rowNum], StandardCharsets.UTF_8); flattenedData.position(flattenedData.position() + rowSize[rowNum]); } TStringColumn column = new TStringColumn(Arrays.asList(vals), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.stringVal(column)); break; } default: throw new IllegalStateException( "Unrecognized column type: " + TTypeId.findByValue(dataType[colNum])); } } input.position(startPos + chunkSize); return outputCols; } catch (IOException e) { e.printStackTrace(); return (ColumnBuffer[]) null; } }
From source file:org.apache.metron.stellar.common.utils.hashing.tlsh.TLSHHasher.java
public Map<String, String> bin(String hash) throws DecoderException { Random r = new Random(0); byte[] h = Hex.decodeHex(hash.substring(2 * checksumOption.getChecksumLength()).toCharArray()); BitSet vector = BitSet.valueOf(h); int n = vector.length(); Map<String, String> ret = new HashMap<>(); boolean singleHash = hashes.size() == 1; for (int numHashes : hashes) { BitSet projection = new BitSet(); for (int i = 0; i < numHashes; ++i) { int index = r.nextInt(n); projection.set(i, vector.get(index)); }/*from w w w . j a v a2 s.c om*/ String outputHash = numHashes + Hex.encodeHexString(projection.toByteArray()); if (singleHash) { ret.put(TLSH_BIN_KEY, outputHash); } else { ret.put(TLSH_BIN_KEY + "_" + numHashes, outputHash); } } return ret; }
From source file:org.asoem.greyfish.utils.collect.BitString.java
/** * Create a random bit string of given {@code length}. * * @param length the length of the bit string * @param rng the random number generator to use * @return a new bit string of given length. */// w w w. j a v a2s. c o m public static BitString random(final int length, final RandomGenerator rng) { checkNotNull(rng); checkArgument(length >= 0); if (length == 0) { return emptyBitSequence(); } long[] longs = new long[(length + 63) / 64]; for (int i = 0; i < longs.length; i++) { longs[i] = rng.nextLong(); } longs[longs.length - 1] &= (~0L >>> (longs.length * 64 - length)); return new BitSetString(BitSet.valueOf(longs), length); }
From source file:org.asoem.greyfish.utils.collect.BitString.java
public static BitString create(final int length, final long... longs) { return create(BitSet.valueOf(longs), length); }
From source file:org.sputnikdev.bluetooth.gattparser.FieldHolder.java
/** * Returns field enumeration according to the field value. * @return fields enumeration according to the field value *//*from w w w .jav a2 s . c o m*/ public Enumeration getEnumeration() { BigInteger key; if (field.getFormat().isStruct() && value instanceof byte[]) { byte[] data = (byte[]) value; key = new TwosComplementNumberFormatter().deserializeBigInteger(BitSet.valueOf(data), data.length * 8, false); } else if (field.getFormat().isString() && value instanceof String) { String encoding = field.getFormat().getType() == FieldType.UTF8S ? "UTF-8" : "UTF-16"; try { byte[] data = ((String) value).getBytes(encoding); key = new TwosComplementNumberFormatter().deserializeBigInteger(BitSet.valueOf(data), data.length * 8, false); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } } else { key = getBigInteger(); } return FlagUtils.getEnumeration(field, key).orElse(null); }
From source file:org.sputnikdev.bluetooth.gattparser.FieldHolder.java
private Object prepareValue() { if (field.getFormat().isStruct() && value instanceof byte[]) { byte[] data = (byte[]) value; return new TwosComplementNumberFormatter().deserializeBigInteger(BitSet.valueOf(data), data.length * 8, false);/*www . j a v a 2 s. co m*/ } else { return value; } }