⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fatfs.java

📁 java 编写的一个FAT16读写源代码 支持长文件名和子目录
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                        {
                            return new String[0];
                        }

                        // Check each entry in the sector.
                        for(int i = 0; i < bytesPerSector; i += DIR_ENTRY_SIZE)
                        {
                            // Found the end of this directory.
                            if(sector[i + OFFSET_DIR_NAME] == DIR_LAST_ENTRY)
                            {
                                String[] names = new String[listVector.size()];
                                listVector.copyInto(names);
                                return names;
                            }

                            // If this is a valid file entry...
                            if((ArrayUtils.getByte(sector, i + OFFSET_DIR_NAME) != DIR_SLOT_DELETED) &&
                               (ArrayUtils.getByte(sector, i + OFFSET_DIR_ATTRIBUTES) != ATTR_LONG_NAME))
                            {
                                if(!com.dalsemi.system.ArrayUtils.arrayComp(sector, i + OFFSET_DIR_NAME, dotBytes, 0, 11) &&
                                   !com.dalsemi.system.ArrayUtils.arrayComp(sector, i + OFFSET_DIR_NAME, dotdotBytes, 0, 11))
                                {
//                                    String name = new String(sector, i + OFFSET_DIR_NAME, 11);
                                    String base = new String(sector, i + OFFSET_DIR_NAME, 8).trim();
                                    String ext = new String(sector, i + OFFSET_DIR_NAME + 8, 3).trim();
                                    if((ext == null) || (ext.length() == 0))
                                    {
                                        listVector.addElement(base);
                                    }
                                    else
                                    {
                                        listVector.addElement(base + "." + ext);
                                    }
                                }
                            }
                        }
                    }

                    try
                    {
                        // Keep going until we reach the end of the cluster chain.
                        dirSector = nextSector(dirSector, false, false);
                    }
                    catch(FATException ex)
                    {
                        return new String[0];
                    }
                } while(dirSector != 0);
            }

            String[] names = new String[listVector.size()];
            listVector.copyInto(names);
            return names;
        }

    }

    public boolean delete(String fileName, byte uid)
    {
        if(isDirectory(fileName) && length(fileName) != 0)
        {
            return false;
        }

        synchronized(lock)
        {
            try
            {
                if(exists(fileName))
                {
                    if(openWriters != null)
                    {
                        if(fileOpened(openWriters, currentFileSector, currentFileOffset, null) != null)
                        {
                            return false;
                        }
                    }

                    if(openReaders != null)
                    {
                        if(fileOpened(openReaders, currentFileSector, currentFileOffset, null) != null)
                        {
                            return false;
                        }
                    }

                    synchronized (sector)
                    {
                        sector[0] = (byte)DIR_SLOT_DELETED;
                        if(!disk.write((currentFileSector << shiftBytesPerSector) + currentFileOffset, sector, 0, 1))
                        {
                            return false;
                        }
                    }

                    int cluster = sectorToCluster(getFirstSector());
                    int next;
                    do
                    {
                        next = nextCluster(cluster, false, false);
                        writeFATEntry(cluster, 0);
                        cluster = next;
              	    } while(next != 0);

                    return true;
                }
                else
                {
                    return true;
                }
            }
            catch(FATException ex)
            {
                return false;
            }
        }
    }

    public void touch(String fileName, byte uid) throws IOException
    {
        return ; /*TODO*/
    }

    public void setUserPermissions(String fileName, int perms, byte uid) throws IOException
    {
        throw new FATException("FAT file system doesn't support permissions");
    }

    public void setOtherPermissions(String fileName, int perms, byte uid) throws IOException
    {
        throw new FATException("FAT file system doesn't support permissions");
    }

    public void setUser(String fileName, byte newUID, byte uid) throws IOException
    {
        throw new FATException("FAT file system doesn't support file ownership");
    }

    public int getUserPermissions(String fileName) throws FileNotFoundException
    {
        // FAT file system doesn't support permissions, return all flags set.
        return -1;
    }

    public int getOtherPermissions(String fileName) throws FileNotFoundException
    {
        // FAT file system doesn't support permissions, return all flags set.
        return -1;
    }

    public int getUser(String fileName) throws FileNotFoundException
    {
        return com.dalsemi.system.TINIOS.getCurrentUID();
    }

    public Object openWritingFD(String fileName, boolean append, byte uid) throws IOException
    {
        synchronized (lock)
        {
            if(exists(fileName, true))
            {
                int size;

                if(openWriters == null)
                {
                    openWriters = new Vector();
                }
                else if(fileOpened(openWriters, currentFileSector, currentFileOffset, null) != null)
                {
                    throw new IOException("File already opened");
                }

                synchronized (sector)
                {
                    if(!disk.read((currentFileSector << shiftBytesPerSector) + currentFileOffset, sector, 0, DIR_ENTRY_SIZE))
                    {
                        throw new IOException();
                    }

                    if((sector[OFFSET_DIR_ATTRIBUTES] & ATTR_READ_ONLY) == ATTR_READ_ONLY)
                    {
                        throw new IOException("File is read only");
                    }

                    size = ArrayUtils.getInt(sector, OFFSET_DIR_FILE_SIZE);
                }

                int firstSector = getFirstSector();
                FATFSDescriptor fd = new FATFSDescriptor(firstSector, size, currentFileSector, currentFileOffset);
                openWriters.addElement(fd);

                if(append)
                {
                    seek(fd, size);
                }
                else
                {
                    // TODO - set modified/access times
                    synchronized (sector)
                    {
                        ArrayUtils.putInt(0, sector, 0);
                        if(!disk.write((currentFileSector << shiftBytesPerSector) + currentFileOffset + OFFSET_DIR_FILE_SIZE, sector, 0, 4))
                        {
                            throw new IOException();
                        }
                    }

                    int cluster = sectorToCluster(firstSector);
                    int next;
                    int entry = -1;
                    do
                    {
                        next = nextCluster(cluster, false, false);
                        writeFATEntry(cluster, entry);
                        entry = 0;
                        cluster = next;
              	    } while(next != 0);

              	    fd.length = 0;

                    FATFSDescriptor desc = fileOpened(openWriters, currentFileSector, currentFileOffset, null);
                    while(desc != null)
                    {
                        desc.length = 0;
                        seek(desc, 0);
                        desc = fileOpened(openWriters, currentFileSector, currentFileOffset, desc);
                    }
                }
                return fd;
            }
        }

        throw new IOException();
    }

    public Object openReadingFD(String fileName, byte uid) throws FileNotFoundException
    {
//TODO - add to open files list
        synchronized (lock)
        {
            if(isFile(fileName))
            {
                int size;
                synchronized (sector)
                {
                    if(!disk.read((currentFileSector << shiftBytesPerSector) + currentFileOffset, sector, 0, DIR_ENTRY_SIZE))
                    {
                        throw new FileNotFoundException();
                    }
                    size = ArrayUtils.getInt(sector, OFFSET_DIR_FILE_SIZE);
                }
                return new FATFSDescriptor(getFirstSector(), size, currentFileSector, currentFileOffset);
            }
        }
        throw new FileNotFoundException();
    }

    public Object openRandomFD(String fileName, byte uid) throws IOException
    {

//TODO - check read only attribute
//TODO - check open for writing
//TODO - add to open files list
        synchronized (lock)
        {
            if(exists(fileName, true))
            {
                int size;
                synchronized (sector)
                {
                    if(!disk.read((currentFileSector << shiftBytesPerSector) + currentFileOffset, sector, 0, DIR_ENTRY_SIZE))
                    {
                        throw new IOException();
                    }
                    size = ArrayUtils.getInt(sector, OFFSET_DIR_FILE_SIZE);
                }

                return new FATFSDescriptor(getFirstSector(), size, currentFileSector, currentFileOffset);
            }
        }

        throw new IOException();
    }

    public void writeBytes(Object fd, byte[] data, int start, int length) throws IOException
    {
        FATFSDescriptor fDesc = (FATFSDescriptor)fd;

        int bytesWritten = 0;

        synchronized (lock)
        {
            while(length > 0)
            {
                int sectorPointer = fDesc.filePointer & (bytesPerSector - 1);
                if(sectorPointer == 0 && fDesc.filePointer != 0)
                {
                    //todo check return
                    fDesc.currentSector = nextSector(fDesc.currentSector, true, false);
                }

                int bytesToWrite = bytesPerSector - sectorPointer;
                if(bytesToWrite > length)
                {
                    bytesToWrite = length;
                }

                disk.write((fDesc.currentSector << shiftBytesPerSector) + sectorPointer, data, start + bytesWritten, bytesToWrite);
                length -= bytesToWrite;
                bytesWritten += bytesToWrite;
                fDesc.filePointer += bytesToWrite;
                fDesc.length += bytesToWrite;
                //todo update open file descriptors
            }
        }
        return;
    }

    public int readBytes(Object fd, byte[] data, int start, int length) throws IOException
    {
//TODO - set accessed time stamp
        FATFSDescriptor fDesc = (FATFSDescriptor)fd;

        synchronized (lock)
        {
            if(fDesc.filePointer >= fDesc.length)
            {
                return -1;
            }

            if((fDesc.length - fDesc.filePointer) < length)
            {
                length = fDesc.length - fDesc.filePointer;
            }

            int bytesRead = 0;
            while (fDesc.filePointer < fDesc.length && length > 0)
            {
                int sectorPointer = fDesc.filePointer & (bytesPerSector - 1);
                if(sectorPointer == 0 && fDesc.filePointer != 0)
                {
                    //todo check return
                    fDesc.currentSector = nextSector(fDesc.currentSector, false, false);
                }

                int bytesToRead = bytesPerSector - sectorPointer;
                if(bytesToRead > length)
                {
                    bytesToRead = length;
                }

                disk.read(fDesc.currentSector << shiftBytesPerSector + sectorPointer, data, start + bytesRead, bytesToRead);
                length -= bytesToRead;
                bytesRead += bytesToRead;
                fDesc.filePointer += bytesToRead;
            }
            return bytesRead;
        }
    }

    public void seek(Object fd, long n) throws IOException
    {
        FATFSDescriptor fDesc = (FATFSDescriptor)fd;

        synchronized (lock)
        {
            fDesc.filePointer = (int)n;

            int hi;
            int lo;

            synchronized(sector)
            {
                disk.read((fDesc.fileSector << shiftBytesPerSector) + fDesc.fileOffset, sector, 0, DIR_ENTRY_SIZE);
                hi = ArrayUtils.getShort(sector, OFFSET_DIR_FIRST_CLUSTER_HIGH);
                lo = ArrayUtils.getShort(sector, OFFSET_DIR_FIRST_CLUSTER_LOW);
            }

            fDesc.currentSector = clusterToSector((hi << 16) | lo);
            int numSectors = (int)n >> shiftBytesPerSector;

            while(numSectors > 0)
            {
                fDesc.currentSector = nextSector(fDesc.currentSector, true, false);
                numSectors--;
            }

            if(fDesc.length < (int)n)
            {
    //TODO - Update any open readers with the new size
    //TODO - update access/modifed times
                fDesc.length = (int)n;
            }
        }
    }

    public long skipBytes(Object fd, long n) throws IOException
    {
        if(n < 0)
        {
            n = 0;
        }

        FATFSDescriptor fDesc = (FATFSDescriptor)fd;
        int newPos = (int)n;

        synchronized (lock)
        {
            if(fDesc.filePointer + newPos > fDesc.length)
            {
                newPos = fDesc.length - fDesc.filePointer;
                seek(fd, fDesc.length);
                return (long)newPos;
            }
            else
            {
                seek(fd, fDesc.length + newPos);
                return n;
            }
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -