KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > store > FileStore


1
2 package com.jofti.store;
3
4 import java.io.File JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.RandomAccessFile JavaDoc;
8 import java.nio.channels.FileChannel JavaDoc;
9 import java.nio.channels.FileLock JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import com.jofti.exception.JoftiException;
15
16
17 class FileStore
18 {
19   File JavaDoc file = null;
20   
21   /**
22    * @see java.io.RandomAccessFile#RandomAccessFile(java.lang.String, java.lang.String)
23    */

24   String JavaDoc fileMode = "rw";
25   
26   /**
27    * FileChannel associated with this FileStore.
28    *
29    * <p>The FileChannel is private to guarantee that all calls to the
30    * channel methods come through this FileStore.
31    */

32   FileChannel JavaDoc channel = null;
33   
34
35   protected int fileId =0;
36   
37   
38   private static Log log = LogFactory.getLog(FileManager.class);
39   
40   
41   /**
42    * FileChannel.position() of last read or write.
43    *
44    * <p>May be used to report the file position when IOException occurs.
45    */

46   long nextPosition = 0;
47   
48   /**
49    * indicates the file was created during the call to open()
50    * @see #open(String filemode)
51    */

52   boolean newFile = true;
53   
54   /**
55    * FileLock acquired when file is opened.
56    */

57   
58   FileLock JavaDoc lock = null;
59   
60   Object JavaDoc recordLock = new Object JavaDoc();
61   
62
63   long writes =0;
64
65
66   
67   /**
68    * construct an instance of FileStore for a given file name
69    * @param file filename
70    */

71   FileStore(File JavaDoc file, int id)
72   {
73     this.file = file;
74     this.fileId =id;
75   }
76   
77   
78  
79     
80   /**
81    * open the file and get the associated nio FileChannel for the file.
82    *
83    * <p>
84    * If the file does not exist, then the newFile member is set true.
85    *
86    * @param fileMode
87    * value passed to RandomAccessFile constructor.
88    * @throws FileNotFoundException
89    * if the parent directory structure does not exist.
90    * @see java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
91    * java.lang.String)
92    */

93   FileStore open(String JavaDoc fileMode) throws JoftiException, FileNotFoundException JavaDoc
94   {
95     this.fileMode = fileMode;
96
97     // remember whether the file existed or not
98
newFile = !file.exists();
99     
100     // if it already existed, but length is zero, then it is still a new file
101
if (!newFile) newFile = file.length() == 0;
102     
103     channel = new RandomAccessFile JavaDoc(file, fileMode).getChannel();
104    
105     try {
106       lock = channel.tryLock();
107     } catch (IOException JavaDoc e) {
108       throw new JoftiException(e);
109     }
110     if (lock == null)
111      log.info("Unable to obtain lock on " + file.getAbsolutePath());
112     
113     return this;
114   }
115   
116   /**
117    * Close the channel associated with this FileStore.
118    * <p>Also releases the lock that is held on the file.
119    * @return this FileStore
120    * @throws IOException
121    */

122   FileStore close() throws IOException JavaDoc
123   {
124     if (channel.isOpen())
125     {
126       if (lock != null)
127       {
128         lock.release();
129       }
130       channel.close();
131     }
132     return this;
133   }
134   
135   /**
136    * Helper provides access to the FileChannel.write() method for
137    * the FileChannel associated with this FileStore.
138    * @param lb Reference to a LogBuffer object that is to be written.
139    * @throws IOException
140    */

141   void write(BlockBuffer lb) throws IOException JavaDoc
142   {
143     
144     channel.write(lb.buffer,lb.positionHolder.position);
145     
146     // force
147
if (writes %10000 == 0){
148         force(true);
149     }
150     writes++;
151   }
152   
153   /**
154    *
155    * @param forceMetadata as defined by FileChannel.force()
156    * @throws IOException
157    * @see FileChannel#force(boolean)
158    */

159   void force(boolean forceMetadata) throws IOException JavaDoc
160   {
161     channel.force(forceMetadata);
162   }
163   
164   
165 }
166
Popular Tags