⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 message.java

📁 tinyos-2.x.rar
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    if (val < 0)
      return val + 256;
    else
      return val;
  }

  // ASSUMES: little endian bits & bytes for the methods without BE, and
  // big endian bits & bytes for the methods with BE

  /**
   * Read the length bit unsigned little-endian int at offset
   * 
   * @param offset
   *          bit offset where the unsigned int starts
   * @param length
   *          bit length of the unsigned int
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   */
  protected long getUIntElement(int offset, int length) {
    checkBounds(offset, length);

    int byteOffset = offset >> 3;
    int bitOffset = offset & 7;
    int shift = 0;
    long val = 0;

    // all in one byte case
    if (length + bitOffset <= 8)
      return (ubyte(byteOffset) >> bitOffset) & ((1 << length) - 1);

    // get some high order bits
    if (bitOffset > 0) {
      val = ubyte(byteOffset) >> bitOffset;
      byteOffset++;
      shift += 8 - bitOffset;
      length -= 8 - bitOffset;
    }

    while (length >= 8) {
      val |= (long) ubyte(byteOffset++) << shift;
      shift += 8;
      length -= 8;
    }

    // data from last byte
    if (length > 0)
      val |= (long) (ubyte(byteOffset) & ((1 << length) - 1)) << shift;

    return val;
  }

  /**
   * Set the length bit unsigned little-endian int at offset to val
   * 
   * @param offset
   *          bit offset where the unsigned int starts
   * @param length
   *          bit length of the unsigned int
   * @param val
   *          value to set the bit field to
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   * @exception IllegalArgumentException
   *              if val is an out-of-range value for this bitfield
   */
  protected void setUIntElement(int offset, int length, long val) {
    checkBounds(offset, length);
    // checkValue(length, val);

    int byteOffset = offset >> 3;
    int bitOffset = offset & 7;
    int shift = 0;

    // all in one byte case
    if (length + bitOffset <= 8) {
      data[base_offset + byteOffset] = (byte) ((ubyte(byteOffset) & ~(((1 << length) - 1) << bitOffset)) | val << bitOffset);
      return;
    }

    // set some high order bits
    if (bitOffset > 0) {
      data[base_offset + byteOffset] = (byte) ((ubyte(byteOffset) & ((1 << bitOffset) - 1)) | val << bitOffset);
      byteOffset++;
      shift += 8 - bitOffset;
      length -= 8 - bitOffset;
    }

    while (length >= 8) {
      data[base_offset + (byteOffset++)] = (byte) (val >> shift);
      shift += 8;
      length -= 8;
    }

    // data for last byte
    if (length > 0)
      data[base_offset + byteOffset] = (byte) ((ubyte(byteOffset) & ~((1 << length) - 1)) | val >> shift);
  }

  /**
   * Read the length bit signed little-endian int at offset
   * 
   * @param offset
   *          bit offset where the signed int starts
   * @param length
   *          bit length of the signed int
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   */
  protected long getSIntElement(int offset, int length)
      throws ArrayIndexOutOfBoundsException {
    long val = getUIntElement(offset, length);

    if (length == 64)
      return val;

    if ((val & 1L << (length - 1)) != 0)
      return val - (1L << length);

    return val;
  }

  /**
   * Set the length bit signed little-endian int at offset to val
   * 
   * @param offset
   *          bit offset where the signed int starts
   * @param length
   *          bit length of the signed int
   * @param value
   *          value to set the bit field to
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   * @exception IllegalArgumentException
   *              if val is an out-of-range value for this bitfield
   */
  protected void setSIntElement(int offset, int length, long value)
      throws ArrayIndexOutOfBoundsException {
    if (length != 64 && value >= 1L << (length - 1))
      throw new IllegalArgumentException();

    if (length != 64 && value < 0)
      value += 1L << length;

    setUIntElement(offset, length, value);
  }

  /**
   * Read the length bit unsigned big-endian int at offset
   * 
   * @param offset
   *          bit offset where the unsigned int starts. Note that these are
   *          big-endian bit offsets: bit 0 is the MSB, bit 7 the LSB.
   * @param length
   *          bit length of the unsigned int
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   */
  protected long getUIntBEElement(int offset, int length) {
    checkBounds(offset, length);

    int byteOffset = offset >> 3;
    int bitOffset = offset & 7;
    long val = 0;

    // All in one byte case
    if (length + bitOffset <= 8)
      return (ubyte(byteOffset) >> (8 - bitOffset - length))
          & ((1 << length) - 1);

    // get some high order bits
    if (bitOffset > 0) {
      length -= 8 - bitOffset;
      val = (long) (ubyte(byteOffset) & ((1 << (8 - bitOffset)) - 1)) << length;
      byteOffset++;
    }

    while (length >= 8) {
      length -= 8;
      val |= (long) ubyte(byteOffset++) << length;
    }

    // data from last byte
    if (length > 0)
      val |= ubyte(byteOffset) >> (8 - length);

    return val;
  }

  /**
   * Set the length bit unsigned big-endian int at offset to val
   * 
   * @param offset
   *          bit offset where the unsigned int starts. Note that these are
   *          big-endian bit offsets: bit 0 is the MSB, bit 7 the LSB.
   * @param length
   *          bit length of the unsigned int
   * @param val
   *          value to set the bit field to
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   * @exception IllegalArgumentException
   *              if val is an out-of-range value for this bitfield
   */
  protected void setUIntBEElement(int offset, int length, long val) {
    checkBounds(offset, length);
    // checkValue(length, val);

    int byteOffset = offset >> 3;
    int bitOffset = offset & 7;
    int shift = 0;

    // all in one byte case
    if (length + bitOffset <= 8) {
      int mask = ((1 << length) - 1) << (8 - bitOffset - length);

      data[base_offset + byteOffset] = (byte) ((ubyte(byteOffset) & ~mask) | val << (8 - bitOffset - length));
      return;
    }

    // set some high order bits
    if (bitOffset > 0) {
      int mask = (1 << (8 - bitOffset)) - 1;

      length -= 8 - bitOffset;
      data[base_offset + byteOffset] = (byte) (ubyte(byteOffset) & ~mask | val >> length);
      byteOffset++;
    }

    while (length >= 8) {
      length -= 8;
      data[base_offset + (byteOffset++)] = (byte) (val >> length);
    }

    // data for last byte
    if (length > 0) {
      int mask = (1 << (8 - length)) - 1;

      data[base_offset + byteOffset] = (byte) ((ubyte(byteOffset) & mask) | val << (8 - length));
    }
  }

  /**
   * Read the length bit signed big-endian int at offset
   * 
   * @param offset
   *          bit offset where the signed int starts
   * @param length
   *          bit length of the signed int
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   */
  protected long getSIntBEElement(int offset, int length)
      throws ArrayIndexOutOfBoundsException {
    long val = getUIntBEElement(offset, length);

    if (length == 64)
      return val;

    if ((val & 1L << (length - 1)) != 0)
      return val - (1L << length);

    return val;
  }

  /**
   * Set the length bit signed big-endian int at offset to val
   * 
   * @param offset
   *          bit offset where the signed int starts
   * @param length
   *          bit length of the signed int
   * @param value
   *          value to set the bit field to
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset, length
   * @exception IllegalArgumentException
   *              if val is an out-of-range value for this bitfield
   */
  protected void setSIntBEElement(int offset, int length, long value)
      throws ArrayIndexOutOfBoundsException {
    if (length != 64 && value >= 1L << (length - 1))
      throw new IllegalArgumentException();

    if (length != 64 && value < 0)
      value += 1L << length;

    setUIntBEElement(offset, length, value);
  }

  /**
   * Read the 32 bit IEEE float at offset
   * 
   * @param offset
   *          bit offset where the float starts
   * @param length
   *          is ignored
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset
   */
  protected float getFloatElement(int offset, int length)
      throws ArrayIndexOutOfBoundsException {

    return Float.intBitsToFloat((int) getUIntElement(offset, 32));
  }

  /**
   * Set the 32 bit IEEE float at offset to value
   * 
   * @param offset
   *          bit offset where the float starts
   * @param length
   *          is ignored
   * @param value
   *          value to store in bitfield
   * @exception ArrayIndexOutOfBoundsException
   *              for invalid offset
   */
  protected void setFloatElement(int offset, int length, float value)
      throws ArrayIndexOutOfBoundsException {

    // using SInt because floatToRawIntBits might return a negative value
    setSIntElement(offset, 32, Float.floatToRawIntBits(value));
  }

  /**
   * 
   * @return the SerialPacket this message originated from, if it was set
   *     externally
   */
  public SerialPacket getSerialPacket() {
    return serialPacket;
  }

  /**
   * 
   * @param mySerialPacket the SerialPacket this message originated from
   */
  protected void setSerialPacket(SerialPacket mySerialPacket) {
    serialPacket = mySerialPacket;
  }
  
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -