Java Code Examples for java.util.BitSet#toByteArray()

The following examples show how to use java.util.BitSet#toByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LongBitSetSimple.java    From jelectrum with MIT License 6 votes vote down vote up
/**
 * Ensure that all setBit operations are on disk
 */
public synchronized void flush()
{
  long t1 = System.nanoTime();

  byte[] buff = new byte[1];
  for(long index : bits_to_set)
  {
    long location = index/8;
    int bit_in_byte = (int) (index % 8);

    long_file.getBytes(location, buff);
    BitSet bs = BitSet.valueOf(buff);

    bs.set(bit_in_byte);
    byte[] save = bs.toByteArray();
    long_file.putBytes(location, save);
  }
  bits_to_set.clear();
  
  TimeRecord.record(t1, "LongBitSetSimple_flush");

}
 
Example 2
Source File: DateIndexHelperTest.java    From datawave with Apache License 2.0 6 votes vote down vote up
public static KeyValue getDateIndexEntry(String shardDate, int[] shardIndicies, String dataType, String type, String dateField, String dateValue,
                ColumnVisibility visibility) throws ParseException {
    // The row is the date to index yyyyMMdd
    
    // the colf is the type (e.g. LOAD or ACTIVITY)
    
    // the colq is the event date yyyyMMdd \0 the datatype \0 the field name
    String colq = shardDate + '\0' + dataType + '\0' + dateField;
    
    // the value is a bitset denoting the shard
    BitSet bits = DateIndexUtil.getBits(shardIndicies[0]);
    for (int i = 1; i < shardIndicies.length; i++) {
        bits = DateIndexUtil.merge(bits, DateIndexUtil.getBits(shardIndicies[i]));
    }
    Value shardList = new Value(bits.toByteArray());
    
    // create the key
    Key key = new Key(dateValue, type, colq, visibility, DateIndexUtil.getBeginDate(dateValue).getTime());
    
    return new KeyValue(key, shardList);
}
 
Example 3
Source File: DeviceStatus.java    From android-ponewheel with MIT License 6 votes vote down vote up
@VisibleForTesting // testing and/or mock
public static byte[] toByteArray(
        boolean riderDetected,
        boolean riderDetectPad1,
        boolean riderDetectPad2,
        boolean icsuFault,
        boolean icsvFault,
        boolean charging,
        boolean bmsCtrlComms,
        boolean brokenCapacitor ) {

    BitSet bitSet = new BitSet(8);
    bitSet.set(0,riderDetected);
    bitSet.set(1,riderDetectPad1);
    bitSet.set(2,riderDetectPad2);
    bitSet.set(3,icsuFault);
    bitSet.set(4,icsvFault);
    bitSet.set(5,charging);
    bitSet.set(6,bmsCtrlComms);
    bitSet.set(7,brokenCapacitor);

    return bitSet.toByteArray();
}
 
Example 4
Source File: GenericCharacteristicParser.java    From bluetooth-gatt-parser with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(Collection<FieldHolder> fieldHolders) throws CharacteristicFormatException {
    BitSet bitSet = new BitSet();
    int offset = 0;

    for (FieldHolder holder : fieldHolders) {
        if (holder.isValueSet()) {
            int size = holder.getField().getFormat().getSize();
            BitSet serialized = serialize(holder);
            if (size == FieldFormat.FULL_SIZE) {
                size = serialized.length();
            }
            concat(bitSet, serialized, offset, size);
            offset += size;
        }
    }
    // BitSet does not keep 0, fields could be set all to 0, resulting bitSet to be of 0 length,
    // however data array must not be empty, hence forcing to return an array with first byte of 0 value
    byte[] data = bitSet.isEmpty() ? new byte[] {0} : bitSet.toByteArray();
    return data.length > 20 ? Arrays.copyOf(bitSet.toByteArray(), 20) : data;
}
 
Example 5
Source File: PrimitiveBooleanArraySerializationProvider.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] writeField(final boolean[] fieldValue) {
  if (fieldValue == null) {
    return new byte[] {};
  }
  final BitSet bits = new BitSet(fieldValue.length);
  for (int i = 0; i < fieldValue.length; i++) {
    bits.set(i, fieldValue[i]);
  }
  byte[] bytes = bits.toByteArray();
  int size = VarintUtils.unsignedIntByteLength(fieldValue.length);
  size += bytes.length;
  final ByteBuffer buf = ByteBuffer.allocate(size);
  VarintUtils.writeUnsignedInt(fieldValue.length, buf);
  buf.put(bytes);
  return buf.array();
}
 
Example 6
Source File: V13Statement.java    From jaybird with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected void writeSqlData(final RowDescriptor rowDescriptor, final RowValue fieldValues) throws IOException, SQLException {
    final XdrOutputStream xdrOut = getXdrOut();
    final BlrCalculator blrCalculator = getDatabase().getBlrCalculator();
    // null indicator bitmap
    final BitSet nullBits = new BitSet(fieldValues.getCount());
    for (int idx = 0; idx < fieldValues.getCount(); idx++) {
        nullBits.set(idx, fieldValues.getFieldData(idx) == null);
    }
    final byte[] nullBitsBytes = nullBits.toByteArray(); // Note only amount of bytes necessary for highest bit set
    xdrOut.write(nullBitsBytes);
    final int requiredBytes = (rowDescriptor.getCount() + 7) / 8;
    final int remainingBytes = requiredBytes - nullBitsBytes.length;
    if (remainingBytes > 0) {
        xdrOut.write(new byte[remainingBytes]);
    }
    xdrOut.writeAlignment(requiredBytes);

    for (int idx = 0; idx < fieldValues.getCount(); idx++) {
        if (!nullBits.get(idx)) {
            final byte[] buffer = fieldValues.getFieldData(idx);
            final FieldDescriptor fieldDescriptor = rowDescriptor.getFieldDescriptor(idx);
            final int len = blrCalculator.calculateIoLength(fieldDescriptor, buffer);
            final int fieldType = fieldDescriptor.getType();
            writeColumnData(xdrOut, len, buffer, fieldType);
        }
    }
}
 
Example 7
Source File: LongMappedBuffer.java    From jelectrum with MIT License 5 votes vote down vote up
public LongMappedBuffer(File f, long total_size)
  throws IOException
{
  RandomAccessFile raf = new RandomAccessFile(f, "rw");
  FileChannel chan = raf.getChannel();

  this.total_size = total_size;

  map_list=new ArrayList<>();

  long opened = 0;
  while(opened < total_size)
  {
    long len = Math.min(total_size - opened, MAP_SIZE);
    MappedByteBuffer buf = chan.map(FileChannel.MapMode.READ_WRITE, opened, len);

    opened += len;

    map_list.add(buf);
  }

  byte_mappings = new byte[8];
  for(int i=0; i<8; i++)
  {
    BitSet bs = new BitSet(8);
    bs.set(i);
    byte[] b = bs.toByteArray();
    byte_mappings[i]=b[0];
  }
}
 
Example 8
Source File: LongRandomFile.java    From jelectrum with MIT License 5 votes vote down vote up
public synchronized void setBit(long bit)
{
  try
  {
    long t1=System.nanoTime();
    long data_pos = bit / 8;
    int bit_in_byte = (int)(bit % 8);

    byte[] b = new byte[1];
    random_file.seek(data_pos);
    random_file.readFully(b);
    BitSet bs = BitSet.valueOf(b);
    if (!bs.get(bit_in_byte))
    {
      bs.set(bit_in_byte);
      b = bs.toByteArray();

      random_file.seek(data_pos);
      random_file.write(b);
    }


    TimeRecord.record(t1, "long_file_set_bit");

  }
  catch(IOException e) { throw new RuntimeException(e);}
}
 
Example 9
Source File: LongMappedBufferMany.java    From jelectrum with MIT License 5 votes vote down vote up
public LongMappedBufferMany(File f, long total_size)
  throws IOException
{
  f.mkdirs();

  this.total_size = total_size;

  map_list=new ArrayList<>();

  long opened = 0;
  while(opened < total_size)
  {
    File child = new File(f, "" + opened);
    RandomAccessFile raf = new RandomAccessFile(child, "rw");
    FileChannel chan = raf.getChannel();
    long len = Math.min(total_size - opened, MAP_SIZE);
    MappedByteBuffer buf = chan.map(FileChannel.MapMode.READ_WRITE, 0, len);

    opened += len;

    map_list.add(buf);
  }

  byte_mappings = new byte[8];
  for(int i=0; i<8; i++)
  {
    BitSet bs = new BitSet(8);
    bs.set(i);
    byte[] b = bs.toByteArray();
    byte_mappings[i]=b[0];
  }
}
 
Example 10
Source File: BitSetSerializer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Kryo kryo, Output output, BitSet bitSet) {
    final int len = bitSet.length();

    output.writeInt(len, true);

    byte[] bytes = bitSet.toByteArray();
    output.writeInt(bytes.length, true);
    output.writeBytes(bitSet.toByteArray());
}
 
Example 11
Source File: ZOrderUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
public static byte[] encode(
    final double[] normalizedValues,
    final int bitsPerDimension,
    final int numDimensions) {
  final BitSet[] bitSets = new BitSet[numDimensions];

  for (int d = 0; d < numDimensions; d++) {
    bitSets[d] = getBits(normalizedValues[d], 0, 1, bitsPerDimension);
  }
  final int usedBits = bitsPerDimension * numDimensions;
  final int usedBytes = (int) Math.ceil(usedBits / 8.0);
  final int bitsetLength = (usedBytes * 8);
  final int bitOffset = bitsetLength - usedBits;
  // round up to a bitset divisible by 8
  final BitSet combinedBitSet = new BitSet(bitsetLength);
  int j = bitOffset;
  for (int i = 0; i < bitsPerDimension; i++) {
    for (int d = 0; d < numDimensions; d++) {
      combinedBitSet.set(j++, bitSets[d].get(i));
    }
  }
  final byte[] littleEndianBytes = combinedBitSet.toByteArray();
  final byte[] retVal = swapEndianFormat(littleEndianBytes);
  if (retVal.length < usedBytes) {
    return Arrays.copyOf(retVal, usedBytes);
  }
  return retVal;
}
 
Example 12
Source File: KademliaId.java    From Kademlia with MIT License 5 votes vote down vote up
/**
 * Generates a NodeId that is some distance away from this NodeId
 *
 * @param distance in number of bits
 *
 * @return NodeId The newly generated NodeId
 */
public KademliaId generateNodeIdByDistance(int distance)
{
    byte[] result = new byte[ID_LENGTH / 8];

    /* Since distance = ID_LENGTH - prefixLength, we need to fill that amount with 0's */
    int numByteZeroes = (ID_LENGTH - distance) / 8;
    int numBitZeroes = 8 - (distance % 8);

    /* Filling byte zeroes */
    for (int i = 0; i < numByteZeroes; i++)
    {
        result[i] = 0;
    }

    /* Filling bit zeroes */
    BitSet bits = new BitSet(8);
    bits.set(0, 8);

    for (int i = 0; i < numBitZeroes; i++)
    {
        /* Shift 1 zero into the start of the value */
        bits.clear(i);
    }
    bits.flip(0, 8);        // Flip the bits since they're in reverse order
    result[numByteZeroes] = (byte) bits.toByteArray()[0];

    /* Set the remaining bytes to Maximum value */
    for (int i = numByteZeroes + 1; i < result.length; i++)
    {
        result[i] = Byte.MAX_VALUE;
    }

    return this.xor(new KademliaId(result));
}
 
Example 13
Source File: BufrDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Write a int value
 *
 * @param value Value
 * @param nbits bit number
 * @return Data length
 * @throws IOException
 */
public int write(int value, int nbits) throws IOException {
    BitSet bits = new BitSet(nbits);
    int index = 0;
    while (value != 0L) {
        if (value % 2L != 0) {
            bits.set(index);
        }
        ++index;
        value = value >>> 1;
    }
    byte[] bytes = bits.toByteArray();
    int n = nbits / 8;
    if (bytes.length < n) {
        byte[] nbytes = new byte[n];
        for (int i = 0; i < n; i++) {
            if (i < n - bytes.length) {
                nbytes[i] = (byte) 0;
            } else {
                nbytes[i] = bytes[i - (n - bytes.length)];
            }
        }
        bw.write(nbytes);
        return nbytes.length;
    } else {
        bw.write(bytes);
        return bytes.length;
    }
}
 
Example 14
Source File: ThinProtocolFeature.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param features Feature set.
 * @return Byte array representing all supported features.
 */
public static <E extends Enum<E> & ThinProtocolFeature> byte[] featuresAsBytes(Collection<E> features) {
    final BitSet set = new BitSet();

    for (ThinProtocolFeature f : features)
        set.set(f.featureId());

    return set.toByteArray();
}
 
Example 15
Source File: PartitionedEventSerializerTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@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);
    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));
}
 
Example 16
Source File: StreamEventSerializer.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(StreamEvent event, DataOutput dataOutput) throws IOException {
    // Bryant: here "metaVersion/streamId" writes to dataOutputUTF
    String metaVersion = event.getMetaVersion();
    String streamId = event.getStreamId();
    String metaVersionStreamId = String.format("%s/%s", metaVersion, streamId);

    dataOutput.writeUTF(metaVersionStreamId);
    dataOutput.writeLong(event.getTimestamp());
    if (event.getData() == null || event.getData().length == 0) {
        dataOutput.writeInt(0);
    } else {
        BitSet isNullIndex = isNullBitSet(event.getData());
        byte[] isNullBytes = isNullIndex.toByteArray();
        dataOutput.writeInt(isNullBytes.length);
        dataOutput.write(isNullBytes);
        int i = 0;
        StreamDefinition definition = serializationMetadataProvider.getStreamDefinition(event.getStreamId());
        if (definition == null) {
            throw new IOException("StreamDefinition not found: " + event.getStreamId());
        }
        if (event.getData().length != definition.getColumns().size()) {
            throw new IOException("Event :" + event + " doesn't match with schema: " + definition);
        }
        for (StreamColumn column : definition.getColumns()) {
            if (!isNullIndex.get(i)) {
                Serializers.getColumnSerializer(column.getType()).serialize(event.getData()[i], dataOutput);
            }
            i++;
        }
    }
}
 
Example 17
Source File: DatabaseValue.java    From claudb with MIT License 5 votes vote down vote up
public static DatabaseValue bitset(int... ones) {
  BitSet bitSet = new BitSet();
  for (int position : ones) {
    bitSet.set(position);
  }
  return new DatabaseValue(DataType.STRING, new SafeString(bitSet.toByteArray()));
}
 
Example 18
Source File: TimeDescriptors.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] toBinary() {
  final BitSet bits = new BitSet(3);
  int length = 1;
  byte[] timeBytes, startRangeBytes, endRangeBytes;
  if (timeName != null) {
    bits.set(0);
    timeBytes = StringUtils.stringToBinary(timeName);
    length += VarintUtils.unsignedIntByteLength(timeBytes.length);
    length += timeBytes.length;
  } else {
    timeBytes = null;
  }
  if (startRangeName != null) {
    bits.set(1);
    startRangeBytes = StringUtils.stringToBinary(startRangeName);
    length += VarintUtils.unsignedIntByteLength(startRangeBytes.length);
    length += startRangeBytes.length;
  } else {
    startRangeBytes = null;
  }
  if (endRangeName != null) {
    bits.set(2);
    endRangeBytes = StringUtils.stringToBinary(endRangeName);
    length += VarintUtils.unsignedIntByteLength(endRangeBytes.length);
    length += endRangeBytes.length;
  } else {
    endRangeBytes = null;
  }
  final ByteBuffer buf = ByteBuffer.allocate(length);
  final byte[] bitMask = bits.toByteArray();
  buf.put(bitMask.length > 0 ? bitMask[0] : (byte) 0);
  if (timeBytes != null) {
    VarintUtils.writeUnsignedInt(timeBytes.length, buf);
    buf.put(timeBytes);
  }
  if (startRangeBytes != null) {
    VarintUtils.writeUnsignedInt(startRangeBytes.length, buf);
    buf.put(startRangeBytes);
  }
  if (endRangeBytes != null) {
    VarintUtils.writeUnsignedInt(endRangeBytes.length, buf);
    buf.put(endRangeBytes);
  }
  return buf.array();
}
 
Example 19
Source File: PVABitSet.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
public static void encodeBitSet(final BitSet bits, final ByteBuffer buffer)
{
    final byte[] bytes = bits.toByteArray();
    PVASize.encodeSize(bytes.length, buffer);
    buffer.put(bits.toByteArray());
}
 
Example 20
Source File: BitChromosome.java    From jenetics with Apache License 2.0 2 votes vote down vote up
/**
 * Constructing a new BitChromosome from a given BitSet.
 * The BitSet is copied while construction. The length of the constructed
 * BitChromosome will be {@code bitSet.length()} ({@link BitSet#length}).
 *
 * @param bits the bit-set which initializes the chromosome
 * @return a new {@code BitChromosome} with the given parameter
 * @throws NullPointerException if the {@code bitSet} is
 *        {@code null}.
 */
public static BitChromosome of(final BitSet bits) {
	return new BitChromosome(bits.toByteArray(), -1);
}