示例#1
0
  /**
   * Sets the length.
   *
   * @param length The length to set
   */
  public synchronized void setLength(long length) throws IOException {

    if (getFileSystem().isReadOnly()) {
      throw new ReadOnlyFileSystemException("setLength in readonly filesystem");
    }

    if (this.length == length) {
      // Do nothing
      return;
    }

    final FatFileSystem fs = getFatFileSystem();
    final Fat fat = fs.getFat();
    final int nrClusters = (int) ((length + clusterSize - 1) / clusterSize);

    if (this.length == 0) {
      final long[] chain = fat.allocNew(nrClusters);
      this.startCluster = chain[0];
      this.myEntry.setStartCluster((int) startCluster);
    } else {
      final long[] chain = fs.getFat().getChain(startCluster);

      if (nrClusters != chain.length) {
        if (nrClusters > chain.length) {
          // Grow
          int count = nrClusters - chain.length;
          while (count > 0) {
            fat.allocAppend(getStartCluster());
            count--;
          }
        } else {
          // Shrink
          fat.setEof(chain[nrClusters - 1]);
          for (int i = nrClusters; i < chain.length; i++) {
            fat.setFree(chain[i]);
          }
        }
      }
    }

    this.length = length;
    this.myEntry.updateLength(length);
  }