Ejemplo n.º 1
1
  protected Header readHeaderFromBuffer(ByteBuffer buffer) throws WWRuntimeException {
    // Read file code - first byte
    int fileCode = buffer.get();
    if (fileCode > 5) {
      String message = Logging.getMessage("SHP.NotADBaseFile", file.getPath());
      Logging.logger().log(java.util.logging.Level.SEVERE, message);
      throw new WWRuntimeException(message);
    }

    // Last update date
    int yy = 0xFF & buffer.get(); // unsigned
    int mm = buffer.get();
    int dd = buffer.get();

    // Number of records
    int numRecords = buffer.getInt();

    // Header struct length
    int headerLength = buffer.getShort();

    // Record length
    int recordLength = buffer.getShort();

    // Assemble header
    Header header = new Header();
    header.fileCode = fileCode;
    Calendar cal = Calendar.getInstance();
    cal.set(1900 + yy, mm - 1, dd);
    header.lastModificationDate = cal.getTime();
    header.numberOfRecords = numRecords;
    header.headerLength = headerLength;
    header.recordLength = recordLength;

    return header;
  }
Ejemplo n.º 2
0
    /** set the contents of this map from a ByteBuffer */
    void setData(ByteBuffer data) {
      short numGlyphs = data.getShort();
      glyphNameIndex = new short[numGlyphs];

      // the highest glyph index seen so far
      int maxGlyph = 257;
      for (int i = 0; i < numGlyphs; i++) {
        glyphNameIndex[i] = data.getShort();

        // see if this is the highest glyph
        if (glyphNameIndex[i] > maxGlyph) {
          maxGlyph = glyphNameIndex[i];
        }
      }

      // subtract off the default glyphs
      maxGlyph -= 257;

      // read in any additional names
      glyphNames = new String[maxGlyph];

      // read each name from a pascal string
      // the length is stored in the first byte, followed by
      // the data
      for (int i = 0; i < maxGlyph; i++) {
        // size in the first byte
        byte size = data.get();

        // then the data
        byte[] stringData = new byte[size];
        data.get(stringData);

        glyphNames[i] = new String(stringData);
      }
    }
Ejemplo n.º 3
0
  /** Initialize this structure from a ByteBuffer */
  public void setData(ByteBuffer data) {
    setFormat(data.getInt());
    setItalicAngle(data.getInt());
    setUnderlinePosition(data.getShort());
    setUnderlineThickness(data.getShort());
    setIsFixedPitch(data.getShort());
    data.getShort();
    setMinMemType42(data.getInt());
    setMaxMemType42(data.getInt());
    setMinMemType1(data.getInt());
    setMaxMemType1(data.getInt());

    // create the map, based on the type
    switch (format) {
      case 0x10000:
        nameMap = new PostMapFormat0();
        break;
      case 0x20000:
        nameMap = new PostMapFormat2();
        break;
      case 0x30000:
        // empty post map.
        nameMap = new PostMap();
        break;
      default:
        nameMap = new PostMap();
        System.out.println("Unknown post map type: " + Integer.toHexString(format));
        break;
    }

    // fill in the data in the map
    nameMap.setData(data);
  }
 @Override
 public void parse(DataSource dataSource, ByteBuffer header, long contentSize, BoxParser boxParser)
     throws IOException {
   ByteBuffer content = ByteBuffer.allocate(l2i(contentSize));
   dataSource.read(content);
   content.position(6);
   dataReferenceIndex = IsoTypeReader.readUInt16(content);
   displayFlags = content.getInt();
   textJustification = content.getInt();
   backgroundR = IsoTypeReader.readUInt16(content);
   backgroundG = IsoTypeReader.readUInt16(content);
   backgroundB = IsoTypeReader.readUInt16(content);
   defaultTextBox = IsoTypeReader.readUInt64(content);
   reserved1 = IsoTypeReader.readUInt64(content);
   fontNumber = content.getShort();
   fontFace = content.getShort();
   reserved2 = content.get();
   reserved3 = content.getShort();
   foregroundR = IsoTypeReader.readUInt16(content);
   foregroundG = IsoTypeReader.readUInt16(content);
   foregroundB = IsoTypeReader.readUInt16(content);
   if (content.remaining() > 0) {
     int length = IsoTypeReader.readUInt8(content);
     byte[] myFontName = new byte[length];
     content.get(myFontName);
     fontName = new String(myFontName);
   } else {
     fontName = null;
   }
   // initContainer(); there are no child boxes!?
 }
  @Override
  public void parse(ByteBuffer buf) {
    UserInfo user = new UserInfo(null, "bbz");
    //		user.setStatus( UserStatus.fromNum( buf.get() ) );
    ErrorCode code = ErrorCode.values()[buf.getShort()];

    if (code == ErrorCode.SUCCESS) { // 成功登陆
      user.setNickName(UtilBase.decodeString(buf)); // 昵称
      user.setSex(buf.get()); // 性别
      user.setAdult(buf.get() == 1 ? true : false); // 是否成年
      user.setStrength(buf.getShort()); // 体力
      user.setCash(buf.getInt()); // 金币
      user.setLoginCount(buf.getShort()); // 登陆次数
      user.setCreateTime(buf.getInt()); // 创建时间——秒数
    }
    int sleepMills = new Random().nextInt(500) * 1000;
    try {
      Thread.sleep(sleepMills);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println(
        code + "\t" + Thread.currentThread().getName() + "睡眠时间" + sleepMills / 1000 + "秒");
  }
Ejemplo n.º 6
0
  /**
   * Deserializer function for ARP packets.
   *
   * @return deserializer function
   */
  public static Deserializer<ARP> deserializer() {
    return (data, offset, length) -> {
      checkInput(data, offset, length, INITIAL_HEADER_LENGTH);

      ARP arp = new ARP();
      final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
      arp.setHardwareType(bb.getShort());
      arp.setProtocolType(bb.getShort());

      byte hwAddressLength = bb.get();
      arp.setHardwareAddressLength(hwAddressLength);

      byte protocolAddressLength = bb.get();
      arp.setProtocolAddressLength(protocolAddressLength);
      arp.setOpCode(bb.getShort());

      // Check we have enough space for the addresses
      checkHeaderLength(
          length, INITIAL_HEADER_LENGTH + 2 * hwAddressLength + 2 * protocolAddressLength);

      arp.senderHardwareAddress = new byte[0xff & hwAddressLength];
      bb.get(arp.senderHardwareAddress, 0, arp.senderHardwareAddress.length);
      arp.senderProtocolAddress = new byte[0xff & protocolAddressLength];
      bb.get(arp.senderProtocolAddress, 0, arp.senderProtocolAddress.length);
      arp.targetHardwareAddress = new byte[0xff & hwAddressLength];
      bb.get(arp.targetHardwareAddress, 0, arp.targetHardwareAddress.length);
      arp.targetProtocolAddress = new byte[0xff & protocolAddressLength];
      bb.get(arp.targetProtocolAddress, 0, arp.targetProtocolAddress.length);

      return arp;
    };
  }
Ejemplo n.º 7
0
 private Object getSingleValue(ByteBuffer buffer) {
   Object v;
   if (type.equals("float")) {
     v = buffer.getFloat();
   } else if (type.equals("double")) {
     v = buffer.getDouble();
   } else if (type.equals("int8_t") || type.equals("bool")) {
     v = (int) buffer.get();
   } else if (type.equals("uint8_t")) {
     v = buffer.get() & 0xFF;
   } else if (type.equals("int16_t")) {
     v = (int) buffer.getShort();
   } else if (type.equals("uint16_t")) {
     v = buffer.getShort() & 0xFFFF;
   } else if (type.equals("int32_t")) {
     v = buffer.getInt();
   } else if (type.equals("uint32_t")) {
     v = buffer.getInt() & 0xFFFFFFFFl;
   } else if (type.equals("int64_t")) {
     v = buffer.getLong();
   } else if (type.equals("uint64_t")) {
     v = buffer.getLong();
   } else if (type.equals("char")) {
     v = buffer.get();
   } else {
     throw new RuntimeException("Unsupported type: " + type);
   }
   return v;
 }
Ejemplo n.º 8
0
  public static BlockLink[] loadBlockMatches(String fileName) {
    File f = new File(fileName);
    if (!f.exists()) {
      return null;
    }
    try {
      FileChannel roChannel = new RandomAccessFile(f, "r").getChannel();

      int count = (int) ((roChannel.size() - 1) / 6);
      ByteBuffer buffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, roChannel.size());
      roChannel.close();
      buffer.order(ByteOrder.LITTLE_ENDIAN);
      if (buffer.get() != version) {
        return null;
      }

      BlockLink[] links = new BlockLink[count];
      for (int i = 0; i < links.length; i++) {
        links[i] = new BlockLink(buffer.getShort(), buffer.get(), buffer.get(), buffer.getShort());
      }

      return links;

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
Ejemplo n.º 9
0
  /**
   * Returns the size of the extended info record if {@code extras} contains a zip64 extended info
   * record, {@code -1} otherwise. The buffer will be positioned at the start of the extended info
   * record.
   */
  private static int getZip64ExtendedInfoSize(ByteBuffer extras) {
    try {
      while (extras.hasRemaining()) {
        final int headerId = extras.getShort() & 0xffff;
        final int length = extras.getShort() & 0xffff;
        if (headerId == ZIP64_EXTENDED_INFO_HEADER_ID) {
          if (extras.remaining() >= length) {
            return length;
          } else {
            return -1;
          }
        } else {
          extras.position(extras.position() + length);
        }
      }

      return -1;
    } catch (BufferUnderflowException bue) {
      // We'll underflow if we have an incomplete header in our extras.
      return -1;
    } catch (IllegalArgumentException iae) {
      // ByteBuffer.position() will throw if we have a truncated extra or
      // an invalid length in the header.
      return -1;
    }
  }
Ejemplo n.º 10
0
  /**
   * Creates ComplexBlock.
   *
   * @param bb : Input byte buffer.
   * @return byte[] : Complex block in byte array.
   */
  private static final byte[] loadComplexBlock(ByteBuffer bb) {
    // initialize buffer
    byte[] buffer = new byte[GeoStructure.BLOCK_CELLS * 3 + 1];

    buffer[0] = GeoStructure.COMPLEX;

    // load data
    for (int i = 0; i < GeoStructure.BLOCK_CELLS; i++) {
      // depending on geodata format
      if (Config.GEODATA_FORMAT != GeoFormat.L2D) {
        // get data
        short data = bb.getShort();

        // get nswe
        buffer[i * 3 + 1] = (byte) (data & 0x000F);

        // get height
        data = (short) ((short) (data & 0xFFF0) >> 1);
        buffer[i * 3 + 2] = (byte) (data & 0x00FF);
        buffer[i * 3 + 3] = (byte) (data >> 8);
      } else {
        // get nswe
        buffer[i * 3 + 1] = bb.get();

        // get height
        short height = bb.getShort();
        buffer[i * 3 + 2] = (byte) (height & 0x00FF);
        buffer[i * 3 + 3] = (byte) (height >> 8);
      }
    }

    return buffer;
  }
Ejemplo n.º 11
0
  public static void parse() throws FileNotFoundException, IOException {
    Archive config = Archive.decode(Cache.fileSystem.getFile(0, 2));

    datBuffer = config.getEntry("loc.dat").getBuffer();
    idxBuffer = config.getEntry("loc.idx").getBuffer();

    // BufferedWriter writer = new BufferedWriter(new FileWriter("./object_ids.txt"));

    positions = new int[idxBuffer.getShort() & 0xFFFF];
    definitions = new RSObjectDefinition[positions.length];
    int off = 2;
    for (int i = 0; i < positions.length; i++) {
      positions[i] = off;
      off += idxBuffer.getShort() & 0xFFFF;
    }

    for (int i = 0; i < definitions.length; i++) {
      definitions[i] = valueOf(i);

      //			writer.write("name= "+obj.name+"_"+i+", actions= "+Arrays.toString(obj.actions));
      //			writer.newLine();
    }

    for (int i = 0; i < cache.length; i++) {
      cache[i] = new RSObjectDefinition();
    }
    //	writer.close();
    Logger.getAnonymousLogger().info("Cached " + positions.length + " Object Definitions.");
  }
Ejemplo n.º 12
0
  public synchronized boolean getFromByteArray(ByteBuffer buffer) {
    byte[] header = new byte[4];
    buffer.get(header, 0, 4);

    int version = buffer.getInt();
    if (version != Constants.STRUCT_VERSION) {
      return false;
    }

    numPlayers = buffer.get();
    gameState = buffer.get();
    firstHalf = buffer.get();
    kickOffTeam = buffer.get();

    secGameState = buffer.get();
    dropInTeam = buffer.get();
    dropInTime = buffer.getShort();

    estimatedSecs = buffer.getInt();

    for (byte team = 0; team < Constants.NUM_TEAMS; team++) {
      setTeamNumber(team, buffer.get());
      buffer.get();
      setGoalColour(team, buffer.get());
      setScore(team, buffer.get());

      for (byte i = 0; i < Constants.MAX_NUM_PLAYERS; i++) {
        teams[team].getPlayer(i).setPenalty(buffer.getShort());
        teams[team].getPlayer(i).setSecsTillUnpenalised(buffer.getShort());
      }
    }

    return true;
  }
Ejemplo n.º 13
0
 private Object getValue(MAVLinkDataType type, int offset) {
   switch (type) {
     case CHAR:
       return payloadBB.get(offset);
     case UINT8:
       return payloadBB.get(offset) & 0xFF;
     case INT8:
       return (int) payloadBB.get(offset);
     case UINT16:
       return payloadBB.getShort(offset) & 0xFFFF;
     case INT16:
       return (int) payloadBB.getShort(offset);
     case UINT32:
       return payloadBB.getInt(offset) & 0xFFFFFFFFl;
     case INT32:
       return payloadBB.getInt(offset);
     case UINT64:
       return payloadBB.getLong(offset);
     case INT64:
       return payloadBB.getLong(offset);
     case FLOAT:
       return payloadBB.getFloat(offset);
     case DOUBLE:
       return payloadBB.getDouble(offset);
     default:
       throw new RuntimeException("Unknown type: " + type);
   }
 }
Ejemplo n.º 14
0
  public static PMT parsePMT(ByteBuffer data) {
    parseSection(data);

    // PMT itself
    int w1 = data.getShort() & 0xffff;
    int pcrPid = w1 & 0x1fff;

    int w2 = data.getShort() & 0xffff;
    int programInfoLength = w2 & 0xfff;

    List<Tag> tags = parseTags(NIOUtils.read(data, programInfoLength));
    List<PMTStream> streams = new ArrayList<PMTStream>();
    while (data.remaining() > 4) {
      int streamType = data.get() & 0xff;
      int wn = data.getShort() & 0xffff;
      int elementaryPid = wn & 0x1fff;

      Logger.info(String.format("Elementary stream: [%d,%d]", streamType, elementaryPid));

      int wn1 = data.getShort() & 0xffff;
      int esInfoLength = wn1 & 0xfff;
      ByteBuffer read = NIOUtils.read(data, esInfoLength);
      streams.add(new PMTStream(streamType, elementaryPid, MPSUtils.parseDescriptors(read)));
    }

    return new PMT(pcrPid, tags, streams);
  }
Ejemplo n.º 15
0
  private static boolean readHeader(RandomAccessFile in, ElfData elf) throws IOException {
    // http://www.sco.com/developers/gabi/1998-04-29/ch4.eheader.html
    byte[] bytes = new byte[ELF_HEADER_SIZE];
    in.readFully(bytes);
    if (bytes[0] != 127
        || bytes[1] != 'E'
        || bytes[2] != 'L'
        || bytes[3] != 'F'
        || (bytes[4] != 1 && bytes[4] != 2)) {
      Log.e(TAG, "ELF header invalid");
      return false;
    }

    elf.is64bits = bytes[4] == 2;
    elf.order =
        bytes[5] == 1
            ? ByteOrder.LITTLE_ENDIAN // ELFDATA2LSB
            : ByteOrder.BIG_ENDIAN; // ELFDATA2MSB

    // wrap bytes in a ByteBuffer to force endianess
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(elf.order);

    elf.e_machine = buffer.getShort(18); /* Architecture */
    elf.e_shoff = buffer.getInt(32); /* Section header table file offset */
    elf.e_shnum = buffer.getShort(48); /* Section header table entry count */
    return true;
  }
Ejemplo n.º 16
0
    public pcx_t(ByteBuffer b) {
      // is stored as little endian
      b.order(ByteOrder.LITTLE_ENDIAN);

      // fill header
      manufacturer = b.get();
      version = b.get();
      encoding = b.get();
      bits_per_pixel = b.get();
      xmin = b.getShort() & 0xffff;
      ymin = b.getShort() & 0xffff;
      xmax = b.getShort() & 0xffff;
      ymax = b.getShort() & 0xffff;
      hres = b.getShort() & 0xffff;
      vres = b.getShort() & 0xffff;
      b.get(palette = new byte[PALETTE_SIZE]);
      reserved = b.get();
      color_planes = b.get();
      bytes_per_line = b.getShort() & 0xffff;
      palette_type = b.getShort() & 0xffff;
      b.get(filler = new byte[FILLER_SIZE]);

      // fill data
      data = b.slice();
    }
Ejemplo n.º 17
0
  /**
   * Optimized {@code strlen} for 64-bit architectures.
   *
   * @see MemoryUtil#memByteBufferNT2(long, int)
   */
  int strlen64NT2(long address, int maxLength) {
    int i = 0;

    ByteBuffer buffer = memByteBuffer(address, maxLength);

    if (8 <= maxLength) {
      int misalignment = (int) address & 7;
      if (misalignment != 0) {
        // Align to 8 bytes
        for (int len = 8 - misalignment; i < len; i += 2) {
          if (buffer.getShort(i) == 0) return i;
        }
      }

      // Aligned longs for performance
      do {
        long v = buffer.getLong(i);
        if (((v - 0x0001000100010001L) & ~v & 0x8000800080008000L) != 0) break;
        i += 8;
      } while (i <= maxLength - 8);
    }

    // Tail
    for (; i < maxLength; i += 2) {
      if (buffer.getShort(i) == 0) break;
    }

    return i;
  }
Ejemplo n.º 18
0
  private void replaceRefs(IntIntMap replaceSpec, int guid, ByteBuffer buf, Set<Integer> pmtPids) {
    if (guid == 0) {
      PATSection pat = PATSection.parse(buf);
      for (int pids : pat.getPrograms().values()) {
        pmtPids.add(pids);
      }
    } else if (pmtPids.contains(guid)) {
      System.out.println(MainUtils.bold("PMT"));
      PSISection.parse(buf);

      buf.getShort();

      NIOUtils.skip(buf, buf.getShort() & 0xfff);

      while (buf.remaining() > 4) {
        byte streamType = buf.get();
        StreamType fromTag = MTSUtils.StreamType.fromTag(streamType);
        System.out.print(
            (fromTag == null ? "UNKNOWN" : fromTag)
                + "("
                + String.format("0x%02x", streamType)
                + "):\t");
        int wn = buf.getShort() & 0xffff;
        int wasPid = wn & 0x1fff;
        int elementaryPid = replacePid(replaceSpec, wasPid);
        buf.putShort(buf.position() - 2, (short) ((elementaryPid & 0x1fff) | (wn & ~0x1fff)));

        NIOUtils.skip(buf, buf.getShort() & 0xfff);
      }
    }
  }
Ejemplo n.º 19
0
  public void parse(ByteBuffer is) {
    super.parse(is);
    if ((flags & 0x1) != 0) // self ref
    return;
    type = NIOUtils.readString(is, 4);
    recordSize = is.getShort();
    version = is.getShort();
    kind = is.getShort();
    volumeName = NIOUtils.readPascalStringL(is, 27);
    volumeCreateDate = is.getInt();
    volumeSignature = is.getShort();
    volumeType = is.getShort();
    parentDirId = is.getInt();
    fileName = NIOUtils.readPascalStringL(is, 63);
    fileNumber = is.getInt();
    createdLocalDate = is.getInt();
    fileTypeName = NIOUtils.readString(is, 4);
    creatorName = NIOUtils.readString(is, 4);
    nlvlFrom = is.getShort();
    nlvlTo = is.getShort();
    volumeAttributes = is.getInt();
    fsId = is.getShort();
    NIOUtils.skip(is, 10);

    extra = new ArrayList<ExtraField>();
    while (true) {
      short type = is.getShort();
      if (type == -1) break;
      int len = is.getShort();
      byte[] bs = NIOUtils.toArray(NIOUtils.read(is, (len + 1) & 0xfffffffe));
      if (bs == null) break;
      extra.add(new ExtraField(type, len, bs));
    }
  }
Ejemplo n.º 20
0
  public static WavInfo readHeader(InputStream wavStream) throws IOException, DecoderException {

    ByteBuffer buffer = ByteBuffer.allocate(HEADER_SIZE);
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    wavStream.read(buffer.array(), buffer.arrayOffset(), buffer.capacity());

    buffer.rewind();
    buffer.position(buffer.position() + 20);
    int format = buffer.getShort();
    checkFormat(format == 1, "Unsupported encoding: " + format); // 1 means Linear PCM
    int channels = buffer.getShort();
    checkFormat(channels == 1 || channels == 2, "Unsupported channels: " + channels);
    int rate = buffer.getInt();
    checkFormat(rate <= 48000 && rate >= 11025, "Unsupported rate: " + rate);
    buffer.position(buffer.position() + 6);
    int bits = buffer.getShort();
    checkFormat(bits == 16, "Unsupported bits: " + bits);
    int dataSize = 0;
    while (buffer.getInt() != 0x61746164) { // "data" marker
      Log.d(TAG, "Skipping non-data chunk");
      int size = buffer.getInt();
      wavStream.skip(size);

      buffer.rewind();
      wavStream.read(buffer.array(), buffer.arrayOffset(), 8);
      buffer.rewind();
    }
    dataSize = buffer.getInt();
    checkFormat(dataSize > 0, "wrong datasize: " + dataSize);

    return new WavInfo(rate, channels == 2, dataSize);
  }
Ejemplo n.º 21
0
Archivo: ARP.java Proyecto: jmdbo/CGR
  @Override
  public IPacket deserialize(byte[] data, int offset, int length) throws PacketParsingException {
    ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
    this.hardwareType = bb.getShort();
    this.protocolType = bb.getShort();
    this.hardwareAddressLength = bb.get();
    this.protocolAddressLength = bb.get();
    if (this.hardwareAddressLength != 6) {
      String msg = "Incorrect ARP hardware address length: " + hardwareAddressLength;
      throw new PacketParsingException(msg);
    }
    if (this.protocolAddressLength != 4) {
      String msg = "Incorrect ARP protocol address length: " + protocolAddressLength;
      throw new PacketParsingException(msg);
    }
    this.opCode = ArpOpcode.of(bb.getShort());

    byte[] tmpMac = new byte[0xff & this.hardwareAddressLength];
    byte[] tmpIp = new byte[0xff & this.protocolAddressLength];

    bb.get(tmpMac, 0, this.hardwareAddressLength);
    this.senderHardwareAddress = MacAddress.of(tmpMac);
    bb.get(tmpIp, 0, this.protocolAddressLength);
    this.senderProtocolAddress = IPv4Address.of(tmpIp);

    bb.get(tmpMac, 0, this.hardwareAddressLength);
    this.targetHardwareAddress = MacAddress.of(tmpMac);
    bb.get(tmpIp, 0, this.protocolAddressLength);
    this.targetProtocolAddress = IPv4Address.of(tmpIp);

    return this;
  }
Ejemplo n.º 22
0
  /**
   * Remove all the local of a context (but keep global).
   *
   * @param context a counter context
   * @return a version of {@code context} where no shards are local.
   */
  public ByteBuffer clearAllLocal(ByteBuffer context) {
    int count = Math.abs(context.getShort(context.position()));
    if (count == 0) return context; // no local or global shards present.

    List<Short> globalShardIndexes = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
      short elt = context.getShort(context.position() + HEADER_SIZE_LENGTH + i * HEADER_ELT_LENGTH);
      if (elt < 0) globalShardIndexes.add(elt);
    }

    if (count == globalShardIndexes.size()) return context; // no local shards detected.

    // allocate a smaller BB for the cleared context - with no local header elts.
    ByteBuffer cleared =
        ByteBuffer.allocate(
            context.remaining() - (count - globalShardIndexes.size()) * HEADER_ELT_LENGTH);

    cleared.putShort(cleared.position(), (short) globalShardIndexes.size());
    for (int i = 0; i < globalShardIndexes.size(); i++)
      cleared.putShort(
          cleared.position() + HEADER_SIZE_LENGTH + i * HEADER_ELT_LENGTH,
          globalShardIndexes.get(i));

    int origHeaderLength = headerLength(context);
    ByteBufferUtil.arrayCopy(
        context,
        context.position() + origHeaderLength,
        cleared,
        cleared.position() + headerLength(cleared),
        context.remaining() - origHeaderLength);

    return cleared;
  }
Ejemplo n.º 23
0
  @Override
  public boolean deserialize() {
    ByteBuffer buffer = getBuffer();
    int bufferPosition = buffer.position();

    try {
      unknown1 = buffer.getInt();
      short prefix_unknown2 = buffer.getShort();
      unknown2 = new char[prefix_unknown2];

      for (int i = 0; i < prefix_unknown2; i++) {
        unknown2[i] = buffer.getChar();
      }
      short prefix_unknown3 = buffer.getShort();
      unknown3 = new char[prefix_unknown3];

      for (int i = 0; i < prefix_unknown3; i++) {
        unknown3[i] = buffer.getChar();
      }
    } catch (BufferUnderflowException e) {
      buffer.position(bufferPosition);
      return false;
    }

    return true;
  }
Ejemplo n.º 24
0
 /**
  * Create a roaring array based on a previously serialized ByteBuffer. As much as possible, the
  * ByteBuffer is used as the backend, however if you modify the content, the result is
  * unspecified.
  *
  * @param bb The source ByteBuffer
  */
 protected RoaringArray(ByteBuffer bb) {
   bb.order(ByteOrder.LITTLE_ENDIAN);
   if (bb.getInt() != SERIAL_COOKIE)
     throw new RuntimeException("I failed to find the right cookie.");
   this.size = bb.getInt();
   // we fully read the meta-data array to RAM, but the containers
   // themselves are memory-mapped
   this.array = new Element[this.size];
   final short keys[] = new short[this.size];
   final int cardinalities[] = new int[this.size];
   final boolean isBitmap[] = new boolean[this.size];
   for (int k = 0; k < this.size; ++k) {
     keys[k] = bb.getShort();
     cardinalities[k] = Util.toIntUnsigned(bb.getShort()) + 1;
     isBitmap[k] = cardinalities[k] > ArrayContainer.DEFAULT_MAX_SIZE;
   }
   for (int k = 0; k < this.size; ++k) {
     if (cardinalities[k] == 0) throw new RuntimeException("no");
     Container val;
     if (isBitmap[k]) {
       final LongBuffer bitmapArray = bb.asLongBuffer().slice();
       bitmapArray.limit(BitmapContainer.MAX_CAPACITY / 64);
       bb.position(bb.position() + BitmapContainer.MAX_CAPACITY / 8);
       val = new BitmapContainer(bitmapArray, cardinalities[k]);
     } else {
       final ShortBuffer shortArray = bb.asShortBuffer().slice();
       shortArray.limit(cardinalities[k]);
       bb.position(bb.position() + cardinalities[k] * 2);
       val = new ArrayContainer(shortArray, cardinalities[k]);
     }
     this.array[k] = new Element(keys[k], val);
   }
 }
Ejemplo n.º 25
0
  /**
   * Optimized {@code strlen} for 32-bit architectures.
   *
   * @see MemoryUtil#memByteBufferNT2(long, int)
   */
  int strlen32NT2(long address, int maxLength) {
    int i = 0;

    ByteBuffer buffer = memByteBuffer(address, maxLength);

    if (4 <= maxLength) {
      int misalignment = (int) address & 3;
      if (misalignment != 0) {
        // Align to 4 bytes
        for (int len = 4 - misalignment; i < len; i += 2) {
          if (buffer.getShort(i) == 0) return i;
        }
      }

      // Aligned longs for performance
      do {
        int v = buffer.getInt(i);
        if (((v - 0x00010001) & ~v & 0x80008000) != 0) break;
        i += 4;
      } while (i <= maxLength - 4);
    }

    // Tail
    for (; i < maxLength; i += 2) {
      if (buffer.getShort(i) == 0) break;
    }

    return i;
  }
Ejemplo n.º 26
0
 public void read(ByteBuffer buffer) {
   size = buffer.getFloat();
   strength = buffer.getFloat();
   phase = buffer.getFloat();
   pad = buffer.getFloat();
   depth = buffer.getShort();
   modification = buffer.getShort();
 }
Ejemplo n.º 27
0
 public Quaternion(I2cDeviceSynch.TimestampedData ts, double scale) {
   ByteBuffer buffer = ByteBuffer.wrap(ts.data).order(ByteOrder.LITTLE_ENDIAN);
   this.w = buffer.getShort() / scale;
   this.x = buffer.getShort() / scale;
   this.y = buffer.getShort() / scale;
   this.z = buffer.getShort() / scale;
   this.nanoTime = ts.nanoTime;
 }
Ejemplo n.º 28
0
 public void parseHeader(ByteBuffer buffer) {
   version = buffer.getShort() & 0xFFFF;
   count = buffer.getShort() & 0xFFFF;
   systemUptime = buffer.getInt() & 0xFFFFFFFFL;
   unixTime = buffer.getInt() & 0xFFFFFFFFL;
   seqNumber = buffer.getInt() & 0xFFFFFFFFL;
   sourceID = buffer.getInt() & 0xFFFFFFFFL;
 }
Ejemplo n.º 29
0
  /**
   * Constructor, obtains image parameters from the <tt>ByteBuffer</tt>.
   *
   * @param bytes <tt>ByteBuffer</tt> containing SHP file header data.
   */
  ShpFileHeaderCnc(ByteBuffer bytes) {
    super((short) (bytes.getShort() & 0xFFFF));

    bytes.getInt();
    width = bytes.getShort();
    height = bytes.getShort();
    unknown3 = bytes.getInt();
  }
Ejemplo n.º 30
0
 public dface_t(ByteBuffer b) {
   planenum = b.getShort() & 0xFFFF;
   side = b.getShort();
   firstedge = b.getInt();
   numedges = b.getShort();
   texinfo = b.getShort();
   b.get(styles);
   lightofs = b.getInt();
 }