Source Code Cross Referenced for TempDirectoryManager.java in  » Source-Control » sourcejammer » org » sourcejammer » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Source Control » sourcejammer » org.sourcejammer.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright (C) 2001, 2002 Robert MacGrogan
003:         *
004:         *  This library is free software; you can redistribute it and/or
005:         *  modify it under the terms of the GNU Lesser General Public
006:         *  License as published by the Free Software Foundation; either
007:         *  version 2.1 of the License, or (at your option) any later version.
008:         *
009:         *  This library is distributed in the hope that it will be useful,
010:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012:         *  Lesser General Public License for more details.
013:         *
014:         *  You should have received a copy of the GNU Lesser General Public
015:         *  License along with this library; if not, write to the Free Software
016:         *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:         *
018:         *
019:         * $Archive: SourceJammer$
020:         * $FileName: TempDirectoryManager.java$
021:         * $FileID: 4319$
022:         *
023:         * Last change:
024:         * $AuthorName: Rob MacGrogan$
025:         * $Date: 4/23/03 5:23 PM$
026:         * $Comment: Replaced GPL header with LGPL header.$
027:         *
028:         * $KeyWordsOff: $
029:         */
030:
031:        package org.sourcejammer.util;
032:
033:        /**
034:         * Title:        SourceJammer 1.2
035:         * Description:
036:         * Copyright:    Copyright (c) 2002
037:         * Company:      SourceJammer Project
038:         * @author Robert MacGrogan
039:         * @version 1.0
040:         */
041:
042:        import java.io.File;
043:        import java.io.*;
044:        import org.sourcejammer.util.AppConfig;
045:        import org.sourcejammer.util.Counter;
046:        import org.sourcejammer.util.FileUtil;
047:
048:        public class TempDirectoryManager {
049:
050:            private static final String TEMP_DIRECTORY_NAME = "_temp";
051:            private static final String COUNTER_FILE_NAME = "namer";
052:            private static File tempDir = new File(AppConfig.getInstance()
053:                    .getConfigFilePath(), TEMP_DIRECTORY_NAME);
054:
055:            private TempDirectoryManager() {
056:            }
057:
058:            /**
059:             * Remove all files in temp directory other than namer.
060:             */
061:            public static void clearAllTempFiles() {
062:                if (tempDir.exists()) {
063:                    File[] tempFiles = tempDir.listFiles();
064:                    for (int i = 0; i < tempFiles.length; i++) {
065:                        if (!tempFiles[i].getName().equals(COUNTER_FILE_NAME)) {
066:                            tempFiles[i].delete();
067:                        }
068:                    }//end for
069:                }
070:            }
071:
072:            /**
073:             * Remove all files in temp directory (except namer) older than specified
074:             * Date.
075:             */
076:            public static void removeTempFiles(java.util.Date olderThanDate) {
077:                if (tempDir.exists()) {
078:                    FileFilter filter = new FileDateFilter(olderThanDate,
079:                            FileDateFilter.FILTER_LESS_THAN_EQUAL);
080:                    File removeFiles[] = tempDir.listFiles(filter);
081:                    for (int i = 0; i < removeFiles.length; i++) {
082:                        if (!removeFiles[i].getName().equals(COUNTER_FILE_NAME)) {
083:                            removeFiles[i].delete();
084:                        }
085:                    }//end for
086:                }
087:            }
088:
089:            /**
090:             * Creates the temp directory if it does not exist.
091:             */
092:            private static synchronized void makeTempDirIfNeeded()
093:                    throws IOException {
094:                if (!tempDir.exists()) {
095:                    tempDir.mkdir();
096:                    Counter namer = new Counter();
097:                    File flNamer = new File(tempDir, COUNTER_FILE_NAME);
098:                    FileUtil.writeObjectToFileSys(namer, flNamer);
099:                }
100:            }
101:
102:            /**
103:             * Gets the next unique ID from the Counter object.
104:             */
105:            public static synchronized long getNextID() throws IOException {
106:                if (!tempDir.exists()) {
107:                    makeTempDirIfNeeded();
108:                }
109:                File flCounter = new File(tempDir, COUNTER_FILE_NAME);
110:                if (!flCounter.exists()) {
111:                    makeCounterIfNeeded(flCounter);
112:                }
113:                Counter c = (Counter) FileUtil.readObjectFromFileSys(flCounter);
114:                long lId = c.getNextValue();
115:                FileUtil.writeObjectToFileSys(c, flCounter);
116:                return lId;
117:            }
118:
119:            public static File getNewTempFile() throws IOException {
120:                long nextID = getNextID();
121:                File nextFile = getFileById(nextID);
122:                return nextFile;
123:            }
124:
125:            public static byte[] getTempFileBytesById(long id)
126:                    throws IOException {
127:                File fl = getTempFileById(id);
128:                byte[] file = FileUtil.readBytesFromFileSys(fl);
129:                return file;
130:            }
131:
132:            public static long makeNewTempFile(byte[] by) throws IOException {
133:                long nextID = getNextID();
134:                File flTemp = getFileById(nextID);
135:                FileUtil.writeBytesToFileSys(by, flTemp);
136:                return nextID;
137:            }
138:
139:            public static long streamFromFileToTemp(File fl) throws IOException {
140:                if (!fl.exists()) {
141:                    throw new IOException("The file does not exist.");
142:                }
143:                if (fl.isDirectory()) {
144:                    throw new IOException("The file is a directory.");
145:                }
146:
147:                long nextID = getNextID();
148:                File flTemp = getFileById(nextID);
149:
150:                FileUtil.streamFileToFile(fl, flTemp);
151:                return nextID;
152:            }
153:
154:            public static void streamFromTempToFile(long tempFileID, File fl)
155:                    throws IOException {
156:                if (fl.isDirectory()) {
157:                    throw new IOException("The file is a directory.");
158:                }
159:                File flTemp = getTempFileById(tempFileID);
160:                if (!flTemp.exists()) {
161:                    throw new IOException("Temp file does not exist.");
162:                }
163:                FileUtil.streamFileToFile(flTemp, fl);
164:            }
165:
166:            public static File getTempFileById(long id) throws IOException {
167:                File fl = getFileById(id);
168:                if (!fl.exists()) {
169:                    throw new IOException(
170:                            "The specified temp file does not exist.");
171:                }
172:                return fl;
173:            }
174:
175:            public static File getNewTempFile(long id) throws IOException {
176:                File fl = getFileById(id);
177:                return fl;
178:            }
179:
180:            public static void deleteTempFile(long id) throws IOException {
181:                File fl = getFileById(id);
182:                fl.delete();
183:            }
184:
185:            private static File getFileById(long id) {
186:                File fl = new File(tempDir, Long.toString(id));
187:                return fl;
188:            }
189:
190:            /**
191:             * Creates and stores the Counter object if it does not already exist
192:             * in the temp directory.
193:             */
194:            private static synchronized void makeCounterIfNeeded(File flCounter)
195:                    throws IOException {
196:                if (!flCounter.exists()) {
197:                    Counter c = new Counter();
198:                    FileUtil.writeObjectToFileSys(c, flCounter);
199:                }
200:            }
201:
202:            public static void main(String[] args) {
203:                try {
204:                    String sSource = args[0];
205:                    String sTarget = args[1];
206:                    long lStart = new java.util.Date().getTime();
207:                    byte[] by = FileUtil.readBytesFromFileSys(sSource);
208:                    long lFinish = new java.util.Date().getTime();
209:                    long lTime = lFinish - lStart;
210:                    System.out.println("Read bytes took " + lTime + " ms.");
211:
212:                    lStart = new java.util.Date().getTime();
213:                    FileUtil.writeBytesToFileSys(by, sTarget);
214:                    lFinish = new java.util.Date().getTime();
215:                    lTime = lFinish - lStart;
216:                    System.out.println("Write bytes took " + lTime + " ms.");
217:
218:                } catch (Throwable thr) {
219:                    thr.printStackTrace();
220:                }
221:            }
222:
223:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.