Example #1
0
  public void post(long timestamp, float value) throws IOException {
    File file = getFile();
    RandomAccessFile rdf = null;

    try {
      rdf = new RandomAccessFile(file, "rw");

      // On vérifie que la longueur est cohérente
      long len = rdf.length();
      if (len > 0) {
        int mod = (int) (len % DATA_LEN);
        if (mod != 0) {
          logger.warning(
              "Taille de fichier incoherence ("
                  + len / DATA_LEN
                  + "x"
                  + DATA_LEN
                  + ", reste "
                  + mod
                  + "). retour a "
                  + (len - mod));
          len = len - mod;
        }

        if (timestamp < getLast(rdf).timestamp) {
          logger.warning(
              "la nouvelle valeur anterieure a la derniere (prev:"
                  + sdf.format(new Date((long) last.timestamp * 1000))
                  + " new:"
                  + sdf.format(new Date((long) timestamp * 1000))
                  + ")");
        }

        // Positionnement en fin de fichier
        rdf.seek(len);
      }

      // On écrit l'enregistrement
      if (timestamp > Integer.MAX_VALUE) {
        logger.warning("timestamp tronqué : " + timestamp + "/" + (int) timestamp);
      }

      if (last == null) last = new Entry();
      last.timestamp = (int) timestamp;
      last.value = value;

      logger.fine("write: " + value + "(" + sdf.format(new Date(timestamp * 1000)) + ")");
      rdf.writeInt((int) last.timestamp);
      rdf.writeFloat(last.value);
    } finally {
      if (rdf != null) rdf.close();
    }
  }
Example #2
0
File: RAF.java Project: Utlesh/code
  public static void main(String args[]) {
    try {
      String fname = "d:\\q.txt";
      String mode;
      // mode = "r";//r : file must exist
      mode = "rw"; // rw : file will be created or opened

      // open the file
      RandomAccessFile raf = new RandomAccessFile(fname, mode);

      /*
      //seek and write demo

         raf.seek(10);//position file r/w pointer at index 10 wrt BOF
         //a seek beyond file size causes file to grow upto the seek value
         raf.write(65);//raf.write('A');

      */

      // r/w java datatypes
      int i1, i2;
      float f1, f2;
      char c1, c2;
      String s1, s2;

      i1 = -10;
      f1 = 1234.5678F;
      c1 = 'q';
      s1 = "hello files";

      raf.seek(0); // reach BOF
      raf.writeInt(i1);
      raf.writeFloat(f1);
      raf.writeChar(c1);
      raf.writeUTF(s1);

      raf.seek(0); // reach BOF
      i2 = raf.readInt();
      f2 = raf.readFloat();
      c2 = raf.readChar();
      s2 = raf.readUTF();

      System.out.println(i2);
      System.out.println(f2);
      System.out.println(c2);
      System.out.println(s2);

      // close the file
      raf.close();
    } catch (IOException ex) {
      System.out.println(ex); // ex converts into ex.toString()
    }
  } // main
Example #3
0
 private void append(SingleHit[] hits, String prefix, int chrom) throws IOException {
   RandomAccessFile positionsRAF = new RandomAccessFile(getPositionsFname(prefix, chrom), "rw");
   RandomAccessFile weightsRAF = new RandomAccessFile(getWeightsFname(prefix, chrom), "rw");
   RandomAccessFile lasRAF = new RandomAccessFile(getLaSFname(prefix, chrom), "rw");
   positionsRAF.seek(positionsRAF.length());
   weightsRAF.seek(weightsRAF.length());
   lasRAF.seek(lasRAF.length());
   for (int i = 0; i < hits.length; i++) {
     SingleHit h = hits[i];
     positionsRAF.writeInt(h.pos);
     weightsRAF.writeFloat(h.weight);
     lasRAF.writeInt(makeLAS(h.length, h.strand));
   }
   positionsRAF.close();
   weightsRAF.close();
   lasRAF.close();
 }
 public static void appendData(File outfile, float[] data) throws IOException {
   RandomAccessFile raFile = new RandomAccessFile(outfile, "rw");
   try {
     SacHeader header = new SacHeader(raFile);
     if (header.getLeven() == FALSE
         || header.getIftype() == IRLIM
         || header.getIftype() == IAMPH) {
       raFile.close();
       throw new IOException("Can only append to evenly sampled sac files, ie only Y");
     }
     int origNpts = header.getNpts();
     header.setNpts(header.getNpts() + data.length);
     header.setE((header.getNpts() - 1) * header.getDelta());
     raFile.seek(0);
     header.writeHeader(raFile);
     raFile.skipBytes(origNpts * 4); // four bytes per float
     if (header.getByteOrder() == LITTLE_ENDIAN) {
       // Phil Crotwell's solution:
       // careful here as dos.writeFloat() will collapse all NaN floats
       // to
       // a single NaN value. But we are trying to write out byte
       // swapped values
       // so different floats that are all NaN are different values in
       // the
       // other byte order. Solution is to swap on the integer bits,
       // not the float.
       for (int i = 0; i < data.length; i++) {
         raFile.writeInt(SacHeader.swapBytes(Float.floatToRawIntBits(data[i])));
       }
     } else {
       for (int i = 0; i < data.length; i++) {
         raFile.writeFloat(data[i]);
       }
     }
   } finally {
     raFile.close();
   }
 }
Example #5
0
 /**
  * This writes the binary float to the file. Later, use readBinaryFloat to read the value from the
  * file.
  */
 public void writeBinaryFloat(int col, int row, float f) throws IOException {
   raf.seek(row * nBytesPerRow + columnStartAt[col]);
   raf.writeFloat(f);
 }
 /* @see java.io.DataOutput.writeFloat(float) */
 public void writeFloat(float v) throws IOException {
   raf.writeFloat(v);
 }