Android Open Source - libaums Fat File






From Project

Back to project page libaums.

License

The source code is released under:

Apache License

If you think the Android project libaums listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * (C) Copyright 2014 mjahnen <jahnen@in.tum.de>
 */*from ww w . j  a  v  a2s  . c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.github.mjdev.libaums.fs.fat32;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.github.mjdev.libaums.driver.BlockDeviceDriver;
import com.github.mjdev.libaums.fs.UsbFile;

public class FatFile implements UsbFile {

  private BlockDeviceDriver blockDevice;
  private FAT fat;
  private Fat32BootSector bootSector;

  private FatDirectory parent;
  private ClusterChain chain;
  private FatLfnDirectoryEntry entry;

  /**
   * Constructs a new file with the given information.
   * 
   * @param blockDevice
   *            The device where the file system is located.
   * @param fat
   *            The FAT used to follow cluster chains.
   * @param bootSector
   *            The boot sector of the file system.
   * @param entry
   *            The corresponding entry in a FAT directory.
   * @param parent
   *            The parent directory of the newly constructed file.
   */
  private FatFile(BlockDeviceDriver blockDevice, FAT fat, Fat32BootSector bootSector,
      FatLfnDirectoryEntry entry, FatDirectory parent) {
    this.blockDevice = blockDevice;
    this.fat = fat;
    this.bootSector = bootSector;
    this.entry = entry;
    this.parent = parent;
  }

  /**
   * Creates a new file with the given information.
   * 
   * @param entry
   *            The corresponding entry in a FAT directory.
   * @param blockDevice
   *            The device where the file system is located.
   * @param fat
   *            The FAT used to follow cluster chains.
   * @param bootSector
   *            The boot sector of the file system.
   * @param parent
   *            The parent directory of the newly created file.
   * @return The newly constructed file.
   * @throws IOException
   *             If reading from device fails.
   */
  public static FatFile create(FatLfnDirectoryEntry entry, BlockDeviceDriver blockDevice,
      FAT fat, Fat32BootSector bootSector, FatDirectory parent) throws IOException {
    FatFile result = new FatFile(blockDevice, fat, bootSector, entry, parent);
    return result;
  }

  /**
   * Initializes the cluster chain to access the contents of the file.
   * 
   * @throws IOException
   *             If reading from FAT fails.
   */
  private void initChain() throws IOException {
    if (chain == null) {
      chain = new ClusterChain(entry.getStartCluster(), blockDevice, fat, bootSector);
    }
  }

  @Override
  public boolean isDirectory() {
    return false;
  }

  @Override
  public String getName() {
    return entry.getName();
  }

  @Override
  public void setName(String newName) throws IOException {
    parent.renameEntry(entry, newName);
  }

  @Override
  public UsbFile getParent() {
    return parent;
  }

  @Override
  public String[] list() {
    throw new UnsupportedOperationException("This is a file!");
  }

  @Override
  public UsbFile[] listFiles() throws IOException {
    throw new UnsupportedOperationException("This is a file!");
  }

  @Override
  public long getLength() {
    return entry.getFileSize();
  }

  @Override
  public void setLength(long newLength) throws IOException {
    chain.setLength(newLength);
    entry.setFileSize(newLength);
  }

  @Override
  public void read(long offset, ByteBuffer destination) throws IOException {
    initChain();
    entry.setLastAccessedTimeToNow();
    chain.read(offset, destination);
  }

  @Override
  public void write(long offset, ByteBuffer source) throws IOException {
    initChain();
    long length = offset + source.remaining();
    if (length > getLength())
      setLength(length);
    entry.setLastModifiedTimeToNow();
    chain.write(offset, source);
  }

  @Override
  public void flush() throws IOException {
    // we only have to update the parent because we are always writing
    // everything
    // immediately to the device
    // the parent directory is responsible for updating the
    // FatDirectoryEntry which
    // contains things like the file size and the date time fields
    parent.write();
  }

  @Override
  public void close() throws IOException {
    flush();
  }

  @Override
  public UsbFile createDirectory(String name) throws IOException {
    throw new UnsupportedOperationException("This is a file!");
  }

  @Override
  public UsbFile createFile(String name) throws IOException {
    throw new UnsupportedOperationException("This is a file!");
  }

  @Override
  public void moveTo(UsbFile destination) throws IOException {
    parent.move(entry, destination);
    parent = (FatDirectory) destination;
  }

  @Override
  public void delete() throws IOException {
    initChain();
    parent.removeEntry(entry);
    parent.write();
    chain.setLength(0);
  }

}




Java Source Code List

com.github.mjdev.libaums.UsbCommunication.java
com.github.mjdev.libaums.UsbMassStorageDevice.java
com.github.mjdev.libaums.driver.BlockDeviceDriverFactory.java
com.github.mjdev.libaums.driver.BlockDeviceDriver.java
com.github.mjdev.libaums.driver.scsi.ScsiBlockDevice.java
com.github.mjdev.libaums.driver.scsi.commands.CommandBlockWrapper.java
com.github.mjdev.libaums.driver.scsi.commands.CommandStatusWrapper.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiInquiryResponse.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiInquiry.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiRead10.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiReadCapacityResponse.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiReadCapacity.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiRequestSense.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiTestUnitReady.java
com.github.mjdev.libaums.driver.scsi.commands.ScsiWrite10.java
com.github.mjdev.libaums.fs.FileSystemFactory.java
com.github.mjdev.libaums.fs.FileSystem.java
com.github.mjdev.libaums.fs.UsbFile.java
com.github.mjdev.libaums.fs.fat32.ClusterChain.java
com.github.mjdev.libaums.fs.fat32.FAT.java
com.github.mjdev.libaums.fs.fat32.Fat32BootSector.java
com.github.mjdev.libaums.fs.fat32.Fat32FileSystem.java
com.github.mjdev.libaums.fs.fat32.FatDirectoryEntry.java
com.github.mjdev.libaums.fs.fat32.FatDirectory.java
com.github.mjdev.libaums.fs.fat32.FatFile.java
com.github.mjdev.libaums.fs.fat32.FatLfnDirectoryEntry.java
com.github.mjdev.libaums.fs.fat32.FsInfoStructure.java
com.github.mjdev.libaums.fs.fat32.ShortNameGenerator.java
com.github.mjdev.libaums.fs.fat32.ShortName.java
com.github.mjdev.libaums.partition.PartitionTableEntry.java
com.github.mjdev.libaums.partition.PartitionTableFactory.java
com.github.mjdev.libaums.partition.PartitionTable.java
com.github.mjdev.libaums.partition.Partition.java
com.github.mjdev.libaums.partition.mbr.MasterBootRecord.java
com.github.mjdev.libaums.usbfileman.MainActivity.java
com.github.mjdev.libaums.usbfileman.MoveClipboard.java
com.github.mjdev.libaums.usbfileman.UsbFileListAdapter.java