Example usage for java.util BitSet valueOf

List of usage examples for java.util BitSet valueOf

Introduction

In this page you can find the example usage for java.util BitSet valueOf.

Prototype

public static BitSet valueOf(ByteBuffer bb) 

Source Link

Document

Returns a new bit set containing all the bits in the given byte buffer between its position and limit.

Usage

From source file:ezbake.deployer.impl.Files.java

public static Set<PosixFilePermission> convertTarArchiveEntryModeToPosixFilePermissions(int mode) {
    Set<PosixFilePermission> permissions = Sets.newHashSet();
    BitSet bs = BitSet.valueOf(new long[] { mode });

    if (bs.get(PosixFilePermission.OTHERS_EXECUTE.ordinal())) {
        permissions.add(PosixFilePermission.OTHERS_EXECUTE);
    }/*from   www  .  j  a  v  a 2  s . c  o  m*/
    if (bs.get(PosixFilePermission.OTHERS_WRITE.ordinal())) {
        permissions.add(PosixFilePermission.OTHERS_WRITE);
    }
    if (bs.get(PosixFilePermission.OTHERS_READ.ordinal())) {
        permissions.add(PosixFilePermission.OTHERS_READ);
    }
    if (bs.get(PosixFilePermission.GROUP_EXECUTE.ordinal())) {
        permissions.add(PosixFilePermission.GROUP_EXECUTE);
    }
    if (bs.get(PosixFilePermission.GROUP_WRITE.ordinal())) {
        permissions.add(PosixFilePermission.GROUP_WRITE);
    }
    if (bs.get(PosixFilePermission.GROUP_READ.ordinal())) {
        permissions.add(PosixFilePermission.GROUP_READ);
    }
    if (bs.get(PosixFilePermission.OWNER_EXECUTE.ordinal())) {
        permissions.add(PosixFilePermission.OWNER_EXECUTE);
    }
    if (bs.get(PosixFilePermission.OWNER_WRITE.ordinal())) {
        permissions.add(PosixFilePermission.OWNER_WRITE);
    }
    if (bs.get(PosixFilePermission.OWNER_READ.ordinal())) {
        permissions.add(PosixFilePermission.OWNER_READ);
    }

    return permissions;
}

From source file:hivemall.ftvec.ranking.PopulateNotInUDTF.java

@Override
public void process(Object[] args) throws HiveException {
    Object arg0 = args[0];/*from www .j a v a 2  s. c o  m*/
    if (arg0 == null || listOI.getListLength(arg0) == 0) {
        populateAll();
        return;
    }

    final BitSet bits;
    if (bitsetInput) {
        long[] longs = HiveUtils.asLongArray(arg0, listOI, listElemOI);
        bits = BitSet.valueOf(longs);
    } else {
        if (_bitset == null) {
            bits = new BitSet();
            this._bitset = bits;
        } else {
            bits = _bitset;
            bits.clear();
        }
        HiveUtils.setBits(arg0, listOI, listElemOI, bits);
    }

    populateItems(bits);
}

From source file:info.rmarcus.birkhoffvonneumann.MatrixUtils.java

private static Iterator<BitSet> bitStringsOfSize(int n) {
    return new Iterator<BitSet>() {
        private BigInteger b = new BigInteger("0");

        @Override/*from w  w  w. ja v  a 2 s  .c  om*/
        public boolean hasNext() {
            return b.compareTo((new BigInteger("2")).pow(n)) < 0;
        }

        @Override
        public @NonNull BitSet next() {
            byte[] bytes = b.toByteArray();
            BitSet toR = BitSet.valueOf(bytes);
            BigInteger nxt = b.add(new BigInteger("1"));
            if (nxt == null || toR == null) {
                throw new NoSuchElementException("Unable to increment big int");
            }
            b = nxt;
            return toR;
        }

    };
}

From source file:hivemall.ftvec.ranking.ItemPairsSamplingUDTF.java

@Override
public void process(Object[] args) throws HiveException {
    final int numPosItems;
    final BitSet bits;
    if (bitsetInput) {
        if (_rand == null) {
            this._rand = new Random(43);
        }//w  w w. j  av a  2  s  .  c  om
        long[] longs = HiveUtils.asLongArray(args[0], listOI, listElemOI);
        bits = BitSet.valueOf(longs);
        numPosItems = bits.cardinality();
    } else {
        if (_bitset == null) {
            bits = new BitSet();
            this._bitset = bits;
            this._rand = new Random(43);
        } else {
            bits = _bitset;
            bits.clear();
        }
        numPosItems = HiveUtils.setBits(args[0], listOI, listElemOI, bits);
    }

    if (numPosItems == 0) {
        return;
    }
    final int numNegItems = maxItemId + 1 - numPosItems;
    if (numNegItems == 0) {
        return;
    } else if (numNegItems < 0) {
        throw new UDFArgumentException(
                "maxItemId + 1 - numPosItems = " + maxItemId + " + 1 - " + numPosItems + " = " + numNegItems);
    }

    if (withReplacement) {
        sampleWithReplacement(numPosItems, numNegItems, bits);
    } else {
        sampleWithoutReplacement(numPosItems, numNegItems, bits);
    }
}

From source file:at.tuwien.mnsa.smssender.SMSPDUConverter.java

/**
 * Get the PDU encoding for the given message
 * @param message//from  w  w  w .  ja  v  a  2  s.c om
 * @param rightshift of the content for concatination
 * @return 
 */
public SMSPDUConversionResult getContent(String message, int rightshift) {
    List<Byte> finalized = new LinkedList<>();
    BitSet currentWorkingBS = new BitSet(16);

    int currentShiftpos = 0;
    boolean currentlyExtended = false;
    int len = 0;

    //repeat while there are characters left
    while (message.length() > 0) {
        String c = message.substring(0, 1);
        message = message.substring(1);
        byte value;

        //loook up current character
        if (this.GSM_3GPP_TS_23_038.containsKey(c)) {
            value = this.GSM_3GPP_TS_23_038.get(c);
        } else {
            if (this.GSM_3GPP_TS_23_038_EXTENSION.containsKey(c)) {
                if (!currentlyExtended) {
                    //extension -> now do the escape character!
                    //do the "new" character the other round
                    message = c + message;
                    currentlyExtended = true;
                    value = this.GSM_3GPP_TS_23_038.get("\u001B");
                } else {
                    //we just did the ecsape character, now do the char from
                    //the extended alphabet
                    value = this.GSM_3GPP_TS_23_038_EXTENSION.get(c);
                    currentlyExtended = false;
                }
            } else {
                throw new RuntimeException("Not found: " + c);

            }

        }

        //start at 0x0
        if (currentShiftpos == 0) {
            //add current char beginning at pos 0
            addByteToBitset(value, currentWorkingBS, 0, 1, 7);
        } else {
            //else start at second byte and do flipping

            //make place for the right bits of the current char in the front
            currentWorkingBS = rightShiftBitset(currentWorkingBS, currentShiftpos);

            //add last X bits in front of the bitset
            addByteToBitset(value, currentWorkingBS, 0, 8 - currentShiftpos, currentShiftpos);

            //add the first X bits at the end of the bitset if any
            if (currentShiftpos < 7) {
                addByteToBitset(value, currentWorkingBS, 8, 1, 7 - currentShiftpos);
            }

            //the first byte of the bitset is now complete! :)
            byte finalByte = currentWorkingBS.toByteArray()[0];
            finalByte = swapEndianFormat(finalByte);
            finalized.add(finalByte);

            //shift bitset left by 8 bits since we just finished and exported a byte
            currentWorkingBS = leftShiftBitset(currentWorkingBS, 8);
        }

        currentShiftpos = (currentShiftpos + 1) % 8;
        len++;
        //for first character -> just add to the bitset
        //addByteToBitset(value, bitset, i*7);

        /*//exchange characters
        for (int j=0;j<((i%8)*8-(i%8)*7);j++) {
        boolean cBit = content.get()
        }*/

    }

    //add last byte (swap back our eagerly shifted byte)
    if (currentShiftpos == 7) {
        byte finalByte = 0x00;
        finalized.add(finalByte);
    } else if (currentShiftpos != 0) {
        byte finalByte = (currentWorkingBS.isEmpty()) ? 0x0 : currentWorkingBS.toByteArray()[0];
        finalByte = swapEndianFormat(finalByte);

        //I don't really know why,  
        //but java fills right shifts with 1s
        //so we have to manually set all new (left) bits to zero
        for (int i = 0; i < currentShiftpos; i++) {
            finalByte = (byte) (finalByte >> 1);
            finalByte = (byte) (finalByte & 0x7F); //unset first bit
        }

        //finalByte = (byte) (finalByte & 0x3F);
        finalized.add(finalByte);
    }

    byte[] finalM = ArrayUtils.toPrimitive(finalized.toArray(new Byte[finalized.size()]));
    Logger.getGlobal().info("1: " + DatatypeConverter.printHexBinary(finalM));

    //in case of rightshift for concatenation -> right shift the whole array
    if (rightshift > 0) {
        BitSet bs = BitSet.valueOf(finalM);
        bs = rightShiftBitset(bs, rightshift);
        finalM = bs.toByteArray();
        Logger.getGlobal().info("2: " + DatatypeConverter.printHexBinary(finalM));
    }

    SMSPDUConversionResult res = new SMSPDUConversionResult(finalM, len);

    return res;
}

From source file:jlg.jade.test.asterix.cat062.Cat062LargeSampleTest.java

@Test
@Description("Used only for printing byte information that can help with developing the tool")
@Ignore/*from  w  w w . j a  v  a2s .c o m*/
public void with_one_packet_should_print_bytes() throws IOException {
    try (InputStream is = TestHelper.getFileInputStreamFromResource("final_frame_062_one_packet_sample2.FF")) {
        FinalFrameReader ffReader = new FinalFrameReader();
        while (is.available() > 0) {
            byte[] ffPayload = ffReader.read(is);
            if (ffPayload != null) {
                System.out.println("DATA BLOCK START");
                for (int i = 0; i < ffPayload.length; i++) {
                    BitSet bs = BitSet.valueOf(new byte[] { ffPayload[i] });
                    System.out.print("ORIGINAL [ ");
                    for (int j = 0; j < 8; j++) {
                        if (bs.get(j)) {
                            System.out.print(1 + " ");
                        } else {
                            System.out.print(0 + " ");
                        }
                    }
                    System.out.print("]");

                    System.out.print("   REVERSE [ ");
                    for (int j = 8; j > 0; j--) {
                        if (bs.get(j)) {
                            System.out.print(1 + " ");
                        } else {
                            System.out.print(0 + " ");
                        }
                    }
                    System.out.print("]   ");
                    int unsignedValue = Byte.toUnsignedInt(ffPayload[i]);

                    System.out.println(unsignedValue);

                    if (i == 0 || i == 2) {
                        System.out.println("----------------------------------------------------------------");
                    }
                }
            }
            System.out.println("DATA BLOCK END");
            System.out.println(System.lineSeparator());
        }
    }
}

From source file:hivemall.ftvec.ranking.ItemPairsSamplingUDTF.java

private void sampleWithoutReplacement(int numPosItems, int numNegItems, @Nonnull final BitSet bitset)
        throws HiveException {
    final BitSet bitsetForPosSampling = bitset;
    final BitSet bitsetForNegSampling = BitSet.valueOf(bitset.toLongArray());

    final int numSamples = Math.max(1, Math.round(numPosItems * samplingRate));
    for (int s = 0; s < numSamples; s++) {
        int nth = _rand.nextInt(numPosItems);
        int i = BitUtils.indexOfSetBit(bitsetForPosSampling, nth);
        if (i == -1) {
            throw new UDFArgumentException("Cannot find a value for " + nth + "-th element in bitset "
                    + bitset.toString() + " where numPosItems = " + numPosItems);
        }/*from ww w.  j  av  a2s .c om*/
        bitsetForPosSampling.set(i, false);
        --numPosItems;

        nth = _rand.nextInt(numNegItems);
        int j = BitUtils.indexOfClearBit(bitsetForNegSampling, nth, maxItemId);
        if (j < 0 || j > maxItemId) {
            throw new UDFArgumentException("j MUST be in [0," + maxItemId + "] but j was " + j);
        }
        bitsetForNegSampling.set(j, true);
        --numNegItems;

        posItemId.set(i);
        negItemId.set(j);
        forward(forwardObjs);

        if (numPosItems <= 0) {
            // cannot draw a positive example anymore
            return;
        } else if (numNegItems <= 0) {
            // cannot draw a negative example anymore
            return;
        }
    }
}

From source file:org.osgp.adapter.protocol.dlms.domain.commands.SetAlarmNotificationsCommandExecutor.java

public AlarmNotifications alarmNotifications(final long alarmFilterLongValue) {

    final BitSet bitSet = BitSet.valueOf(new long[] { alarmFilterLongValue });
    final Set<AlarmNotification> notifications = new TreeSet<>();

    final AlarmType[] alarmTypes = AlarmType.values();
    for (final AlarmType alarmType : alarmTypes) {
        final boolean enabled = bitSet.get(alarmRegisterBitIndexPerAlarmType.get(alarmType));
        notifications.add(new AlarmNotification(alarmType, enabled));
    }/*from   ww w  .java  2  s  .com*/

    return new AlarmNotifications(notifications);
}

From source file:de.uniba.wiai.lspi.chord.data.ID.java

public static ID NodeId(final byte[] blob) {
    return new ID(bitsetResize(BitSet.valueOf(blob), kTotalBitLen));
}

From source file:com.google.uzaygezen.core.BitSetBackedBitVector.java

@Override
public void copyFrom(long[] array) {
    int len = (size + 63) >>> 6;
    Preconditions.checkArgument(array.length == len, "Length must be %s.", len);
    if (size == 0) {
        return;/* w  w w .  j ava2  s . c  om*/
    }
    Preconditions.checkArgument(Long.numberOfLeadingZeros(array[len - 1]) >= (len << 6) - size,
            "Some bit positions are too high.");
    BitSet bs = BitSet.valueOf(array);
    bitset.clear();
    bitset.or(bs);
}