Source Code Cross Referenced for FileUtil.java in  » Testing » webtest » com » canoo » webtest » 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 » Testing » webtest » com.canoo.webtest.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright © 2002-2007 Canoo Engineering AG, Switzerland. Released under the Canoo Webtest license.
002:        package com.canoo.webtest.util;
003:
004:        import java.io.File;
005:        import java.io.FileInputStream;
006:        import java.io.FileOutputStream;
007:        import java.io.IOException;
008:        import java.io.InputStream;
009:        import java.io.ObjectInputStream;
010:        import java.io.ObjectOutputStream;
011:
012:        import com.canoo.webtest.boundary.StreamBoundary;
013:        import com.canoo.webtest.engine.StepExecutionException;
014:        import com.canoo.webtest.steps.Step;
015:        import com.gargoylesoftware.htmlunit.WebResponse;
016:
017:        import org.apache.commons.io.IOUtils;
018:        import org.apache.log4j.Logger;
019:
020:        /**
021:         * Utility class for working with files.
022:         *
023:         * @author Paul King
024:         */
025:        public class FileUtil {
026:            private static final Logger LOG = Logger.getLogger(FileUtil.class);
027:
028:            /**
029:             * Reads a file into a String.
030:             *
031:             * @param file
032:             * @param step
033:             * @return the resulting String
034:             */
035:            public static String readFileToString(final File file,
036:                    final Step step) {
037:                String canonicalPath = null;
038:                String result = null;
039:                InputStream inputStream = null;
040:                try {
041:                    canonicalPath = file.getCanonicalPath();
042:                    inputStream = new FileInputStream(file);
043:                    result = IOUtils.toString(inputStream);
044:                } catch (IOException e) {
045:                    throw new StepExecutionException("Could not find/read \""
046:                            + canonicalPath + "\".", step);
047:                } finally {
048:                    IOUtils.closeQuietly(inputStream);
049:                }
050:                return result;
051:            }
052:
053:            /**
054:             * Reads a file into a byte[].
055:             *
056:             * @param file
057:             * @param step
058:             * @return the resulting byte[]
059:             */
060:            public static byte[] readFileToByteArray(final File file,
061:                    final Step step) {
062:                String canonicalPath = null;
063:                byte[] result = null;
064:                InputStream inputStream = null;
065:                try {
066:                    canonicalPath = file.getCanonicalPath();
067:                    inputStream = new FileInputStream(file);
068:                    result = IOUtils.toByteArray(inputStream);
069:                } catch (IOException e) {
070:                    throw new StepExecutionException("Could not find/read \""
071:                            + canonicalPath + "\".", step);
072:                } finally {
073:                    IOUtils.closeQuietly(inputStream);
074:                }
075:                return result;
076:            }
077:
078:            /**
079:             * Writes a String to a file.
080:             *
081:             * @param file
082:             * @param content
083:             * @param step
084:             * @throws StepExecutionException if something goes wrong
085:             */
086:            public static void writeStringToFile(final File file,
087:                    final String content, final Step step) {
088:                String canonicalPath = null;
089:                FileOutputStream outputStream = null;
090:                try {
091:                    canonicalPath = file.getCanonicalPath();
092:                    outputStream = new FileOutputStream(file);
093:                    IOUtils.write(content, outputStream);
094:                } catch (IOException e) {
095:                    throw new StepExecutionException("Could not find/write \""
096:                            + canonicalPath + "\".", step);
097:                } finally {
098:                    IOUtils.closeQuietly(outputStream);
099:                }
100:            }
101:
102:            /**
103:             * Writes a Stream to a file.
104:             *
105:             * @param inputResponse
106:             * @param destfile
107:             * @param step
108:             * @throws StepExecutionException if something goes wrong
109:             */
110:            public static void writeResponseStreamToFile(
111:                    final WebResponse inputResponse, final File destfile,
112:                    final Step step) {
113:                String canonicalPath = null;
114:                FileOutputStream outputStream = null;
115:                try {
116:                    canonicalPath = destfile.getCanonicalPath();
117:                    outputStream = new FileOutputStream(destfile);
118:                    IOUtils.copy(inputResponse.getContentAsStream(),
119:                            outputStream);
120:                } catch (IOException e) {
121:                    throw new StepExecutionException("Could not find/write \""
122:                            + canonicalPath + "\".", step);
123:                } finally {
124:                    IOUtils.closeQuietly(outputStream);
125:                }
126:            }
127:
128:            /**
129:             * Creates parent directory for file if required.
130:             *
131:             * @param file
132:             */
133:            public static void prepareDirs(final File file) {
134:                if (file.getParentFile() != null
135:                        && !file.getParentFile().exists()) {
136:                    prepareDirs(file.getParentFile());
137:                    file.getParentFile().mkdirs();
138:                }
139:            }
140:
141:            /**
142:             * Helper method for reading objects from a file.
143:             *
144:             * @param file the file to read from
145:             * @param step step requesting the operation
146:             * @return the object from the file if everything worked correctly
147:             */
148:            public static Object tryReadObjectFromFile(final File file,
149:                    final Step step) {
150:                FileInputStream fis = null;
151:                ObjectInputStream ois = null;
152:                String message = "finding";
153:                try {
154:                    fis = new FileInputStream(file);
155:                    ois = new ObjectInputStream(fis);
156:                    message = "reading from";
157:                    return StreamBoundary.tryReadObject(ois, step);
158:                } catch (IOException e) {
159:                    LOG.error(e.getMessage(), e);
160:                    throw new StepExecutionException("Error " + message
161:                            + " file: " + e.getMessage(), step);
162:                } finally {
163:                    IOUtils.closeQuietly(ois);
164:                    IOUtils.closeQuietly(fis);
165:                }
166:            }
167:
168:            /**
169:             * Helper method for writing objects to a file.
170:             *
171:             * @param file the file to write to
172:             * @param object the object to write
173:             * @param step step requesting the operation
174:             */
175:            public static boolean tryWriteObjectToFile(final File file,
176:                    final Object object, final Step step) {
177:                FileOutputStream fos = null;
178:                ObjectOutputStream oos = null;
179:                String message = "creating";
180:                boolean success = false;
181:                try {
182:                    fos = new FileOutputStream(file);
183:                    oos = new ObjectOutputStream(fos);
184:                    message = "filling";
185:                    oos.writeObject(object);
186:                    success = true;
187:                } catch (IOException e) {
188:                    LOG.error("Error during write: " + e.getMessage(), e);
189:                    if (step != null) {
190:                        throw new StepExecutionException("Error " + message
191:                                + " file: " + e.getMessage(), step);
192:                    }
193:                } finally {
194:                    StreamBoundary.closeOutputStream(oos);
195:                    StreamBoundary.closeOutputStream(fos);
196:                }
197:                return success;
198:            }
199:
200:            /**
201:             * Creates a temporary file using a prefix and suffix and marks it for deletion on exit.
202:             *
203:             * @param prefix
204:             * @param suffix
205:             * @param step
206:             * @throws StepExecutionException if an error occurs while creating the file
207:             * @return the newly created temp file
208:             */
209:            public static File tryCreateTempFile(final String prefix,
210:                    final String suffix, final Step step) {
211:                final File tmpFile;
212:                try {
213:                    tmpFile = File.createTempFile(prefix, suffix);
214:                } catch (Exception e) {
215:                    LOG.error(e.getMessage(), e);
216:                    throw new StepExecutionException(
217:                            "Error creating temporary file " + e.getMessage(),
218:                            step);
219:                }
220:                tmpFile.deleteOnExit();
221:                return tmpFile;
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.