Reconstitute the <tt>BitSet</tt> instance from a ByteBuffer (i.e.,
deserialize it).
428979
public void deserialize(final ByteBuffer buf) {
		wordsInUse = buf.getInt();
		words = new long[wordsInUse];
		for (int i = 0; i < words.length; i++) {
			words[i] = buf.getLong();
		}

		// Assume maximum length then find real length
		// because recalculateWordsInUse assumes maintenance
		// or reduction in logical size
		wordsInUse = words.length;
		recalculateWordsInUse();
		sizeIsSticky = (words.length > 0 && words[words.length-1] == 0L); // heuristic
		checkInvariants();
	}
1054995
public BitSet toBitSet() {
		final BitSet set = new BitSet(length());
		for (int i = 0, n = length(); i < n; ++i) {
			set.set(i, getGene(i).getBit());
		}
		return set;
	}
79332
public static <S extends Serializable> S deserialize(byte[] bytes) throws IOException {
    if (bytes == null) {
      return null;
    }
    return deserialize(new ByteArrayInputStream(bytes));
  }
196333
@Override
  public ByteBuffer readBytes(ByteBuffer old) throws IOException {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    while (true) {
      int byteRead = in.read();
      if (byteRead < 0) {
        throw new EOFException();
      }
      if (byteRead == 0) {
        int secondByteRead = in.read();
        if (byteRead < 0) {
          throw new EOFException();
        }
        if (secondByteRead == 0) {
          break;
        } else if (secondByteRead == 1) {
          bytesOut.write(0);
        } else {
          String msg = "Illegal encoding. 0 byte cannot be followed by "
              + "anything other than 0 or 1. It was followed by "
              + Integer.toString(byteRead);
          throw new IOException(msg);
        }
      } else {
        bytesOut.write(byteRead);
      }
    }
    return ByteBuffer.wrap(bytesOut.toByteArray());
  }
753314
def cast_bytes(s, encoding=None):
    """"""
    if not isinstance(s, bytes):
        return encode(s, encoding)
    return s
1374631
public <T> T deserialize(final InputStream stream, final int bufferSize) {
    ByteBufferInput in = new ByteBufferInput(stream, bufferSize);
    Kryo kryo = borrow();
    try {
      @SuppressWarnings("unchecked")
      T obj = (T) kryo.readClassAndObject(in);
      return obj;
    } finally {
      release(kryo);
    }
  }
1140669
def from_bytes(self, raw):
        ''''''
        if len(raw) < 4:
            raise NotEnoughDataError("Not enough bytes ({}) to reconstruct a Null object".format(len(raw)))
        fields = struct.unpack('=I', raw[:4])
        self._af = fields[0]
        return raw[4:]
342253
def from_bytes(cls, bitstream):
        '''
        
        '''
        packet = cls()

        # Convert to ConstBitStream (if not already provided)
        if not isinstance(bitstream, ConstBitStream):
            if isinstance(bitstream, Bits):
                bitstream = ConstBitStream(auto=bitstream)
            else:
                bitstream = ConstBitStream(bytes=bitstream)

        # Read the source and destination ports
        (packet.source_port,
         packet.destination_port) = bitstream.readlist('2*uint:16')

        # Store the length
        length = bitstream.read('uint:16')
        if length < 8:
            raise ValueError('Invalid UDP length')

        # Read the checksum
        packet.checksum = bitstream.read('uint:16')

        # And the rest is payload
        payload_bytes = length - 8
        packet.payload = bitstream.read('bytes:%d' % payload_bytes)

        # LISP-specific handling
        if packet.source_port == 4341 or packet.destination_port == 4341:
            # Payload is a LISP data packet
            from pylisp.packet.lisp.data import DataPacket
            packet.payload = DataPacket.from_bytes(packet.payload)
        elif packet.source_port == 4342 or packet.destination_port == 4342:
            # Payload is a LISP control message
            from pylisp.packet.lisp.control.base import ControlMessage
            packet.payload = ControlMessage.from_bytes(packet.payload)

        # There should be no remaining bits
        if bitstream.pos != bitstream.len:
            raise ValueError('Bits remaining after processing packet')

        # Verify that the properties make sense
        packet.sanitize()

        return packet
771789
public static BitSet decodeBitString(ByteBuffer buf) {
        DerId id = DerId.decode(buf);
        if (!id.matches(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_BIT_STRING_TAG_NUM)
                && !id.matches(DerId.TagClass.UNIVERSAL, DerId.EncodingType.CONSTRUCTED, ASN1_BIT_STRING_TAG_NUM)) {
            throw new IllegalArgumentException("Expected BIT STRING identifier, received " + id);
        }
        int len = DerUtils.decodeLength(buf);
        if (buf.remaining() < len) {
            throw new IllegalArgumentException("Insufficient content for BIT STRING");
        }

        // If primitive encoding, then the initial octet is just used for padding
        if (id.getEncodingType() == DerId.EncodingType.PRIMITIVE) {
            buf.get();
            len--;
        }

        int nbits = len * 8;
        BitSet bits = new BitSet(nbits);
        for (int i = 0; i < len; i++) {
            short next = (short) (0xff & buf.get());
            int bitIndex = (i + 1) * 8 - 1;
            while (next != 0) {
                if ((next & 1) == 1) {
                    bits.set(bitIndex);
                }
                bitIndex--;
                next >>>= 1;
            }
        }
        return bits;
    }
1075681
public static byte[] toArray(ByteBuffer bytebuffer) {
		if (false == bytebuffer.hasArray()) {
			int oldPosition = bytebuffer.position();
			bytebuffer.position(0);
			int size = bytebuffer.limit();
			byte[] buffers = new byte[size];
			bytebuffer.get(buffers);
			bytebuffer.position(oldPosition);
			return buffers;
		} else {
			return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
		}
	}
1010658
static TagMapImpl deserializeBinary(byte[] bytes) throws TagContextDeserializationException {
    try {
      if (bytes.length == 0) {
        // Does not allow empty byte array.
        throw new TagContextDeserializationException("Input byte[] can not be empty.");
      }

      ByteBuffer buffer = ByteBuffer.wrap(bytes).asReadOnlyBuffer();
      int versionId = buffer.get();
      if (versionId > VERSION_ID || versionId < 0) {
        throw new TagContextDeserializationException(
            "Wrong Version ID: " + versionId + ". Currently supports version up to: " + VERSION_ID);
      }
      return new TagMapImpl(parseTags(buffer));
    } catch (BufferUnderflowException exn) {
      throw new TagContextDeserializationException(exn.toString()); // byte array format error.
    }
  }
936254
def from_bytes(cls, bitstream, decode_payload=True):
        '''
        
        '''
        packet = cls()

        # Convert to ConstBitStream (if not already provided)
        if not isinstance(bitstream, ConstBitStream):
            if isinstance(bitstream, Bits):
                bitstream = ConstBitStream(auto=bitstream)
            else:
                bitstream = ConstBitStream(bytes=bitstream)

        # Read the version
        version = bitstream.read('uint:4')
        if version != packet.version:
            raise ValueError('Provided bytes do not contain an IPv6 packet')

        # Read the traffic class
        packet.traffic_class = bitstream.read('uint:8')

        # Read the flow label
        packet.flow_label = bitstream.read('uint:20')

        # Read the payload length
        payload_length = bitstream.read('uint:16')

        # Read the next header type
        packet.next_header = bitstream.read('uint:8')

        # Read the hop limit
        packet.hop_limit = bitstream.read('uint:8')

        # Read the source and destination addresses
        packet.source = IPv6Address(bitstream.read('uint:128'))
        packet.destination = IPv6Address(bitstream.read('uint:128'))

        # And the rest is payload
        packet.payload = bitstream.read('bytes:%d' % payload_length)

        if decode_payload:
            payload_class = protocol_registry.get_type_class(packet.next_header)
            if payload_class:
                packet.payload = payload_class.from_bytes(packet.payload)

        # There should be no remaining bits
        if bitstream.pos != bitstream.len:
            raise ValueError('Bits remaining after processing packet')

        # Verify that the properties make sense
        packet.sanitize()

        return packet
1330096
public void flattenToBufferForOriginalVersion(ByteBuffer buf) throws IOException
    {
        assert((params != null) || (serializedParams != null));

        // for self-check assertion
        int startPosition = buf.position();

        buf.put(ProcedureInvocationType.ORIGINAL.getValue());

        SerializationHelper.writeVarbinary(getProcNameBytes(), buf);

        buf.putLong(clientHandle);

        serializeParams(buf);

        int len = buf.position() - startPosition;
        assert(len == getSerializedSizeForOriginalVersion());
    }
317427
def frombits(cls, bits='0'):
        """"""
        if len(bits) > cls._len:
            raise ValueError('too many bits %r' % (bits,))
        return cls.fromint(bits[::-1], 2)
340042
def fromints(cls, ints):
        """"""
        return cls.frombitsets(map(cls.BitSet.fromint, ints))
144548
private Serializable deserializeObject(byte[] buffer) throws IOException, ClassNotFoundException {

        Serializable theObject = null;
        DeserializationObjectInputStream objectIn = null;

        if (buffer != null) {
            objectIn = new DeserializationObjectInputStream(new ByteArrayInputStream(buffer), Thread.currentThread().getContextClassLoader());
            theObject = (Serializable) objectIn.readObject();
            objectIn.close();
        }
        return theObject;
    }
540109
private static byte[] bitSetToByteArray(BitSet bitSet) {

    while (bitSet.length() % 8 != 0) { bitSet.set(bitSet.length(), true); }
    
    byte[] array = new byte[bitSet.length()/8];

    for (int i = 0; i < array.length; i++) {
      int offset = i * 8;

      int index = 0;
      for (int j = 0; j < 8; j++) {
        index <<= 1;        
        if (bitSet.get(offset+j)) { index++; }
      }

      array[i] = (byte)(index - 128);
    }

    return array;
  }