/**
   * Creates and initializes an SPV block store. Will create the given file if it's missing. This
   * operation will block on disk.
   */
  public SPVBlockStore(NetworkParameters params, File file) throws BlockStoreException {
    checkNotNull(file);
    this.params = checkNotNull(params);
    try {
      this.numHeaders = DEFAULT_NUM_HEADERS;
      boolean exists = file.exists();
      // Set up the backing file.
      randomAccessFile = new RandomAccessFile(file, "rw");
      long fileSize = getFileSize();
      if (!exists) {
        log.info("Creating new SPV block chain file " + file);
        randomAccessFile.setLength(fileSize);
      } else if (randomAccessFile.length() != fileSize) {
        throw new BlockStoreException(
            "File size on disk does not match expected size: "
                + randomAccessFile.length()
                + " vs "
                + fileSize);
      }

      FileChannel channel = randomAccessFile.getChannel();
      fileLock = channel.tryLock();
      if (fileLock == null)
        throw new BlockStoreException("Store file is already locked by another process");

      // Map it into memory read/write. The kernel will take care of flushing writes to disk at the
      // most
      // efficient times, which may mean that until the map is deallocated the data on disk is
      // randomly
      // inconsistent. However the only process accessing it is us, via this mapping, so our own
      // view will
      // always be correct. Once we establish the mmap the underlying file and channel can go away.
      // Note that
      // the details of mmapping vary between platforms.
      buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, fileSize);

      // Check or initialize the header bytes to ensure we don't try to open some random file.
      byte[] header;
      if (exists) {
        header = new byte[4];
        buffer.get(header);
        if (!new String(header, "US-ASCII").equals(HEADER_MAGIC))
          throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC);
      } else {
        initNewStore(params);
      }
    } catch (Exception e) {
      try {
        if (randomAccessFile != null) randomAccessFile.close();
      } catch (IOException e2) {
        throw new BlockStoreException(e2);
      }
      throw new BlockStoreException(e);
    }
  }
 /**
  * reads the content of a group of existing files in a zipped block
  *
  * @param domain The namespace used to identify the application domain (1st level directory) to
  *     use
  * @param paths The paths relative to the domain for the files to be read
  * @param offset The offset in the file to start reading from
  * @param len The length of the block in bytes in zipped form
  * @return The contents of the file in zipped form together with the description of the files
  */
 public MultiFileBlock readFromStreamZipped(String[] paths, long offset, int len) {
   MultiFileBlock block = null;
   try {
     if (_isLocal) {
       int startFileIndex =
           Util.getStartFileIndex(
               _rootFile.getCanonicalPath() + File.separatorChar, paths, offset);
       long startFileOffset =
           Util.getStartFileOffset(
               _rootFile.getCanonicalPath() + File.separatorChar, paths, offset);
       int i = startFileIndex;
       long j = startFileOffset;
       // int bufSize=0;
       int totalBufSize = 0;
       int readResult = 0;
       ByteArrayOutputStream bOut = new ByteArrayOutputStream(len);
       GZIPOutputStream zOut = new GZIPOutputStream(bOut, len);
       block = new MultiFileBlock();
       while (totalBufSize < len && i < paths.length) {
         File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + paths[i]);
         if (tmpFile.isFile() && tmpFile.exists()) {
           RandomAccessFile in = new RandomAccessFile(tmpFile, "r");
           byte[] tmpBuf = new byte[len - totalBufSize];
           in.seek(j);
           while (totalBufSize < len && in.getFilePointer() < in.length()) {
             readResult = in.read(tmpBuf);
             if (readResult != -1) {
               zOut.write(tmpBuf, 0, readResult);
               // bufSize = bOut.size();
               totalBufSize += readResult;
             } else {
               break;
             }
           }
           BlockFileDescriptor des = new BlockFileDescriptor(_domain, paths[i], in.length());
           block.addBlockFileDescriptor(des);
           in.close();
           i++;
           j = 0;
         } else {
           return null;
         }
       }
       zOut.close();
       block.setBlockData(bOut.toByteArray());
     } else {
       block = _remote.readFromStreamZipped(_domain, paths, offset, len);
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return block;
 }
  /**
   * Add the given text string to the end of a given file.
   *
   * @param inStr The string to be added.
   * @param fileStr the name of the file to be added.
   */
  public static void addStringToFile(String inStr, String fileStr) throws Exception {

    RandomAccessFile raFile = new RandomAccessFile(fileStr, "rw");
    raFile.seek(raFile.length());
    raFile.writeBytes(inStr);
    raFile.close();
  }
 public long length() {
   try {
     return image.length();
   } catch (IOException e) {
     return -1L;
   }
 }
 /**
  * reads the content of an existing file using the current domain
  *
  * @param domain The namespace used to identify the application domain (1st level directory) to
  *     use
  * @param path The path relative to the domain for the file
  * @param block The sequential block number for the data to be read starting with 1
  * @param len The length of the block in bytes
  * @return The contents of the file
  */
 public byte[] readFromFile(String path, int block, int len) {
   byte[] buffer = null;
   try {
     if (_isLocal) {
       File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path);
       if (tmpFile.isFile()) {
         RandomAccessFile in = new RandomAccessFile(tmpFile, "r");
         in.seek((block - 1) * len);
         int result = -1;
         buffer = new byte[len];
         if (in.getFilePointer() < in.length()) {
           result = in.read(buffer);
           ByteArrayOutputStream out = new ByteArrayOutputStream(result);
           out.write(buffer, 0, result);
           in.close();
           return out.toByteArray();
         } else {
           in.close();
         }
       }
     } else {
       buffer = _remote.readFromFile(_domain, path, block, len);
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return buffer;
 }
  // see DbFile.java for javadocs
  public ArrayList<Page> insertTuple(TransactionId tid, Tuple t)
      throws DbException, IOException, TransactionAbortedException {
    // some code goes here
    BufferPool bp = Database.getBufferPool();
    int id = getId(), i, slots;
    ArrayList<Page> retlist = new ArrayList<Page>();
    PageId pid = null;
    HeapPage p = null;
    for (i = 0; i < numPages(); i++) {
      pid = new HeapPageId(id, i);
      p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE);
      slots = p.getNumEmptySlots();
      if (slots > 0) {
        p.insertTuple(t);
        retlist.add(p);
        return retlist;
      }
    }

    // create new page and add tuple to it
    pid = new HeapPageId(id, i);
    raf.setLength(raf.length() + BufferPool.PAGE_SIZE);
    p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE);
    p.insertTuple(t);
    retlist.add(p);
    return retlist;
  }
Exemple #7
0
  /**
   * Copy the given byte range of the given input to the given output.
   *
   * @param input The input to copy the given range to the given output for.
   * @param output The output to copy the given range from the given input for.
   * @param start Start of the byte range.
   * @param length Length of the byte range.
   * @throws IOException If something fails at I/O level.
   */
  private static void copy(RandomAccessFile input, OutputStream output, long start, long length)
      throws IOException {
    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int read;

    if (input.length() == length) {
      // Write full range.
      while ((read = input.read(buffer)) > 0) {
        output.write(buffer, 0, read);
      }
    } else {
      // Write partial range.
      input.seek(start);
      long toRead = length;

      while ((read = input.read(buffer)) > 0) {
        if ((toRead -= read) > 0) {
          output.write(buffer, 0, read);
        } else {
          output.write(buffer, 0, (int) toRead + read);
          break;
        }
      }
    }
  }
 /**
  * reads the content of an existing file using the current domain
  *
  * @param domain The namespace used to identify the application domain (1st level directory) to
  *     use
  * @param path The path relative to the domain for the file
  * @param offset the offset from the beginning of the file.
  * @param len The length of the block in bytes
  * @return The contents of the file
  */
 public byte[] readByteFromFile(String path, long offset, int len)
     throws EOFException, FileAccessException {
   try {
     if (_isLocal) {
       File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path);
       if (tmpFile.isFile()) {
         RandomAccessFile raf = new RandomAccessFile(tmpFile, "r");
         byte[] buffer = new byte[len];
         raf.seek(offset);
         int totalByteRead = 0;
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         int result = 0;
         while (totalByteRead < len && raf.getFilePointer() < raf.length()) {
           result = raf.read(buffer, 0, (len - totalByteRead));
           if (result != -1) {
             out.write(buffer, 0, result);
             totalByteRead += result;
           } else if (totalByteRead == 0) throw new EOFException("End of file reached!");
           else break;
         }
         raf.close();
         out.flush();
         out.close();
         return out.toByteArray();
       } else throw new FileAccessException("Path is not a file");
     } else return _remote.readByteFromFile(_domain, path, offset, len);
   } catch (EOFException eofe) {
     throw eofe;
   } catch (FileAccessException fae) {
     throw fae;
   } catch (Exception e) {
     throw new FileAccessException(e);
   }
 }
Exemple #9
0
 public double getProgress() {
   try {
     return 1.0 * filein.getFilePointer() / (1.0 * filein.length());
   } catch (IOException e) {
     return 1.0;
   }
 }
 private void init(SharedFile sf, int size) throws IOException {
   this.sf = sf;
   this.in = sf.open();
   this.start = 0;
   this.datalen = in.length(); // XXX - file can't grow
   this.bufsize = size;
   buf = new byte[size];
 }
Exemple #11
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();
 }
 /** Returns the number of pages in this HeapFile. */
 public int numPages() {
   // some code goes here
   try {
     return (int) raf.length() / BufferPool.PAGE_SIZE;
   } catch (IOException e) {
     throw new RuntimeException("error accessing file length");
   }
 }
  public static void main(String args[]) throws IOException {
    if (args.length != 1) {
      System.err.println("Usage: java LockingExample <input file>");
      System.exit(0);
    }

    FileLock sharedLock = null;
    FileLock exclusiveLock = null;

    try {
      RandomAccessFile raf = new RandomAccessFile(args[0], "rw");

      // get the channel for the file
      FileChannel channel = raf.getChannel();

      System.out.println("trying to acquire lock ...");
      // this locks the first half of the file - exclusive
      exclusiveLock = channel.lock(0, raf.length() / 2, SHARED);
      System.out.println("lock acquired ...");

      /** Now modify the data . . . */
      try {
        // sleep for 10 seconds
        Thread.sleep(10000);
      } catch (InterruptedException ie) {
      }

      // release the lock
      exclusiveLock.release();
      System.out.println("lock released ...");

      // this locks the second half of the file - shared
      sharedLock = channel.lock(raf.length() / 2 + 1, raf.length(), SHARED);

      /** Now read the data . . . */

      // release the lock
      sharedLock.release();
    } catch (java.io.IOException ioe) {
      System.err.println(ioe);
    } finally {
      if (exclusiveLock != null) exclusiveLock.release();
      if (sharedLock != null) sharedLock.release();
    }
  }
Exemple #14
0
  public void read() throws Exception {
    file.seek(0);

    System.out.print("[ ");

    while (file.getFilePointer() < file.length()) System.out.print(file.readInt() + " ");

    System.out.println("]");
  }
  /**
   * Appends a string to the file and returns the offset of the start of the string.
   *
   * @param str the string to be written.
   * @return the offset of the start of the string that was written.
   * @throws IOException if an I/O error occurs or the offset would be larger than 2^32-1.
   */
  private long writeString(String str) throws IOException {
    // Write the string at the end of the file.
    long pos = stringFile.length();
    if (pos > MASK32) throw new IOException("String file too large");

    stringFile.seek(pos);
    stringFile.writeUTF(str);
    return pos;
  }
 public int luaCB_length(Lua l, LuaPlugin plugin) {
   try {
     l.pushNumber((double) object.length());
   } catch (IOException e) {
     l.pushNil();
     l.pushString("IOException: " + e.getMessage());
     return 2;
   }
   return 1;
 }
Exemple #17
0
 public static byte[] loadFileIntoByteArray(String path) throws IOException {
   RandomAccessFile f = new RandomAccessFile(path, "r");
   try {
     byte[] data = new byte[(int) f.length()];
     f.readFully(data);
     return data;
   } finally {
     f.close();
   }
 }
 /**
  * @param file the file to read the line numbers from
  * @return Number of lines in file
  * @throws IOException low level file error
  */
 public static int numberOfLines(File file) throws IOException {
   RandomAccessFile randFile = new RandomAccessFile(file, "r");
   long lastRec = randFile.length();
   randFile.close();
   FileReader fileRead = new FileReader(file);
   LineNumberReader lineRead = new LineNumberReader(fileRead);
   lineRead.skip(lastRec);
   int count = lineRead.getLineNumber() - 1;
   fileRead.close();
   lineRead.close();
   return count;
 }
  private static void replacePackageForJavaFile(File path, String oldPackage, String newPackage) {
    if (path != null && path.exists()) {
      // 处理文件夹
      if (path.isDirectory()) {
        if ("target".equals(path.getName())) {
          return;
        }
        String[] children = path.list();
        for (int i = 0; i < children.length; i++) {
          File child =
              new File(path.getPath() + System.getProperty("file.separator") + children[i]);
          // 递归处理
          replacePackageForJavaFile(child, oldPackage, newPackage);
        }
      } else {
        // 处理相关文件
        if (path.getName().toLowerCase().endsWith(".java")
            || path.getName().toLowerCase().endsWith(".jsp")
            || path.getName().toLowerCase().endsWith(".ftl")
            || path.getName().toLowerCase().endsWith(".properties")
            || path.getName().toLowerCase().endsWith(".xml")) {
          System.out.println(path.getAbsolutePath());
          fileCount++;
          try {
            byte[] content;
            try (RandomAccessFile f = new RandomAccessFile(path, "rw")) {
              content = new byte[(int) f.length()];
              f.readFully(content);
            }
            String text = new String(content, "utf-8");

            // 替换package
            text = text.replaceAll("package " + oldPackage, "package " + newPackage);
            // 替换import
            text = text.replaceAll("import " + oldPackage, "import " + newPackage);
            // 替换/分割的路径
            String _old = oldPackage.split("\\.")[0] + "/" + oldPackage.split("\\.")[1];
            String _new = newPackage.split("\\.")[0] + "/" + newPackage.split("\\.")[1];
            text = text.replaceAll(_old, _new);
            // 除了本文件,其他文件要全文替换,这个替换要在最后
            if (!path.getName().endsWith("ChangePackageName.java")) {
              text = text.replaceAll(oldPackage, newPackage);
            }
            try (Writer writer = new OutputStreamWriter(new FileOutputStream(path), "utf-8")) {
              writer.write(text);
            }
          } catch (Exception ex) {
            failCount++;
          }
        }
      }
    }
  }
  public static void main(String[] args) throws IOException {
    try ( // Create a random access file
    RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw"); ) {
      // Clear the file to destroy the old contents if exists
      inout.setLength(0);

      // Write new integers to the file
      for (int i = 0; i < 200; i++) inout.writeInt(i);

      // Display the current length of the file
      System.out.println("Current file length is " + inout.length());

      // Retrieve the first number
      inout.seek(0); // Move the file pointer to the beginning
      System.out.println("The first number is " + inout.readInt());

      // Retrieve the second number
      inout.seek(1 * 4); // Move the file pointer to the second number
      System.out.println("The second number is " + inout.readInt());

      // Retrieve the tenth number
      inout.seek(9 * 4); // Move the file pointer to the tenth number
      System.out.println("The tenth number is " + inout.readInt());

      // Modify the eleventh number
      inout.writeInt(555);

      // Append a new number
      inout.seek(inout.length()); // Move the file pointer to the end
      inout.writeInt(999);

      // Display the new length
      System.out.println("The new length is " + inout.length());

      // Retrieve the new eleventh number
      inout.seek(10 * 4); // Move the file pointer to the eleventh number
      System.out.println("The eleventh number is " + inout.readInt());
    }
  }
  public static long checksumRandomAccessFile(Path filename) throws IOException {
    try (RandomAccessFile file = new RandomAccessFile(filename.toFile(), "r")) {
      long length = file.length();
      CRC32 crc = new CRC32();

      for (long p = 0; p < length; p++) {
        file.seek(p);
        int c = file.readByte();
        crc.update(c);
      }
      return crc.getValue();
    }
  }
  public long length() {
    try {
      if (!exists()) {
        return 0;
      }

      RandomAccessFile raf = new RandomAccessFile(this, "r");
      long len = raf.length();
      raf.close();
      return len;
    } catch (IOException e) {
      return 0;
    }
  }
Exemple #23
0
 /** constructs a frequency table from a file */
 public static int[] constructTable(RandomAccessFile file) {
   int[] ret = new int[TABLESIZE];
   try {
     file.seek(0L);
     long l = file.length();
     for (long i = 0; i < l; i++) {
       int c = file.read();
       ret[c]++;
     }
     return ret;
   } catch (Exception ex) {
     ExHandler.handle(ex);
     return null;
   }
 }
Exemple #24
0
 /**
  * Start recording
  *
  * @param stub
  */
 public static void startRecorder(String stub) {
   try {
     stamp = new SimpleDateFormat("HH:mm:ss");
     SimpleDateFormat dateformat = new SimpleDateFormat("yyMMdd");
     Date d = new Date();
     String s = dateformat.format(d);
     String name = logdir + stub + s + ".txt";
     R = new RandomAccessFile(name, "rw");
     System.out.println("Log file: " + name);
     long N = R.length();
     R.seek(N);
     recording = true;
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  public SeatInfo() {
    File file = new File(".", "data");
    file.mkdir();
    File f = new File(file, "SeatInfo.txt");

    try {
      raf = new RandomAccessFile(f, "rw");

      if (raf.length() == 0) {
        raf.setLength(31 * 4 * FLIGHT_PER_DAY);
        for (int i = 0; i < 31 * FLIGHT_PER_DAY; i++) raf.writeInt(0);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Get the next rtp packet recorded in the rtpdump file.
   *
   * @param loopFile if true, when the end of the rtpdump file is reached, this
   *     <tt>RtpdumpFileReader</tt> will go back at the beginning of the file and get the first
   *     packet.
   * @return a <tt>RawPacket</tt> containing all the information and data of the next rtp packet
   *     recorded in the rtpdump file
   * @throws IOException if <tt>loopFile</tt> was false and the end of the file is reached.
   */
  public RawPacket getNextPacket(boolean loopFile) throws IOException {
    if (loopFile && (stream.getFilePointer() >= stream.length())) {
      resetFile();
    }

    byte[] rtpdumpPacket;
    int sizeInBytes;

    stream.readShort(); // read away an useless short (2 bytes)
    sizeInBytes = stream.readUnsignedShort();
    rtpdumpPacket = new byte[sizeInBytes];
    stream.readInt(); // read away the rtpdump timestamp

    stream.read(rtpdumpPacket);

    return new RawPacket(rtpdumpPacket, 0, rtpdumpPacket.length);
  }
Exemple #27
0
  private static byte[] readFile(String sFile) {
    byte[] emptybuf = null;

    try {
      RandomAccessFile file = new RandomAccessFile(new File(sFile), "r");
      long iLen = file.length();
      log(sFile + " length is " + iLen);
      byte[] buf = new byte[(int) iLen];
      // int[] buf = new int[(int)iLen];
      file.readFully(buf);
      file.close();
      return buf;
    } catch (IOException es) {
      log("File not found.");
    }

    return emptybuf;
  }
  @Override
  public int read() throws IOException {
    bitOffset = 0;

    if (streamPos >= raf.length()) {
      int b = is.read();

      if (b < 0) {
        return -1;
      }

      raf.seek(streamPos++);
      raf.write(b);
      return b;
    }

    raf.seek(streamPos++);
    return raf.read();
  }
Exemple #29
0
  /**
   * Create a new DBWriter according to the input file name.
   *
   * @param fileName the name of the file
   * @exception FileNotFoundException from library call
   * @exception IOException from library call or if file is corrupted
   */
  public DBWriter(String fileName) throws IOException {
    outStream = new RandomAccessFile(fileName, "rw");

    lastPosition = outStream.length();

    // case this is a new database
    if (lastPosition == 0) {
      wroteColumnNames = false;

      headerSize = 0;
      numRows = 0;
      CRC = 0;
      needReposition = false;
    }
    // case this might be an old database
    else if (lastPosition > MIN_DATA_OFFSET) {
      // check that we have a valid database here
      checkID();

      wroteColumnNames = true;

      // read header data
      outStream.seek(HEAD_SIZE_OFFSET);
      headerSize = outStream.readLong();
      numRows = outStream.readLong();
      numColumns = outStream.readLong();

      // read description
      outStream.seek(headerSize - DESCRIPTION_SIZE - ID_SIZE - CRC_SIZE);
      description = "";
      for (int i = 0; i < DESCRIPTION_LENGTH; i++) description += outStream.readChar();
      description = description.trim();

      // read CRC
      outStream.seek(headerSize - CRC_SIZE);
      CRC = outStream.readInt();

      // by default we append to an existing database
      needReposition = true;
    }
    // case this is not a database
    else throw new IOException("Attempting to load invalid database");
  }
    protected int read(byte[] buffer, long offset, int length) throws IOException {

      AESemaphore sem;

      synchronized (lock) {
        if (raf.length() > offset) {

          raf.seek(offset);

          return (raf.read(buffer, 0, length));
        }

        if (stream_details == null) {

          if (stream_got_eof) {

            return (-1);
          }

          throw (new IOException("Premature end of stream (read)"));
        }

        sem = new AESemaphore("DMS:block");

        waiters.add(sem);
      }

      try {
        sem.reserve(1000);

      } finally {

        synchronized (lock) {
          waiters.remove(sem);
        }
      }

      return (0);
    }