public static void writeByteBuffer(ByteBuffer bbuf, String filename) {
    // Write bbuf to filename
    File file;
    try {
      // Get log file
      String logfile = "C:\\" + filename + ".txt";
      file = new File(logfile);
      boolean exists = file.exists();
      if (!exists) {
        // create a new, empty node file
        try {
          file = new File(logfile);
          boolean success = file.createNewFile();
        } catch (IOException e) {
          System.out.println("Create Event Log file failed!");
        }
      }
      try {
        // Create a writable file channel
        FileChannel wChannel = new FileOutputStream(file, true).getChannel();
        // Write the ByteBuffer contents; the bytes between the ByteBuffer's
        // position and the limit is written to the file
        wChannel.write(bbuf);

        // Close the file
        wChannel.close();
      } catch (IOException e) {
      }
    } catch (java.lang.Exception e) {
    }
  }
 public static void writeLogFile(String descript, String filename) {
   File file;
   FileOutputStream outstream;
   // BufferedWriter outstream;
   Date time = new Date();
   long bytes = 30000000;
   try {
     // Get log file
     String logfile = "C:\\" + filename + ".txt";
     file = new File(logfile);
     boolean exists = file.exists();
     if (!exists) {
       // create a new, empty node file
       try {
         file = new File(logfile);
         boolean success = file.createNewFile();
       } catch (IOException e) {
         System.out.println("Create Event Log file failed!");
       }
     }
     try {
       descript = descript + "\n";
       outstream = new FileOutputStream(file, true);
       for (int i = 0; i < descript.length(); ++i) {
         outstream.write((byte) descript.charAt(i));
       }
       outstream.close();
     } catch (IOException e) {
     }
   } catch (java.lang.Exception e) {
   }
 }
示例#3
0
 /**
  * This function writes the generated relative overview to a file.
  *
  * @param f The file to write to.
  * @throws IOException Thrown if unable to open or write to the file.
  * @throws InsufficientDataException Thrown if unable to generate the overview.
  */
 public void writeToFile(File f) throws IOException, InsufficientDataException {
   f.createNewFile();
   FileWriter fw = new FileWriter(f);
   fw.write(generateOverviewText());
   fw.close();
 }
  public static int decompress(byte[] source) {

    int codingparameter, lengthofpacket;
    int numdecodedsamples;
    int packetbytes_length;
    int denomexponent = 14;
    int muexponent = 15;
    int biasscalebits = 8;
    int halfmu = (1 << (muexponent - 1)) - 1;
    int biasscalealmosthalf = (1 << (biasscalebits - 1)) - 1;
    int Msg_head = 24; // Message header in bytes
    int weightqshift = denomexponent + capexponent - weightquantbits + 1;

    packetbytes_length = source.length - Msg_head;
    decodepacketbitpointer = 0;
    decodepacketbytepointer = 0;
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 84; j++) {
        destination[i][j] = 0;
      }
    }
    decodedfoldedsamples = new int[2048];
    decodedunfoldedsamples = new int[2048];
    packetdebiasedsamples = new int[2048];
    for (int k = 0; k < 2048; k++) {
      decodedfoldedsamples[k] = 0;
      decodedunfoldedsamples[k] = 0;
      packetdebiasedsamples[k] = 0;
    }
    // Create a logfile

    String logfile = "C:\\" + "test_compression_ratio" + ".txt";
    compress_logfile = new File(logfile);
    boolean exists = compress_logfile.exists();
    if (!exists) {
      // create a new, empty node file
      try {
        compress_logfile = new File(logfile);
        boolean success = compress_logfile.createNewFile();
      } catch (IOException e) {
        System.out.println("Create Event Log file failed!");
      }
    }

    time_s = "";
    // System.out.println("--------Decompressing start*****************************" + "\n");

    numdecodedsamples =
        decodepacket(
            codeoverheadbits, packetbytes_length, source, Msg_head); // 1 sample costs 2 bytes

    // Reset bias estimate based on quantized value just read from packet
    // int biasestimate_r = decoderbiasestimate_r;

    // System.out.println("The decoded number of sample in the packet =" + numdecodedsamples + " | "
    // + "decoded biasestimate: " + biasestimate_r + "\n");

    // Reset weight vector based on quantized components just read from packet
    for (int i = 0; i < order; i++) {

      if (decodedweightquant[i] > 0) {
        weight_r[i] = (decodedweightquant[i]) << weightqshift;
      } else if (decodedweightquant[i] < 0) {
        weight_r[i] = -((-decodedweightquant[i]) << weightqshift);
      } else {
        weight_r[i] = 0;
      }
      // System.out.println("decodedweightquant[i]:"+decodedweightquant[i]+"weight_r"+ weight_r[i]+
      // "| weightqshift" + weightqshift + "\n");
    }

    // At this point we have bias estimate and weight vector, and a packet of folded samples.
    // follow the prediction procedure to decode each sample given the folded value
    for (int i = 0; i < numdecodedsamples; i++) {
      int j;
      int predicteddebiased_r;
      long predictedsample_r;
      int err_r; // error in prediction of de-biased signal
      int maxsample_r = ((1 << (bitdepth)) - 1) << denomexponent;
      int minsample_r = (-(1 << (bitdepth))) << denomexponent;

      // System.out.println("max sample value:" + (maxsample_r>>denomexponent) + "\n");

      predicteddebiased_r = predictdebiasedsample_r(i);
      predictedsample_r = predicteddebiased_r + decoderbiasestimate_r;

      // predicted sample value cannot exceed the known instrument dynamic range
      if (predictedsample_r > maxsample_r) predictedsample_r = maxsample_r;
      if (predictedsample_r < minsample_r) predictedsample_r = minsample_r;

      decodedunfoldedsamples[i] = unfoldsample(decodedfoldedsamples[i], predictedsample_r);

      // System.out.println("predicteddebiased_r:" + predicteddebiased_r + "decoderbiasestimate_r" +
      // decoderbiasestimate_r + "\n");

      // System.out.println("Decoded folded sample value:" + decodedfoldedsamples[i] +
      // "predictedsample_r" + predictedsample_r + "\n");

      // System.out.println("Sample: " + i + "Decoded unfolded samples:" + decodedunfoldedsamples[i]
      // + "\n");

      packetdebiasedsamples[i] =
          decodedunfoldedsamples[i] - ((decoderbiasestimate_r + almosthalf) >> denomexponent);
      /* update weight vector if we have sufficient samples in the packet */
      if (i >= order) {
        err_r = predicteddebiased_r - (packetdebiasedsamples[i] << denomexponent);

        if (err_r < 0)
          for (j = 1; j <= order; j++)
            weight_r[j - 1] +=
                ((packetdebiasedsamples[i - j] << denomexponent) + halfmu) >> muexponent;
        else
          for (j = 1; j <= order; j++)
            weight_r[j - 1] -=
                ((packetdebiasedsamples[i - j] << denomexponent) + halfmu) >> muexponent;
      }

      /* update bias estimate */
      decoderbiasestimate_r -=
          (decoderbiasestimate_r
                  - (decodedunfoldedsamples[i] << denomexponent)
                  + biasscalealmosthalf)
              >> biasscalebits;

      /* At this point, decodedsample is our decoded sample value and the prediction information has been updated to decode the next sample. */
    }

    write_Byte_array(
        Msg_head,
        packetbytes_length,
        numdecodedsamples,
        source); // judge whether the generated array is contained in one packet.

    /*int arr_num = (numdecodedsamples / 28)+1;
    	for (int k=0;k<arr_num-1 ;k++ )
    	{
    		decodepacketbitpointer = 0;
    		decodepacketbytepointer = 28;
    		for (int i=0;i<28 ;i++ )
    		{
    			System.out.println("***Samples decompressed from each packet K : " + k + " | " + readint(16,destination[k]) + "\n");
    		}
    	}
    	if ((numdecodedsamples % 28) != 0)
    	{
    		decodepacketbitpointer = 0;
    		decodepacketbytepointer = 28;
    		for (int k=0;k< numdecodedsamples % 28 ;k++ )
    		{
    			System.out.println("***Samples decompressed from each packet :  " + (numdecodedsamples % 28) + " | "+ readint(16,destination_last_row) + "\n");
    		}
    	}
    */
    return numdecodedsamples;
  }