KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21
22 import org.alfresco.filesys.server.filesys.AccessDeniedException;
23 import org.alfresco.filesys.server.filesys.FileInfo;
24 import org.alfresco.filesys.server.filesys.NetworkFile;
25 import org.alfresco.filesys.smb.SeekType;
26
27 /**
28  * In Memory Network File Class
29  *
30  * <p>In memory network file implementation that uses a memory buffer for the file data.
31  *
32  * @author gkspencer
33  */

34 public class MemoryNetworkFile extends NetworkFile
35 {
36     // Current file position
37

38     private long m_filePos;
39
40     // File data
41

42     private byte[] m_data;
43     
44     // End of file flag
45

46     private boolean m_eof;
47
48     /**
49      * Class constructor.
50      *
51      * @param name String
52      * @param localPath String
53      * @param finfo FileInfo
54      */

55     public MemoryNetworkFile(String JavaDoc name, byte[] data, FileInfo finfo)
56     {
57         super( name);
58
59         // Set the file data
60

61         m_data = data;
62         if ( m_data == null)
63             m_data = new byte[0];
64         
65         // Set the file size
66

67         setFileSize( m_data.length);
68         m_eof = false;
69
70         // Set the creation and modification date/times
71

72         setModifyDate( finfo.getModifyDateTime());
73         setCreationDate( finfo.getCreationDateTime());
74
75         // Set the file id and relative path
76

77         if ( finfo.getPath() != null)
78         {
79             setFileId( finfo.getPath().hashCode());
80             setFullName( finfo.getPath());
81         }
82     }
83
84     /**
85      * Close the network file.
86      */

87     public void closeFile() throws java.io.IOException JavaDoc
88     {
89         // Nothing to do
90
}
91
92     /**
93      * Return the current file position.
94      *
95      * @return long
96      */

97     public long currentPosition()
98     {
99         return m_filePos;
100     }
101
102     /**
103      * Flush the file.
104      *
105      * @exception IOException
106      */

107     public void flushFile() throws IOException JavaDoc
108     {
109         // Nothing to do
110
}
111
112     /**
113      * Determine if the end of file has been reached.
114      *
115      * @return boolean
116      */

117     public boolean isEndOfFile() throws java.io.IOException JavaDoc
118     {
119         // Check if we reached end of file
120

121         if ( m_filePos == m_data.length)
122             return true;
123         return false;
124     }
125
126     /**
127      * Open the file.
128      *
129      * @param createFlag boolean
130      * @exception IOException
131      */

132     public void openFile(boolean createFlag) throws java.io.IOException JavaDoc
133     {
134         // Indicate that the file is open
135

136         setClosed(false);
137     }
138
139     /**
140      * Read from the file.
141      *
142      * @param buf byte[]
143      * @param len int
144      * @param pos int
145      * @param fileOff long
146      * @return Length of data read.
147      * @exception IOException
148      */

149     public int readFile(byte[] buf, int len, int pos, long fileOff) throws java.io.IOException JavaDoc
150     {
151         // Check if the read is within the file data range
152

153         long fileLen = (long) m_data.length;
154         
155         if ( fileOff >= fileLen)
156             return 0;
157         
158         // Calculate the actual read length
159

160         if (( fileOff + len) > fileLen)
161             len = (int) ( fileLen - fileOff);
162
163         // Copy the data to the user buffer
164

165         System.arraycopy( m_data, (int) fileOff, buf, pos, len);
166
167         // Update the current file position
168

169         m_filePos = fileOff + len;
170         
171         // Return the actual length of data read
172

173         return len;
174     }
175
176     /**
177      * Seek to the specified file position.
178      *
179      * @param pos long
180      * @param typ int
181      * @return long
182      * @exception IOException
183      */

184     public long seekFile(long pos, int typ) throws IOException JavaDoc
185     {
186         // Seek to the required file position
187

188         switch (typ)
189         {
190         // From start of file
191

192         case SeekType.StartOfFile:
193             if (currentPosition() != pos)
194                 m_filePos = pos;
195             break;
196
197         // From current position
198

199         case SeekType.CurrentPos:
200             m_filePos += pos;
201             break;
202
203         // From end of file
204

205         case SeekType.EndOfFile:
206             m_filePos += pos;
207             if ( m_filePos < 0)
208                 m_filePos = 0L;
209             break;
210         }
211
212         // Return the new file position
213

214         return currentPosition();
215     }
216
217     /**
218      * Truncate the file
219      *
220      * @param siz long
221      * @exception IOException
222      */

223     public void truncateFile(long siz) throws IOException JavaDoc
224     {
225         // Do not allow the file to be written to
226

227         throw new AccessDeniedException("Cannot truncate pseudo file");
228     }
229
230     /**
231      * Write a block of data to the file.
232      *
233      * @param buf byte[]
234      * @param len int
235      * @exception IOException
236      */

237     public void writeFile(byte[] buf, int len, int pos) throws java.io.IOException JavaDoc
238     {
239         // Do not allow the file to be written to
240

241         throw new AccessDeniedException("Cannot write to pseudo file");
242     }
243
244     /**
245      * Write a block of data to the file.
246      *
247      * @param buf byte[]
248      * @param len int
249      * @param pos int
250      * @param offset long
251      * @exception IOException
252      */

253     public void writeFile(byte[] buf, int len, int pos, long offset) throws java.io.IOException JavaDoc
254     {
255         // Do not allow the file to be written to
256

257         throw new AccessDeniedException("Cannot write to pseudo file");
258     }
259 }
260
Popular Tags