Source Code Cross Referenced for FileSysUtil.java in  » Source-Control » sourcejammer » org » sourcejammer » client » filesys » 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.client.filesys 
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: FileSysUtil.java$
021:         * $FileID: 3961$
022:         *
023:         * Last change:
024:         * $AuthorName: Rob MacGrogan$
025:         * $Date: 8/10/03 1:42 AM$
026:         * $Comment: New SourceJammerClient property names.$
027:         */
028:
029:        package org.sourcejammer.client.filesys;
030:
031:        import java.io.*;
032:        import java.awt.*;
033:
034:        import org.sourcejammer.client.gui.CommandCentral;
035:        import javax.swing.JOptionPane;
036:        import org.sourcejammer.util.ConfigurationException;
037:        import org.sourcejammer.util.RepeatingResponse;
038:        import org.sourcejammer.client.DisplayTextLibrary;
039:        import org.sourcejammer.client.SourceJammerClient;
040:        import org.sourcejammer.util.FileUtil;
041:
042:        /**
043:         * Title:  $FileName: FileSysUtil.java$
044:         * @author $AuthorName: Rob MacGrogan$
045:         * @version $VerNum: 4$<br><br>
046:         * 
047:         * $Description: $.
048:         * $KeyWordsOff: $<br><br>
049:         * 
050:         * 
051:         * Client file system utilities.
052:         */
053:        public class FileSysUtil {
054:
055:            private static final int YES = 0;
056:            private static final int YES_ALL = 1;
057:            private static final int NO = 2;
058:            private static final int NO_ALL = 3;
059:
060:            private FileSysUtil() {
061:            }
062:
063:            /**
064:             * Returns the file at the specified location as an array of bytes.
065:             */
066:            public static byte[] readLocalFile(String sFullPathAndName)
067:                    throws IOException {
068:
069:                java.io.File fileRead = new java.io.File(sFullPathAndName);
070:                return readLocalFile(fileRead);
071:
072:            }
073:
074:            /**
075:             * Returns the file at the specified location as an array of bytes.
076:             */
077:            public static byte[] readLocalFile(File fileRead)
078:                    throws IOException {
079:
080:                FileInputStream oFileIn = new FileInputStream(fileRead);
081:                ByteArrayOutputStream oByOut = new ByteArrayOutputStream();
082:                byte[] byFile = null;
083:                try {
084:                    boolean bKeepReadingBytes = true;
085:                    while (bKeepReadingBytes) {
086:                        int iByte = oFileIn.read();
087:                        if (iByte == -1) {
088:                            bKeepReadingBytes = false;
089:                        } else {
090:                            oByOut.write(iByte);
091:                        }
092:                    }
093:                    byFile = oByOut.toByteArray();
094:                } finally {
095:                    oByOut.close();
096:                    oFileIn.close();
097:                }
098:                return byFile;
099:
100:            }
101:
102:            /**
103:             * Write the byte array file to the specified directory using the specified
104:             * name.
105:             *
106:             * @return true if file is successfully written.
107:             */
108:            public static boolean writeLocalFile(
109:                    java.io.File flTargetDirectory, String sName, byte[] file)
110:                    throws IOException {
111:                return writeLocalFile(flTargetDirectory, sName, file, true,
112:                        new RepeatingResponse());
113:            }
114:
115:            public static boolean streamFileToFile(java.io.File flSource,
116:                    java.io.File flTarget, boolean setToReadOnly)
117:                    throws IOException {
118:                return streamFileToFile(flSource, flTarget, setToReadOnly,
119:                        new RepeatingResponse());
120:            }
121:
122:            public static boolean streamFileToFile(java.io.File flSource,
123:                    java.io.File flTarget, boolean setToReadOnly,
124:                    RepeatingResponse repeating) throws IOException {
125:
126:                boolean promptIfWritable = !repeating.isDisablePrompt();
127:                boolean bStreamCompleted = false;
128:                boolean bProcedeWithWrite = true;
129:                //Prompt if file exists.
130:                if (flTarget.exists()) {
131:                    if (flTarget.canWrite() && promptIfWritable) {
132:                        int iResponse = -1;
133:                        if (repeating.hasResponse()) {
134:                            iResponse = repeating.getIntResponse();
135:                        } else {
136:                            iResponse = overwriteDialog(flTarget.getName(),
137:                                    repeating.getParentComponent());
138:                        }
139:
140:                        bProcedeWithWrite = interpretOverwriteResponse(
141:                                iResponse, repeating);
142:                    }
143:                }
144:
145:                if (bProcedeWithWrite) {
146:
147:                    //make any required directories. Maybe prompt for this some day. . .
148:                    java.io.File parent = flTarget.getParentFile();
149:                    if (!parent.exists()) {
150:                        parent.mkdirs();
151:                    }
152:
153:                    if (flTarget.exists() && !flTarget.canWrite()) {
154:                        makeFileWritable(flTarget);
155:                    }
156:                    FileUtil.streamFileToFile(flSource, flTarget);
157:                    if (setToReadOnly) {
158:                        flTarget.setReadOnly();
159:                    }
160:                    bStreamCompleted = true;
161:                }
162:                return bStreamCompleted;
163:            }
164:
165:            /**
166:             * Write the byte array file to the specified directory using the specified
167:             * name.
168:             *
169:             * @param flTargetDirectory -- the directory to write the file to.
170:             * @param sName -- the name to give the file.
171:             * @param file -- the byte array that is the file's contents.
172:             * @param setToReadOnly -- if true, sets the file to readonly after writing it.
173:             *
174:             * @return true if file is successfully written.
175:             */
176:            public static boolean writeLocalFile(
177:                    java.io.File flTargetDirectory, String sName, byte[] file,
178:                    boolean setToReadOnly) throws IOException {
179:
180:                return writeLocalFile(flTargetDirectory, sName, file,
181:                        setToReadOnly, new RepeatingResponse());
182:            }
183:
184:            private static boolean interpretOverwriteResponse(int iResponse,
185:                    RepeatingResponse repeating) {
186:
187:                boolean procede = false;
188:
189:                switch (iResponse) {
190:                case YES_ALL:
191:                    repeating.setRepeatingResponse(YES);
192:                    procede = true;
193:                    break;
194:                case YES:
195:                    procede = true;
196:                    break;
197:                case NO_ALL:
198:                    repeating.setRepeatingResponse(NO);
199:                    procede = false;
200:                    break;
201:                case NO:
202:                    procede = false;
203:                    break;
204:                }//end switch
205:
206:                return procede;
207:
208:            }
209:
210:            /**
211:             * Write the byte array file to the specified directory using the specified
212:             * name.
213:             *
214:             * @param flTargetDirectory -- the directory to write the file to.
215:             * @param sName -- the name to give the file.
216:             * @param file -- the byte array that is the file's contents.
217:             * @param setToReadOnly -- if true, sets the file to readonly after writing it.
218:             * @param repeating -- To hold "all" response from dialog, if applicable.
219:             *
220:             * @return true if file is successfully written.
221:             */
222:            public static boolean writeLocalFile(
223:                    java.io.File flTargetDirectory, String sName, byte[] file,
224:                    boolean setToReadOnly, RepeatingResponse repeating)
225:                    throws IOException {
226:
227:                boolean promptIfWritable = !repeating.isDisablePrompt();
228:                boolean bFileHasBeenWritten = false;
229:
230:                java.io.File fileWrite = new java.io.File(flTargetDirectory,
231:                        sName);
232:                boolean bProcedeWithWrite = true;
233:                //Prompt if file exists.
234:                boolean bSetToReadOnly = setToReadOnly;
235:                if (fileWrite.exists()) {
236:                    if (fileWrite.canWrite() && promptIfWritable) {
237:                        int iResponse = -1;
238:                        if (repeating.hasResponse()) {
239:                            iResponse = repeating.getIntResponse();
240:                        } else {
241:                            iResponse = overwriteDialog(sName, repeating
242:                                    .getParentComponent());
243:                        }
244:
245:                        bProcedeWithWrite = interpretOverwriteResponse(
246:                                iResponse, repeating);
247:
248:                    }
249:                }
250:
251:                if (bProcedeWithWrite) {
252:
253:                    //make any required directories. Maybe prompt for this some day. . .
254:                    java.io.File parent = fileWrite.getParentFile();
255:                    if (!parent.exists()) {
256:                        parent.mkdirs();
257:                    }
258:
259:                    if (!fileWrite.canWrite()) {
260:                        makeFileWritable(fileWrite);
261:                    }
262:                    FileOutputStream stmFileOut = new FileOutputStream(
263:                            fileWrite);
264:                    try {
265:                        stmFileOut.write(file);
266:                        stmFileOut.flush();
267:                    } catch (IOException ex) {
268:                        //Let's give a better message.
269:                        throw new IOException("Unable to save the file "
270:                                + fileWrite.getName()
271:                                + "\r\nfor the following reason: "
272:                                + ex.getMessage());
273:                    } finally {
274:                        stmFileOut.close();
275:                        bFileHasBeenWritten = true;
276:                    }
277:                    if (bSetToReadOnly) {
278:                        try {
279:                            fileWrite.setReadOnly();
280:                        } catch (java.lang.SecurityException ex) {
281:                            //Let's give a better message.
282:                            throw new java.lang.SecurityException(
283:                                    "Unable to set the file "
284:                                            + fileWrite.getName()
285:                                            + " to read-only \r\nfor the following reason: "
286:                                            + ex.getMessage());
287:                        }
288:                    }
289:                }
290:                return bFileHasBeenWritten;
291:            }
292:
293:            private static int overwriteDialog(String fileName,
294:                    Component parentComponent) {
295:                String[] message = new String[4];
296:                message[0] = DisplayTextLibrary
297:                        .displayText(DisplayTextLibrary.MSG_OVERWRITE_FILE);
298:                message[2] = " ";
299:                message[1] = "      " + fileName;
300:                message[2] = " ";
301:                message[3] = DisplayTextLibrary
302:                        .displayText(DisplayTextLibrary.LBL_OVERWRITE_FILE);
303:
304:                if (parentComponent == null) {
305:                    parentComponent = CommandCentral.getInstance()
306:                            .getRootAppFrame();
307:                }
308:
309:                String[] options = {
310:                        DisplayTextLibrary
311:                                .displayText(DisplayTextLibrary.BTN_YES),
312:                        DisplayTextLibrary
313:                                .displayText(DisplayTextLibrary.BTN_YES_ALL),
314:                        DisplayTextLibrary
315:                                .displayText(DisplayTextLibrary.BTN_NO),
316:                        DisplayTextLibrary
317:                                .displayText(DisplayTextLibrary.BTN_NO_ALL) };
318:
319:                int iResponse = JOptionPane
320:                        .showOptionDialog(
321:                                parentComponent,
322:                                message,
323:                                DisplayTextLibrary
324:                                        .displayText(DisplayTextLibrary.LBL_OVERWRITE_FILE),
325:                                JOptionPane.DEFAULT_OPTION,
326:                                JOptionPane.WARNING_MESSAGE, null, options,
327:                                DisplayTextLibrary
328:                                        .displayText(DisplayTextLibrary.BTN_NO));
329:                return iResponse;
330:            }
331:
332:            public static void makeFileHidden(java.io.File file)
333:                    throws IOException {
334:                String sFileName = file.getAbsolutePath();
335:
336:                SourceJammerClient config = SourceJammerClient.getInstance();
337:
338:                String[] saHideFileHack = config.getHideFileCommand();
339:                if (saHideFileHack != null) {
340:                    String[] saCommand = new String[saHideFileHack.length + 1];
341:                    for (int i = 0; i < saHideFileHack.length; i++) {
342:                        saCommand[i] = saHideFileHack[i];
343:                    }
344:                    saCommand[saHideFileHack.length] = sFileName;
345:
346:                    Process proc = Runtime.getRuntime().exec(saCommand);
347:                    try {
348:                        proc.waitFor();
349:                    } catch (InterruptedException ex) {
350:                        System.out
351:                                .println("!! Make Hidden Process Interrupted !!");
352:                        ex.printStackTrace();
353:                    }
354:                } else {
355:                    //No hide file command, so we assume this does not apply to underlying OS.
356:                }
357:            }
358:
359:            public static void makeFileVisible(java.io.File file)
360:                    throws IOException {
361:                String sFileName = file.getAbsolutePath();
362:
363:                SourceJammerClient config = SourceJammerClient.getInstance();
364:
365:                String[] saShowFileHack = config.getShowFileCommand();
366:                if (saShowFileHack != null) {
367:                    String[] saCommand = new String[saShowFileHack.length + 1];
368:                    for (int i = 0; i < saShowFileHack.length; i++) {
369:                        saCommand[i] = saShowFileHack[i];
370:                    }
371:                    saCommand[saShowFileHack.length] = sFileName;
372:
373:                    Process proc = Runtime.getRuntime().exec(saCommand);
374:                    try {
375:                        proc.waitFor();
376:                    } catch (InterruptedException ex) {
377:                        System.out
378:                                .println("!! Make Hidden Process Interrupted !!");
379:                        ex.printStackTrace();
380:                    }
381:                } else {
382:                    //No hide file command, so we assume this does not apply to underlying OS.
383:                }
384:            }
385:
386:            /**
387:             * Makes a read-only file writable.
388:             */
389:            public static void makeFileWritable(java.io.File file)
390:                    throws IOException {
391:                if (!file.canWrite()) {
392:                    String sFileName = file.getAbsolutePath();
393:
394:                    SourceJammerClient config = SourceJammerClient
395:                            .getInstance();
396:
397:                    String[] saWritableHack = config.getMakeWritableCommand();
398:                    String[] saCommand = new String[saWritableHack.length + 1];
399:                    for (int i = 0; i < saWritableHack.length; i++) {
400:                        saCommand[i] = saWritableHack[i];
401:                    }
402:                    saCommand[saWritableHack.length] = sFileName;
403:
404:                    Process proc = Runtime.getRuntime().exec(saCommand);
405:                    try {
406:                        proc.waitFor();
407:                    } catch (InterruptedException ex) {
408:                        System.out
409:                                .println("!! Make Writable Process Interrupted !!");
410:                        ex.printStackTrace();
411:                    }
412:                }
413:            }
414:
415:            /**
416:             * Returns the sum of all bytes in the file.
417:             */
418:            public static long fileCheckSum(String sFullPathAndName)
419:                    throws IOException {
420:                File fl = new File(sFullPathAndName);
421:                return fileCheckSum(fl);
422:            }
423:
424:            /**
425:             * Returns the sum of all bytes in the file.
426:             */
427:            public static long fileCheckSum(File fl) throws IOException {
428:                byte[] byFile = readLocalFile(fl);
429:                return fileCheckSum(byFile);
430:            }
431:
432:            /**
433:             * Returns the sum of all bytes in the byte array.
434:             */
435:            public static long fileCheckSum(byte[] file) {
436:                long lSum = 0;
437:                for (int i = 0; i < file.length; i++) {
438:                    lSum += file[i];
439:                }
440:                return lSum;
441:            }
442:
443:            /**
444:             * Read and return a serialized object from specified File.
445:             */
446:            public static Object readObjectFromFileSys(File fileRead)
447:                    throws IOException {
448:
449:                Object obj = null;
450:                FileInputStream stmFileIn = new FileInputStream(fileRead);
451:                try {
452:                    ObjectInputStream stmObjIn = new ObjectInputStream(
453:                            stmFileIn);
454:                    try {
455:                        obj = stmObjIn.readObject();
456:                    } catch (ClassNotFoundException ex) {
457:                        throw new ConfigurationException(
458:                                "Could not find an object in the specified file.",
459:                                ex);
460:                    } finally {
461:                        stmObjIn.close();
462:                    }
463:                } finally {
464:                    stmFileIn.close();
465:                }
466:
467:                return obj;
468:
469:            }
470:
471:            /**
472:             * Writes the object to the specified location on the
473:             * file system. If the file already exists, it will be overwritten.
474:             */
475:            public static void writeObjectToFileSys(Object o, File fileWrite)
476:                    throws IOException {
477:
478:                FileOutputStream stmFileOut = new FileOutputStream(fileWrite);
479:                try {
480:                    ObjectOutputStream stmObjOut = new ObjectOutputStream(
481:                            stmFileOut);
482:                    try {
483:                        stmObjOut.writeObject(o);
484:                        stmObjOut.flush();
485:                    } finally {
486:                        stmObjOut.close();
487:                    }
488:                } finally {
489:                    stmFileOut.close();
490:                }
491:
492:            }
493:
494:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.