Source Code Cross Referenced for FileTool.java in  » IDE-Eclipse » ui » org » eclipse » ui » tests » harness » 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 » IDE Eclipse » ui » org.eclipse.ui.tests.harness.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2006 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.tests.harness.util;
011:
012:        import java.io.File;
013:        import java.io.FileInputStream;
014:        import java.io.FileOutputStream;
015:        import java.io.FileReader;
016:        import java.io.FileWriter;
017:        import java.io.IOException;
018:        import java.io.InputStream;
019:        import java.io.OutputStream;
020:        import java.io.Reader;
021:        import java.io.Writer;
022:        import java.net.URL;
023:        import java.util.Enumeration;
024:        import java.util.zip.ZipEntry;
025:        import java.util.zip.ZipFile;
026:
027:        import org.eclipse.core.runtime.IPath;
028:        import org.eclipse.core.runtime.Platform;
029:        import org.eclipse.core.runtime.Plugin;
030:
031:        public class FileTool {
032:
033:            /**
034:             * A buffer.
035:             */
036:            private static byte[] buffer = new byte[8192];
037:
038:            /**
039:             * Unzips the given zip file to the given destination directory
040:             * extracting only those entries the pass through the given
041:             * filter.
042:             * 
043:             * @param filter filters out unwanted zip entries
044:             * @param zipFile the zip file to unzip
045:             * @param dstDir the destination directory
046:             */
047:            public static void unzip(ZipFile zipFile, File dstDir)
048:                    throws IOException {
049:                unzip(zipFile, dstDir, dstDir, 0);
050:            }
051:
052:            private static void unzip(ZipFile zipFile, File rootDstDir,
053:                    File dstDir, int depth) throws IOException {
054:
055:                Enumeration entries = zipFile.entries();
056:
057:                try {
058:                    while (entries.hasMoreElements()) {
059:                        ZipEntry entry = (ZipEntry) entries.nextElement();
060:                        if (entry.isDirectory()) {
061:                            continue;
062:                        }
063:                        String entryName = entry.getName();
064:                        File file = new File(dstDir, changeSeparator(entryName,
065:                                '/', File.separatorChar));
066:                        file.getParentFile().mkdirs();
067:                        InputStream src = null;
068:                        OutputStream dst = null;
069:                        try {
070:                            src = zipFile.getInputStream(entry);
071:                            dst = new FileOutputStream(file);
072:                            transferData(src, dst);
073:                        } finally {
074:                            if (dst != null) {
075:                                try {
076:                                    dst.close();
077:                                } catch (IOException e) {
078:                                }
079:                            }
080:                            if (src != null) {
081:                                try {
082:                                    src.close();
083:                                } catch (IOException e) {
084:                                }
085:                            }
086:                        }
087:                    }
088:                } finally {
089:                    try {
090:                        zipFile.close();
091:                    } catch (IOException e) {
092:                    }
093:                }
094:            }
095:
096:            /**
097:             * Returns the given file path with its separator
098:             * character changed from the given old separator to the
099:             * given new separator.
100:             * 
101:             * @param path a file path
102:             * @param oldSeparator a path separator character
103:             * @param newSeparator a path separator character
104:             * @return the file path with its separator character
105:             * changed from the given old separator to the given new
106:             * separator
107:             */
108:            public static String changeSeparator(String path,
109:                    char oldSeparator, char newSeparator) {
110:                return path.replace(oldSeparator, newSeparator);
111:            }
112:
113:            /**
114:             * Copies all bytes in the given source file to
115:             * the given destination file.
116:             * 
117:             * @param source the given source file
118:             * @param destination the given destination file
119:             */
120:            public static void transferData(File source, File destination)
121:                    throws IOException {
122:                destination.getParentFile().mkdirs();
123:                InputStream is = null;
124:                OutputStream os = null;
125:                try {
126:                    is = new FileInputStream(source);
127:                    os = new FileOutputStream(destination);
128:                    transferData(is, os);
129:                } finally {
130:                    if (os != null) {
131:                        try {
132:                            os.close();
133:                        } catch (IOException e) {
134:                        }
135:                    }
136:                    if (is != null) {
137:                        try {
138:                            is.close();
139:                        } catch (IOException e) {
140:                        }
141:                    }
142:                }
143:            }
144:
145:            /**
146:             * Copies all bytes in the given source stream to
147:             * the given destination stream. Neither streams
148:             * are closed.
149:             * 
150:             * @param source the given source stream
151:             * @param destination the given destination stream
152:             */
153:            public static void transferData(InputStream source,
154:                    OutputStream destination) throws IOException {
155:                int bytesRead = 0;
156:                while (bytesRead != -1) {
157:                    bytesRead = source.read(buffer, 0, buffer.length);
158:                    if (bytesRead != -1) {
159:                        destination.write(buffer, 0, bytesRead);
160:                    }
161:                }
162:            }
163:
164:            /**
165:             * Copies the given source file to the given destination file.
166:             * 
167:             * @param src the given source file
168:             * @param dst the given destination file
169:             */
170:            public static void copy(File src, File dst) throws IOException {
171:                if (src.isDirectory()) {
172:                    String[] srcChildren = src.list();
173:                    for (int i = 0; i < srcChildren.length; ++i) {
174:                        File srcChild = new File(src, srcChildren[i]);
175:                        File dstChild = new File(dst, srcChildren[i]);
176:                        copy(srcChild, dstChild);
177:                    }
178:                } else
179:                    transferData(src, dst);
180:            }
181:
182:            public static File getFileInPlugin(Plugin plugin, IPath path) {
183:                try {
184:                    URL installURL = plugin.getBundle().getEntry(
185:                            path.toString());
186:                    URL localURL = Platform.asLocalURL(installURL);
187:                    return new File(localURL.getFile());
188:                } catch (IOException e) {
189:                    return null;
190:                }
191:            }
192:
193:            public static StringBuffer read(String fileName) throws IOException {
194:                return read(new FileReader(fileName));
195:            }
196:
197:            public static StringBuffer read(Reader reader) throws IOException {
198:                StringBuffer s = new StringBuffer();
199:                try {
200:                    char[] buffer = new char[8196];
201:                    int chars = reader.read(buffer);
202:                    while (chars != -1) {
203:                        s.append(buffer, 0, chars);
204:                        chars = reader.read(buffer);
205:                    }
206:                } finally {
207:                    try {
208:                        reader.close();
209:                    } catch (IOException e) {
210:                    }
211:                }
212:                return s;
213:            }
214:
215:            public static void write(String fileName, StringBuffer content)
216:                    throws IOException {
217:                Writer writer = new FileWriter(fileName);
218:                try {
219:                    writer.write(content.toString());
220:                } finally {
221:                    try {
222:                        writer.close();
223:                    } catch (IOException e) {
224:                    }
225:                }
226:            }
227:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.