@Test
 public void test() {
   byte[] fromObj = {113, 123, 124, 23};
   byte[] result = Helper.byteArrayCut(fromObj, 0, 2);
   for (int i = 0; i < 2; i++) {
     System.out.println(result[i]);
   }
   byte[] result2 = Helper.byteArrayCut(fromObj, 2, 2);
   for (int i = 0; i < 2; i++) {
     System.out.println(result2[i]);
   }
 }
  // TODO Implementation pending
  public Bucket readBucketFromFile(String path, Long offset, String datatype) {

    // If offset is -1 , return null
    // Since the bucket cannot be read.
    if (offset == -1) return null;

    RandomAccessFile raf;
    Bucket temp = new Bucket(this.maxSize, (long) -1);
    byte[] tempData = new byte[4];
    byte[] tempOffsetAddress = new byte[8];
    long tempOffset = 0;
    Comparer comparer = new Comparer();
    try {
      raf = new RandomAccessFile(new File(path), "rw");
      raf.seek(offset);

      raf.read(tempData);
      temp.maxSize = Helper.toInt(tempData);
      raf.read(tempData);
      temp.currentSize = Helper.toInt(tempData);
      raf.read(tempData);
      temp.numberOfOverflowBuckets = Helper.toInt(tempData);

      raf.read(tempOffsetAddress);
      temp.overflowOffset = Helper.toLong(tempOffsetAddress);
      tempOffset = raf.getFilePointer();
      raf.close();

      for (int i = 0; i < this.maxSize; i++) {
        if (datatype.contains("c")) {
          temp.data[i][0] =
              comparer.compare_functions[6].readString(
                  path, (int) tempOffset, Integer.parseInt(datatype.substring(1)));
        } else
          temp.data[i][0] =
              comparer.compare_functions[comparer.mapper.indexOf(datatype)].readString(
                  path, (int) tempOffset, Integer.parseInt(datatype.substring(1)));
        tempOffset += Integer.parseInt(datatype.substring(1));

        temp.data[i][1] = comparer.compare_functions[3].readString(path, (int) tempOffset, 8);
        tempOffset += 8;
      }

      return temp;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
Exemple #3
0
  private static void merge(Comparable<?> a[], Comparable<?> aux[], int lo, int mid, int hi) {
    assert Helper.isSorted(a, lo, mid);
    assert Helper.isSorted(a, mid + 1, hi);

    for (int k = lo; k <= hi; k++) aux[k] = a[k];

    int i = lo, j = mid + 1;
    for (int k = lo; k <= hi; k++) {
      if (i > mid) a[k] = aux[j++];
      else if (j > hi) a[k] = aux[i++];
      else if (Helper.less(aux[j], aux[i])) a[k] = aux[j++];
      else a[k] = aux[i++];
    }
  }
 // 可以依据taskkind的byte数据获得task的种类,返回一个TaskKind
 public static InstructionTaskKind getTaskKind(byte[] taskByte) {
   if (taskByte.length != InstructionDecode.TASK_KIND_LENGTH) {
     return InstructionTaskKind.UnKnow;
   }
   if (Helper.byteArrayEqual(taskByte, TASK_DATA_OPEN)) {
     return InstructionTaskKind.Open;
   } else if (Helper.byteArrayEqual(taskByte, TASK_DATA_SLEEP)) {
     return InstructionTaskKind.Sleep;
   } else if (Helper.byteArrayEqual(taskByte, TASK_DATA_CLOSE)) {
     return InstructionTaskKind.Close;
   } else if (Helper.byteArrayEqual(taskByte, TASK_DATA_WAIT)) {
     return InstructionTaskKind.Wait;
   } else {
     return InstructionTaskKind.UnKnow;
   }
 }
  public void writeBucketToFile(String path, Long offset, String datatype) {

    //		this.writeData(); // For testing purposes.
    RandomAccessFile raf;
    Comparer comparer = new Comparer();

    try {
      raf = new RandomAccessFile(new File(path), "rw");
      raf.seek(offset);

      raf.write(Helper.toByta(this.maxSize));
      raf.write(Helper.toByta(this.currentSize));
      raf.write(Helper.toByta(this.numberOfOverflowBuckets));
      raf.write(Helper.toByta(this.overflowOffset));
      offset = raf.getFilePointer();

      for (int i = 0; i < this.maxSize; i++) {
        if (datatype.contains("c")) {
          comparer.compare_functions[6].writeAtOffset(
              raf, offset, this.data[i][0] + "", Integer.parseInt(datatype.substring(1)));
        }
        // Use appropriate write method based on the datatype that the index file holds.
        else
          comparer.compare_functions[comparer.mapper.indexOf(datatype)].writeAtOffset(
              raf, offset, this.data[i][0] + "", Integer.parseInt(datatype.substring(1)));
        offset += Integer.parseInt(datatype.substring(1));
        // Write the pointer , right after the
        comparer.compare_functions[3].writeAtOffset(raf, offset, this.data[i][1] + "", 8);
        offset += 8;
      }
      raf.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  private void initBrightnessSensorReader() {
    if (_brightnessReaderThread != null) return;

    if (Helper.isWindows() || _configuredAutoBrightnessPin == null) {
      _brightnessSensorReader = new DummyBrightnessSensorReader(100);
      if (_configuredAutoBrightnessPin == null)
        System.out.println(
            "Warning: brightness.properties Brightness.AutoBrightness.LdrPinName invalid, falling back to dummy auto-brightness");
    } else {
      _brightnessSensorReader =
          new GpioLdrReader(
              _configuredAutoBrightnessPin, _configuredAutoBrightnessCapacitorUnloadNs);
    }

    _brightnessReaderThread =
        new BrightnessReaderThread(
            _brightnessSensorReader,
            _configuredAutoBrightnessNotificationThreshold,
            _configuredAutoBrightnessMsBetweenUpdates,
            _configuredAutoBrightnessNumValuesForAverage);
    _brightnessReaderThread.addObserver(this);

    new Thread(_brightnessReaderThread).start();
  }
 public InstructionTaskKind getTaskKind() {
   int pos = FROM_ADDRESS_LENGTH + TO_ADDRESS_LENGTH;
   byte[] kind = Helper.byteArrayCut(data, pos, TASK_KIND_LENGTH);
   return TaskMap.getTaskKind(kind);
 }
 public byte[] getToAdress() {
   byte[] toAdress = Helper.byteArrayCut(data, FROM_ADDRESS_LENGTH, TO_ADDRESS_LENGTH);
   return toAdress;
 }
 public byte[] getFromAddress() {
   byte[] fromAdress = Helper.byteArrayCut(data, 0, FROM_ADDRESS_LENGTH);
   return fromAdress;
 }
 @Test
 public void testGetStringList() throws JSONException {
   assertEquals(empty_JAVA, Helper.getStringList(empty_JSON));
   assertEquals(one_JAVA, Helper.getStringList(one_JSON));
   assertEquals(more_JAVA, Helper.getStringList(more_JSON));
 }