Example #1
0
  /* Binary Format
   *
   * format 			(13 bytes)
   * version 			(5 bytes)
   * size 			(4 bytes)
   *
   * schedulePiece	(N bytes)
   * 		endTime		(4 bytes)
   * 		size		(4 bytes)
   * 		scIndex		(4 bytes)
   * 		...
   * 		scIndex		(4 bytes)
   * ...
   * schedulePiece	(N bytes)
   *
   * */
  public byte[] encode() throws IOException {
    if (isScheduleUpdated || null == bytes) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();

      // TODO: write format and version
      bos.write(format.getBytes());
      bos.write(version.getBytes());
      bos.write(BinaryUtil.intToBytes(toSchedulePieces().size()));

      for (SchedulePiece p : toSchedulePieces()) {
        bos.write(BinaryUtil.intToBytes(p.getEndTime()));
        bos.write(BinaryUtil.intToBytes(p.soundCount()));

        for (int index = 0; index < p.soundCount(); index++)
          bos.write(BinaryUtil.intToBytes(p.getSoundIndex(index)));
      }

      bytes = bos.toByteArray();
      size = bytes.length;
      bos.close();
    }

    return bytes;
  }
Example #2
0
  public static SoundSchedule decode(byte[] bytes, int offset) throws IOException {
    int initialOffset = offset;
    int bytesIndex = offset;

    // check format and version
    if (!format.equals(BinaryUtil.toString(bytes, bytesIndex, format.length()))) return null;
    bytesIndex += format.length();

    if (!version.equals(BinaryUtil.toString(bytes, bytesIndex, version.length()))) return null;
    bytesIndex += version.length();

    // decodes schedule pieces

    int schedulePieceCount = BinaryUtil.toInt(bytes, bytesIndex);
    ArrayList<SchedulePiece> schedulePieces = new ArrayList<SchedulePiece>(schedulePieceCount);
    bytesIndex += 4;

    for (int schedulePieceIndex = 0;
        schedulePieceIndex < schedulePieceCount;
        schedulePieceIndex++) {
      int endTime = BinaryUtil.toInt(bytes, bytesIndex);
      bytesIndex += 4;

      int scIndexCount = BinaryUtil.toInt(bytes, bytesIndex);
      int[] scIndecies = new int[scIndexCount];
      bytesIndex += 4;

      for (int scIndexIndex = 0; scIndexIndex < scIndexCount; scIndexIndex++) {
        scIndecies[scIndexIndex] = BinaryUtil.toInt(bytes, bytesIndex);
        bytesIndex += 4;
      }

      schedulePieces.add(new SchedulePiece(endTime, scIndecies));
    }

    SoundSchedule schedule = new SoundSchedule(schedulePieces);
    schedule.size = bytesIndex - initialOffset;
    return schedule;
  }