KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > filesys > FileInfo


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 package org.alfresco.filesys.server.filesys;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Date JavaDoc;
21
22 import org.alfresco.filesys.smb.SMBDate;
23
24 /**
25  * File information class.
26  * <p>
27  * The FileInfo class is returned by the DiskInterface.getFileInformation () and
28  * SearchContext.nextFileInfo() methods.
29  *
30  * @see DiskInterface
31  * @see SearchContext
32  */

33 public class FileInfo implements Serializable JavaDoc
34 {
35     private static final long serialVersionUID = 5710753560656277110L;
36
37     // Constants
38
//
39
// Set file information flags
40

41     public static final int SetFileSize = 0x0001;
42     public static final int SetAllocationSize = 0x0002;
43     public static final int SetAttributes = 0x0004;
44     public static final int SetModifyDate = 0x0008;
45     public static final int SetCreationDate = 0x0010;
46     public static final int SetAccessDate = 0x0020;
47     public static final int SetChangeDate = 0x0040;
48     public static final int SetGid = 0x0080;
49     public static final int SetUid = 0x0100;
50     public static final int SetMode = 0x0200;
51     public static final int SetDeleteOnClose = 0x0400;
52
53     // File name string
54

55     protected String JavaDoc m_name;
56
57     // 8.3 format file name
58

59     protected String JavaDoc m_shortName;
60
61     // Path string
62

63     protected String JavaDoc m_path;
64
65     // File size, in bytes
66

67     protected long m_size;
68
69     // File attributes bits
70

71     protected int m_attr = -1;
72
73     // File modification date/time
74

75     private long m_modifyDate;
76
77     // Creation date/time
78

79     private long m_createDate;
80
81     // Last access date/time (if available)
82

83     private long m_accessDate;
84
85     // Change date/time (for Un*x inode changes)
86

87     private long m_changeDate;
88
89     // Filesystem allocation size
90

91     private long m_allocSize;
92
93     // File identifier and parent directory id
94

95     private int m_fileId = -1;
96     private int m_dirId = -1;
97
98     // User/group id
99

100     private int m_gid = -1;
101     private int m_uid = -1;
102
103     // Unix mode
104

105     private int m_mode = -1;
106
107     // Delete file on close
108

109     private boolean m_deleteOnClose;
110
111     // Set file information flags
112
//
113
// Used to indicate which values in the file information object are valid and should be used to
114
// set
115
// the file information.
116

117     private int m_setFlags;
118
119     /**
120      * Default constructor
121      */

122     public FileInfo()
123     {
124     }
125
126     /**
127      * Construct an SMB file information object.
128      *
129      * @param fname File name string.
130      * @param fsize File size, in bytes.
131      * @param fattr File attributes.
132      */

133     public FileInfo(String JavaDoc fname, long fsize, int fattr)
134     {
135         m_name = fname;
136         m_size = fsize;
137         m_attr = fattr;
138
139         setAllocationSize(0);
140     }
141
142     /**
143      * Construct an SMB file information object.
144      *
145      * @param fname File name string.
146      * @param fsize File size, in bytes.
147      * @param fattr File attributes.
148      * @param ftime File time, in seconds since 1-Jan-1970 00:00:00
149      */

150     public FileInfo(String JavaDoc fname, long fsize, int fattr, int ftime)
151     {
152         m_name = fname;
153         m_size = fsize;
154         m_attr = fattr;
155         m_modifyDate = new SMBDate(ftime).getTime();
156
157         setAllocationSize(0);
158     }
159
160     /**
161      * Construct an SMB file information object.
162      *
163      * @param fname File name string.
164      * @param fsize File size, in bytes.
165      * @param fattr File attributes.
166      * @param fdate SMB encoded file date.
167      * @param ftime SMB encoded file time.
168      */

169     public FileInfo(String JavaDoc fname, long fsize, int fattr, int fdate, int ftime)
170     {
171         m_name = fname;
172         m_size = fsize;
173         m_attr = fattr;
174
175         if (fdate != 0 && ftime != 0)
176             m_modifyDate = new SMBDate(fdate, ftime).getTime();
177
178         setAllocationSize(0);
179     }
180
181     /**
182      * Construct an SMB file information object.
183      *
184      * @param fpath File path string.
185      * @param fname File name string.
186      * @param fsize File size, in bytes.
187      * @param fattr File attributes.
188      */

189     public FileInfo(String JavaDoc fpath, String JavaDoc fname, long fsize, int fattr)
190     {
191         m_path = fpath;
192         m_name = fname;
193         m_size = fsize;
194         m_attr = fattr;
195
196         setAllocationSize(0);
197     }
198
199     /**
200      * Construct an SMB file information object.
201      *
202      * @param fpath File path string.
203      * @param fname File name string.
204      * @param fsize File size, in bytes.
205      * @param fattr File attributes.
206      * @param ftime File time, in seconds since 1-Jan-1970 00:00:00
207      */

208     public FileInfo(String JavaDoc fpath, String JavaDoc fname, long fsize, int fattr, int ftime)
209     {
210         m_path = fpath;
211         m_name = fname;
212         m_size = fsize;
213         m_attr = fattr;
214         m_modifyDate = new SMBDate(ftime).getTime();
215
216         setAllocationSize(0);
217     }
218
219     /**
220      * Construct an SMB file information object.
221      *
222      * @param fpath File path string.
223      * @param fname File name string.
224      * @param fsize File size, in bytes.
225      * @param fattr File attributes.
226      * @param fdate SMB encoded file date.
227      * @param ftime SMB encoded file time.
228      */

229     public FileInfo(String JavaDoc fpath, String JavaDoc fname, long fsize, int fattr, int fdate, int ftime)
230     {
231         m_path = fpath;
232         m_name = fname;
233         m_size = fsize;
234         m_attr = fattr;
235         m_modifyDate = new SMBDate(fdate, ftime).getTime();
236
237         setAllocationSize(0);
238     }
239
240     /**
241      * Return the files last access date/time.
242      *
243      * @return long
244      */

245     public long getAccessDateTime()
246     {
247         return m_accessDate;
248     }
249
250     /**
251      * Get the files allocated size.
252      *
253      * @return long
254      */

255     public long getAllocationSize()
256     {
257         return m_allocSize;
258     }
259
260     /**
261      * Get the files allocated size, as a 32bit value
262      *
263      * @return int
264      */

265     public int getAllocationSizeInt()
266     {
267         return (int) (m_allocSize & 0x0FFFFFFFFL);
268     }
269
270     /**
271      * Return the inode change date/time of the file.
272      *
273      * @return long
274      */

275     public long getChangeDateTime()
276     {
277         return m_changeDate;
278     }
279
280     /**
281      * Return the creation date/time of the file.
282      *
283      * @return long
284      */

285     public long getCreationDateTime()
286     {
287         return m_createDate;
288     }
289
290     /**
291      * Return the delete on close flag setting
292      *
293      * @return boolean
294      */

295     public final boolean hasDeleteOnClose()
296     {
297         return m_deleteOnClose;
298     }
299
300     /**
301      * Return the file attributes value.
302      *
303      * @return File attributes value.
304      */

305     public int getFileAttributes()
306     {
307         return m_attr;
308     }
309
310     /**
311      * Get the file name string
312      *
313      * @return File name string.
314      */

315     public final String JavaDoc getFileName()
316     {
317         return m_name;
318     }
319
320     /**
321      * Check if the short (8.3) file name is available
322      *
323      * @return boolean
324      */

325     public final boolean hasShortName()
326     {
327         return m_shortName != null ? true : false;
328     }
329
330     /**
331      * Get the short file name (8.3 format)
332      *
333      * @return String
334      */

335     public final String JavaDoc getShortName()
336     {
337         return m_shortName;
338     }
339
340     /**
341      * Get the files date/time of last write
342      *
343      * @return long
344      */

345     public final long getModifyDateTime()
346     {
347         return m_modifyDate;
348     }
349
350     /**
351      * Get the file path string.
352      *
353      * @return File path string, relative to the share.
354      */

355     public final String JavaDoc getPath()
356     {
357         return m_path;
358     }
359
360     /**
361      * Get the file size, in bytes.
362      *
363      * @return File size in bytes.
364      */

365     public final long getSize()
366     {
367         return m_size;
368     }
369
370     /**
371      * Get the file size in bytes, as a 32bit value
372      *
373      * @return File size in bytes, as an int
374      */

375     public final int getSizeInt()
376     {
377         return (int) (m_size & 0x0FFFFFFFFL);
378     }
379
380     /**
381      * Get the file identifier
382      *
383      * @return int
384      */

385     public final int getFileId()
386     {
387         return m_fileId;
388     }
389
390     /**
391      * Get the file identifier
392      *
393      * @return long
394      */

395     public final long getFileIdLong()
396     {
397         return ((long) m_fileId) & 0xFFFFFFFFL;
398     }
399
400     /**
401      * Get the parent directory identifier
402      *
403      * @return int
404      */

405     public final int getDirectoryId()
406     {
407         return m_dirId;
408     }
409
410     /**
411      * Get the parent directory identifier
412      *
413      * @return long
414      */

415     public final long getDirectoryIdLong()
416     {
417         return ((long) m_dirId) & 0xFFFFFFFFL;
418     }
419
420     /**
421      * Determine if the last access date/time is available.
422      *
423      * @return boolean
424      */

425     public boolean hasAccessDateTime()
426     {
427         return m_accessDate == 0L ? false : true;
428     }
429
430     /**
431      * Determine if the inode change date/time details are available.
432      *
433      * @return boolean
434      */

435     public boolean hasChangeDateTime()
436     {
437         return m_changeDate == 0L ? false : true;
438     }
439
440     /**
441      * Determine if the creation date/time details are available.
442      *
443      * @return boolean
444      */

445     public boolean hasCreationDateTime()
446     {
447         return m_createDate == 0L ? false : true;
448     }
449
450     /**
451      * Determine if the modify date/time details are available.
452      *
453      * @return boolean
454      */

455     public boolean hasModifyDateTime()
456     {
457         return m_modifyDate == 0L ? false : true;
458     }
459
460     /**
461      * Determine if the file attributes field has been set
462      *
463      * @return boolean
464      */

465     public final boolean hasFileAttributes()
466     {
467         return m_attr != -1 ? true : false;
468     }
469
470     /**
471      * Return the specified attribute status
472      *
473      * @param attr int
474      */

475     public final boolean hasAttribute(int attr)
476     {
477         return (m_attr & attr) != 0 ? true : false;
478     }
479
480     /**
481      * Return the directory file attribute status.
482      *
483      * @return true if the file is a directory, else false.
484      */

485     public final boolean isDirectory()
486     {
487         return (m_attr & FileAttribute.Directory) != 0 ? true : false;
488     }
489
490     /**
491      * Return the hidden file attribute status.
492      *
493      * @return true if the file is hidden, else false.
494      */

495     public final boolean isHidden()
496     {
497         return (m_attr & FileAttribute.Hidden) != 0 ? true : false;
498     }
499
500     /**
501      * Return the read-only file attribute status.
502      *
503      * @return true if the file is read-only, else false.
504      */

505     public final boolean isReadOnly()
506     {
507         return (m_attr & FileAttribute.ReadOnly) != 0 ? true : false;
508     }
509
510     /**
511      * Return the system file attribute status.
512      *
513      * @return true if the file is a system file, else false.
514      */

515     public final boolean isSystem()
516     {
517         return (m_attr & FileAttribute.System) != 0 ? true : false;
518     }
519
520     /**
521      * Return the archived attribute status
522      *
523      * @return boolean
524      */

525     public final boolean isArchived()
526     {
527         return (m_attr & FileAttribute.Archive) != 0 ? true : false;
528     }
529
530     /**
531      * Determine if the group id field has been set
532      *
533      * @return boolean
534      */

535     public final boolean hasGid()
536     {
537         return m_gid != -1 ? true : false;
538     }
539
540     /**
541      * Return the owner group id
542      *
543      * @return int
544      */

545     public final int getGid()
546     {
547         return m_gid;
548     }
549
550     /**
551      * Determine if the user id field has been set
552      *
553      * @return boolean
554      */

555     public final boolean hasUid()
556     {
557         return m_uid != -1 ? true : false;
558     }
559
560     /**
561      * Return the owner user id
562      *
563      * @return int
564      */

565     public final int getUid()
566     {
567         return m_uid;
568     }
569
570     /**
571      * Determine if the mode field has been set
572      *
573      * @return boolean
574      */

575     public final boolean hasMode()
576     {
577         return m_mode != -1 ? true : false;
578     }
579
580     /**
581      * Return the Unix mode
582      *
583      * @return int
584      */

585     public final int getMode()
586     {
587         return m_mode;
588     }
589
590     /**
591      * Reset all values to zero/null values.
592      */

593     public final void resetInfo()
594     {
595         m_name = "";
596         m_path = null;
597
598         m_size = 0L;
599         m_allocSize = 0L;
600
601         m_attr = 0;
602
603         m_accessDate = 0L;
604         m_createDate = 0L;
605         m_modifyDate = 0L;
606         m_changeDate = 0L;
607
608         m_fileId = -1;
609         m_dirId = -1;
610
611         m_gid = -1;
612         m_uid = -1;
613         m_mode = -1;
614     }
615
616     /**
617      * Copy the file information
618      *
619      * @param finfo FileInfo
620      */

621     public final void copyFrom(FileInfo finfo)
622     {
623         m_name = finfo.getFileName();
624         m_path = finfo.getPath();
625
626         m_size = finfo.getSize();
627         m_allocSize = finfo.getAllocationSize();
628
629         m_attr = finfo.getFileAttributes();
630
631         m_accessDate = finfo.getAccessDateTime();
632         m_createDate = finfo.getCreationDateTime();
633         m_modifyDate = finfo.getModifyDateTime();
634         m_changeDate = finfo.getChangeDateTime();
635
636         m_fileId = finfo.getFileId();
637         m_dirId = finfo.getDirectoryId();
638
639         m_gid = finfo.getGid();
640         m_uid = finfo.getUid();
641         m_mode = finfo.getMode();
642     }
643
644     /**
645      * Set the files last access date/time.
646      *
647      * @param timesec long
648      */

649     public void setAccessDateTime(long timesec)
650     {
651
652         // Create the access date/time
653

654         m_accessDate = timesec;
655     }
656
657     /**
658      * Set the files allocation size.
659      *
660      * @param siz long
661      */

662     public void setAllocationSize(long siz)
663     {
664         m_allocSize = siz;
665     }
666
667     /**
668      * Set the inode change date/time for the file.
669      *
670      * @param timesec long
671      */

672     public void setChangeDateTime(long timesec)
673     {
674
675         // Set the inode change date/time
676

677         m_changeDate = timesec;
678     }
679
680     /**
681      * Set the creation date/time for the file.
682      *
683      * @param timesec long
684      */

685     public void setCreationDateTime(long timesec)
686     {
687
688         // Set the creation date/time
689

690         m_createDate = timesec;
691     }
692
693     /**
694      * Set/clear the delete on close flag
695      *
696      * @param del boolean
697      */

698     public final void setDeleteOnClose(boolean del)
699     {
700         m_deleteOnClose = del;
701     }
702
703     /**
704      * Set the file attributes.
705      *
706      * @param attr int
707      */

708     public final void setFileAttributes(int attr)
709     {
710         m_attr = attr;
711     }
712
713     /**
714      * Set the file name.
715      *
716      * @param name java.lang.String
717      */

718     public final void setFileName(String JavaDoc name)
719     {
720         m_name = name;
721     }
722
723     /**
724      * Set the file size in bytes
725      *
726      * @param siz long
727      */

728     public final void setFileSize(long siz)
729     {
730         m_size = siz;
731     }
732
733     /**
734      * Set the modification date/time for the file.
735      *
736      * @param timesec long
737      */

738     public void setModifyDateTime(long timesec)
739     {
740
741         // Set the date/time
742

743         m_modifyDate = timesec;
744     }
745
746     /**
747      * Set the file identifier
748      *
749      * @param id int
750      */

751     public final void setFileId(int id)
752     {
753         m_fileId = id;
754     }
755
756     /**
757      * Set the parent directory id
758      *
759      * @param id int
760      */

761     public final void setDirectoryId(int id)
762     {
763         m_dirId = id;
764     }
765
766     /**
767      * Set the short (8.3 format) file name
768      *
769      * @param name String
770      */

771     public final void setShortName(String JavaDoc name)
772     {
773         m_shortName = name;
774     }
775
776     /**
777      * Set the path
778      *
779      * @param path String
780      */

781     public final void setPath(String JavaDoc path)
782     {
783         m_path = path;
784     }
785
786     /**
787      * Set the file size.
788      *
789      * @param siz int
790      */

791     public final void setSize(int siz)
792     {
793         m_size = siz;
794     }
795
796     /**
797      * Set the file size.
798      *
799      * @param siz long
800      */

801     public final void setSize(long siz)
802     {
803         m_size = siz;
804     }
805
806     /**
807      * Set the owner group id
808      *
809      * @param id int
810      */

811     public final void setGid(int id)
812     {
813         m_gid = id;
814     }
815
816     /**
817      * Set the owner user id
818      *
819      * @param id int
820      */

821     public final void setUid(int id)
822     {
823         m_uid = id;
824     }
825
826     /**
827      * Set the file mode
828      *
829      * @param mode int
830      */

831     public final void setMode(int mode)
832     {
833         m_mode = mode;
834     }
835
836     /**
837      * Set the set file information flags to indicated which values are to be set
838      *
839      * @param setFlags int
840      */

841     public final void setFileInformationFlags(int setFlags)
842     {
843         m_setFlags = setFlags;
844     }
845
846     /**
847      * Determine if the specified set file information flags is enabled
848      *
849      * @param setFlag int
850      * @return boolean
851      */

852     public final boolean hasSetFlag(int flag)
853     {
854         if ((m_setFlags & flag) != 0)
855             return true;
856         return false;
857     }
858
859     /**
860      * Return the set file information flags
861      *
862      * @return int
863      */

864     public final int getSetFileInformationFlags()
865     {
866         return m_setFlags;
867     }
868
869     /**
870      * Return the file information as a string.
871      *
872      * @return File information string.
873      */

874     public String JavaDoc toString()
875     {
876         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
877
878         // Append the path, and terminate with a trailing '\'
879

880         if (m_path != null)
881         {
882             str.append(m_path);
883             if (!m_path.endsWith("\\"))
884                 str.append("\\");
885         }
886
887         // Append the file name
888

889         str.append(m_name);
890
891         // Space fill
892

893         while (str.length() < 15)
894             str.append(" ");
895
896         // Append the attribute states
897

898         if (isReadOnly())
899             str.append("R");
900         else
901             str.append("-");
902         if (isHidden())
903             str.append("H");
904         else
905             str.append("-");
906         if (isSystem())
907             str.append("S");
908         else
909             str.append("-");
910         if (isDirectory())
911             str.append("D");
912         else
913             str.append("-");
914
915         // Append the file size, in bytes
916

917         str.append(" ");
918         str.append(m_size);
919
920         // Space fill
921

922         while (str.length() < 30)
923             str.append(" ");
924
925         // Append the file write date/time, if available
926

927         if (m_modifyDate != 0L)
928         {
929             str.append(" - ");
930             str.append(new Date JavaDoc(m_modifyDate));
931         }
932
933         // Append the short (8.3) file name, if available
934

935         if (hasShortName())
936         {
937             str.append(" (");
938             str.append(getShortName());
939             str.append(")");
940         }
941
942         // Return the file information string
943

944         return str.toString();
945     }
946 }
Popular Tags