Code example for FileChannel

Methods: write

0
	public void write(Block block, ByteBuffer bt_buffer) throws FatwormException {
		try { 
			if(block.fileExist()) {
				bt_buffer.rewind();
				FileChannel fc = getFile(block.getFileName());
				fc.write(bt_buffer, block.getBlockNumber() * BLOCK_SIZE);
			} 
	    } 
		catch (Exception e) {
			System.out.println("In IOMgr, " + e);
		} 
	} 
 
	public Block append(String filename, ByteBuffer contents) throws FatwormException {
		try { 
			int new_blk_num = fileBlockNum(filename);
			Block block = new Block(filename, new_blk_num, conn);
			write(block, contents);
			return block;
		} catch (Exception e) {
			System.out.println("In IOMgr, " + e);
			return null; 
		} 
	} 
	 
	FileChannel getFile(String filename) throws FileNotFoundException {
		FileChannel fc = open_files.get(filename);
		if (fc == null) {
			File table_file = new File(db_dir, filename);
			RandomAccessFile raf = new RandomAccessFile(table_file, "rws");
			fc = raf.getChannel();
			open_files.put(filename, fc);
		} 
		return fc;
	} 
	 
	public int fileBlockNum(String file_name) {