KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > repo > pseudo > PseudoNetworkFile


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17
18 package org.alfresco.filesys.smb.server.repo.pseudo;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.RandomAccessFile JavaDoc;
23
24 import org.alfresco.filesys.server.filesys.AccessDeniedException;
25 import org.alfresco.filesys.server.filesys.NetworkFile;
26 import org.alfresco.filesys.smb.SeekType;
27
28 /**
29  * Pseudo File Network File Class
30  * <p>
31  * Represents an open pseudo file and provides access to the file data.
32  *
33  * @author gkspencer
34  */

35 public class PseudoNetworkFile extends NetworkFile
36 {
37     // File details
38

39     protected File JavaDoc m_file;
40
41     // Random access file used to read/write the actual file
42

43     protected RandomAccessFile JavaDoc m_io;
44
45     // End of file flag
46

47     protected boolean m_eof;
48
49     /**
50      * Class constructor.
51      *
52      * @param name String
53      * @param localPath String
54      * @param netPath String
55      */

56     public PseudoNetworkFile(String JavaDoc name, String JavaDoc localPath, String JavaDoc netPath)
57     {
58         super( name);
59
60         // Set the file using the existing file object
61

62         m_file = new File JavaDoc( localPath);
63
64         // Set the file size
65

66         setFileSize(m_file.length());
67         m_eof = false;
68
69         // Set the modification date/time, if available. Fake the creation date/time as it's not
70
// available from the File class
71

72         long modDate = m_file.lastModified();
73         setModifyDate(modDate);
74         setCreationDate(modDate);
75
76         // Set the file id
77

78         setFileId(netPath.hashCode());
79         
80         // Set the full relative path
81

82         setFullName( netPath);
83     }
84
85     /**
86      * Close the network file.
87      */

88     public void closeFile() throws java.io.IOException JavaDoc
89     {
90
91         // Close the file, if used
92

93         if (m_io != null)
94         {
95
96             // Close the file
97

98             m_io.close();
99             m_io = null;
100
101             // Set the last modified date/time for the file
102

103             if (this.getWriteCount() > 0)
104                 m_file.setLastModified(System.currentTimeMillis());
105
106             // Indicate that the file is closed
107

108             setClosed(true);
109         }
110     }
111
112     /**
113      * Return the current file position.
114      *
115      * @return long
116      */

117     public long currentPosition()
118     {
119
120         // Check if the file is open
121

122         try
123         {
124             if (m_io != null)
125                 return m_io.getFilePointer();
126         }
127         catch (Exception JavaDoc ex)
128         {
129         }
130         return 0;
131     }
132
133     /**
134      * Flush the file.
135      *
136      * @exception IOException
137      */

138     public void flushFile() throws IOException JavaDoc
139     {
140         // Flush all buffered data
141

142         if (m_io != null)
143             m_io.getFD().sync();
144     }
145
146     /**
147      * Determine if the end of file has been reached.
148      *
149      * @return boolean
150      */

151     public boolean isEndOfFile() throws java.io.IOException JavaDoc
152     {
153         // Check if we reached end of file
154

155         if (m_io != null && m_io.getFilePointer() == m_io.length())
156             return true;
157         return false;
158     }
159
160     /**
161      * Open the file.
162      *
163      * @param createFlag boolean
164      * @exception IOException
165      */

166     public void openFile(boolean createFlag) throws java.io.IOException JavaDoc
167     {
168
169         synchronized (m_file)
170         {
171             // Check if the file is open
172

173             if (m_io == null)
174             {
175
176                 // Open the file, always read-only for now
177

178                 m_io = new RandomAccessFile JavaDoc(m_file, "r");
179
180                 // Indicate that the file is open
181

182                 setClosed(false);
183             }
184         }
185     }
186
187     /**
188      * Read from the file.
189      *
190      * @param buf byte[]
191      * @param len int
192      * @param pos int
193      * @param fileOff long
194      * @return Length of data read.
195      * @exception IOException
196      */

197     public int readFile(byte[] buf, int len, int pos, long fileOff) throws java.io.IOException JavaDoc
198     {
199
200         // Open the file, if not already open
201

202         if (m_io == null)
203             openFile(false);
204
205         // Seek to the required file position
206

207         if (currentPosition() != fileOff)
208             seekFile(fileOff, SeekType.StartOfFile);
209
210         // Read from the file
211

212         int rdlen = m_io.read(buf, pos, len);
213
214         // Return the actual length of data read
215

216         return rdlen;
217     }
218
219     /**
220      * Seek to the specified file position.
221      *
222      * @param pos long
223      * @param typ int
224      * @return long
225      * @exception IOException
226      */

227     public long seekFile(long pos, int typ) throws IOException JavaDoc
228     {
229
230         // Open the file, if not already open
231

232         if (m_io == null)
233             openFile(false);
234
235         // Check if the current file position is the required file position
236

237         switch (typ)
238         {
239
240         // From start of file
241

242         case SeekType.StartOfFile:
243             if (currentPosition() != pos)
244                 m_io.seek(pos);
245             break;
246
247         // From current position
248

249         case SeekType.CurrentPos:
250             m_io.seek(currentPosition() + pos);
251             break;
252
253         // From end of file
254

255         case SeekType.EndOfFile: {
256             long newPos = m_io.length() + pos;
257             m_io.seek(newPos);
258         }
259             break;
260         }
261
262         // Return the new file position
263

264         return currentPosition();
265     }
266
267     /**
268      * Truncate the file
269      *
270      * @param siz long
271      * @exception IOException
272      */

273     public void truncateFile(long siz) throws IOException JavaDoc
274     {
275         // Do not allow the file to be written to
276

277         throw new AccessDeniedException("Cannot truncate pseudo file");
278     }
279
280     /**
281      * Write a block of data to the file.
282      *
283      * @param buf byte[]
284      * @param len int
285      * @exception IOException
286      */

287     public void writeFile(byte[] buf, int len, int pos) throws java.io.IOException JavaDoc
288     {
289         // Do not allow the file to be written to
290

291         throw new AccessDeniedException("Cannot write to pseudo file");
292     }
293
294     /**
295      * Write a block of data to the file.
296      *
297      * @param buf byte[]
298      * @param len int
299      * @param pos int
300      * @param offset long
301      * @exception IOException
302      */

303     public void writeFile(byte[] buf, int len, int pos, long offset) throws java.io.IOException JavaDoc
304     {
305         // Do not allow the file to be written to
306

307         throw new AccessDeniedException("Cannot write to pseudo file");
308     }
309 }
310
Popular Tags