The Artima Developer Community
Sponsored Link

Java Answers Forum
FAT filestore implementation in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

FAT filestore implementation in java Posted: Oct 10, 2004 5:55 AM
Reply to this message Reply
Advertisement
Hi, I'm currently in the process of attempting to implement a FAT (file allocation table) filestore in java. The first part of this is to return a simple directory listing of the files on the filestore with the file lengths in bytes. The way I think it will work is to create a 2-d array and then use a for-loop to run through the array, at 2-byte intervals (note that as it is 2-bytes they will be signed.., but I have pseudo-code to produce it, so it is not a problem). To return the length of the file, perhaps create an instance of StringBuffer and then use the length() method to return the string length? Any help would be appreciated. The method being called is the dir() method. I am now posting what I was given (and what I've done so far).

Any help would be much appreciated as there might be an easier way to implement this filestore - the first bit affects the rest of the program, which is simply taking an input via BufferedReader, and then reading/deleting files, which I'm sure I am capable of doing.

MINIDISKFILESTORE.JAVA

import java.io.*;
package minidisk;


public class MiniDiskFileStore
{
byte[][] store;







public MiniDiskFileStore(String fname, boolean newOne) throws MiniDiskException;
{

//looping through the array currently at 1 byte intervals,
// not 2 byte as will be the case.


MiniDiskDevice mdDevice = MiniDiskDevice.openDevice(fname, newOne);

for(int i =0; i < 128; i++)
{

}//end for

} // constructor
/*
public void unload() throws MiniDiskException;
{
}//unload

public InputStream openRead(String filename) throws MiniDiskException;
{
}//InputStream

public OutputStream openWrite(String filename) throws MiniDiskException;
{
}//OutputStream


public void delete(String filename) throws MiniDiskException;
{
}//delete

*/
public String dir() throws MiniDiskException;
{
}//dir

} //class MiniDiskFileStore

MINIDISKDEVICE.JAVA (GIVEN AND NOT TO BE MODIFIED)


package minidisk ;

import java.io.* ;

// This class simulates a "MiniDisk" - storing information in a file
// which will be about 1MB. It will only be useable by other classes
// in the same package - which will implement the file system.

public class MiniDiskDevice implements Serializable {

static final int BLOCKSIZE = 128 ;
static final int BLOCKCOUNT = 8 * 1024 + 128 ;
private byte [][] store ;
transient File assocFile ;

// constructor - for use within this class only!

private MiniDiskDevice(File f) throws MiniDiskException {
assocFile = f ;
if (f.exists())
throw new MiniDiskException("Can't create new " + f.getName()
+ " - it already exists!") ;
store = new byte [BLOCKCOUNT][BLOCKSIZE] ;
try {
assocFile.createNewFile() ;
} catch (Exception e) {
throw new MiniDiskException("Can't create " + f.getName()
+ " as a minidisk!") ;
}
}


// The method which MiniDiskFileStore uses to get an instance of a minidisk
// fname is the name of the file in which the information is/will be stored
// newOne is true if the file is being created (and therefore should NOT exist now)

static MiniDiskDevice openDevice(String fname, boolean newOne) throws MiniDiskException {
File f = new File(fname) ;
if (newOne) return new MiniDiskDevice(f) ;
MiniDiskDevice result = null ;
if (f.exists()) // if fname is a file which exists, deserialize from it
try {
ObjectInputStream infile = new ObjectInputStream(
new FileInputStream(f)) ;
result = (MiniDiskDevice) infile.readObject() ;
result.assocFile = f ;
} catch (Exception e) {
throw new MiniDiskException("Can't read " + fname
+ " as a minidisk!") ;
}
else throw new MiniDiskException("File does not exist: " + fname) ;
return result ;
}


// Method to "read" a block from the minidisk.
// bno is the block number to be read.
// the result is an array of bytes containing what is in that block

byte [] readBlock (int bno) throws MiniDiskException {
byte [] block = new byte [BLOCKSIZE] ;
if (bno < 0 || bno >= BLOCKCOUNT) throw new MiniDiskException("Bad block no " + bno) ;
System.arraycopy(store[bno], 0 , block, 0, BLOCKSIZE) ;
return block ;
}

// Method to "write" a block to the minidisk.
// bno is the block number to be written.
// block is the information to write to it.

void writeBlock(int bno, byte [] block) throws MiniDiskException {
if (bno < 0 || bno >= BLOCKCOUNT) throw new MiniDiskException("Bad block no " + bno) ;
System.arraycopy(block, 0, store[bno], 0, BLOCKSIZE) ;
}

// Method to update the real file version of the minidisk.
// MiniDiskFileSytem must close this as its final action or all
// changes to the minidisk contents since it was opened will be lost.

void close() throws MiniDiskException {
try {
ObjectOutputStream outfile = new ObjectOutputStream(
new FileOutputStream(assocFile)) ;
outfile.writeObject(this) ;
} catch (Exception e) {
throw new MiniDiskException("Can't write back minidisk to " + assocFile) ;
}
}

}



MINIDISKEXCEPTION.JAVA - SIMPLY CALLS THE SUPERCLASS


package minidisk ;

// This exception is thrown when any operation on the
// simulated minidisk fails.

public class MiniDiskException extends Exception {

public MiniDiskException(String s) {
super(s) ;
}
}

Topic: Problem! Previous Topic   Next Topic Topic: To Senthoorkumaran

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use