Source Code Cross Referenced for CopyUtils.java in  » Search-Engine » compass-2.0 » org » compass » core » 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 » Search Engine » compass 2.0 » org.compass.core.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2006 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.compass.core.util;
018:
019:        import java.io.*;
020:
021:        import org.apache.commons.logging.Log;
022:        import org.apache.commons.logging.LogFactory;
023:
024:        /**
025:         * Simple utility methods for file and stream copying.
026:         * All copy methods use a block size of 4096 bytes,
027:         * and close all affected streams when done.
028:         *
029:         * @author kimhcy
030:         */
031:        public abstract class CopyUtils {
032:
033:            private static final Log log = LogFactory.getLog(CopyUtils.class);
034:
035:            public static final int BUFFER_SIZE = 4096;
036:
037:            //---------------------------------------------------------------------
038:            // Copy methods for java.io.File
039:            //---------------------------------------------------------------------
040:
041:            /**
042:             * Copy the contents of the given input File to the given output File.
043:             *
044:             * @param in  the file to copy from
045:             * @param out the file to copy to
046:             * @return the number of bytes copied
047:             * @throws IOException in case of I/O errors
048:             */
049:            public static int copy(File in, File out) throws IOException {
050:                Assert.notNull(in, "No input File specified");
051:                Assert.notNull(out, "No output File specified");
052:                return copy(new BufferedInputStream(new FileInputStream(in)),
053:                        new BufferedOutputStream(new FileOutputStream(out)));
054:            }
055:
056:            /**
057:             * Copy the contents of the given byte array to the given output File.
058:             *
059:             * @param in  the byte array to copy from
060:             * @param out the file to copy to
061:             * @throws IOException in case of I/O errors
062:             */
063:            public static void copy(byte[] in, File out) throws IOException {
064:                Assert.notNull(in, "No input byte array specified");
065:                Assert.notNull(out, "No output File specified");
066:                ByteArrayInputStream inStream = new ByteArrayInputStream(in);
067:                OutputStream outStream = new BufferedOutputStream(
068:                        new FileOutputStream(out));
069:                copy(inStream, outStream);
070:            }
071:
072:            /**
073:             * Copy the contents of the given input File into a new byte array.
074:             *
075:             * @param in the file to copy from
076:             * @return the new byte array that has been copied to
077:             * @throws IOException in case of I/O errors
078:             */
079:            public static byte[] copyToByteArray(File in) throws IOException {
080:                Assert.notNull(in, "No input File specified");
081:                return copyToByteArray(new BufferedInputStream(
082:                        new FileInputStream(in)));
083:            }
084:
085:            //---------------------------------------------------------------------
086:            // Copy methods for java.io.InputStream / java.io.OutputStream
087:            //---------------------------------------------------------------------
088:
089:            /**
090:             * Copy the contents of the given InputStream to the given OutputStream.
091:             * Closes both streams when done.
092:             *
093:             * @param in  the stream to copy from
094:             * @param out the stream to copy to
095:             * @return the number of bytes copied
096:             * @throws IOException in case of I/O errors
097:             */
098:            public static int copy(InputStream in, OutputStream out)
099:                    throws IOException {
100:                Assert.notNull(in, "No InputStream specified");
101:                Assert.notNull(out, "No OutputStream specified");
102:                try {
103:                    int byteCount = 0;
104:                    byte[] buffer = new byte[BUFFER_SIZE];
105:                    int bytesRead = -1;
106:                    while ((bytesRead = in.read(buffer)) != -1) {
107:                        out.write(buffer, 0, bytesRead);
108:                        byteCount += bytesRead;
109:                    }
110:                    out.flush();
111:                    return byteCount;
112:                } finally {
113:                    try {
114:                        in.close();
115:                    } catch (IOException ex) {
116:                        log.warn("Could not close InputStream", ex);
117:                    }
118:                    try {
119:                        out.close();
120:                    } catch (IOException ex) {
121:                        log.warn("Could not close OutputStream", ex);
122:                    }
123:                }
124:            }
125:
126:            /**
127:             * Copy the contents of the given byte array to the given OutputStream.
128:             * Closes the stream when done.
129:             *
130:             * @param in  the byte array to copy from
131:             * @param out the OutputStream to copy to
132:             * @throws IOException in case of I/O errors
133:             */
134:            public static void copy(byte[] in, OutputStream out)
135:                    throws IOException {
136:                Assert.notNull(in, "No input byte array specified");
137:                Assert.notNull(out, "No OutputStream specified");
138:                try {
139:                    out.write(in);
140:                } finally {
141:                    try {
142:                        out.close();
143:                    } catch (IOException ex) {
144:                        log.warn("Could not close OutputStream", ex);
145:                    }
146:                }
147:            }
148:
149:            /**
150:             * Copy the contents of the given InputStream into a new byte array.
151:             * Closes the stream when done.
152:             *
153:             * @param in the stream to copy from
154:             * @return the new byte array that has been copied to
155:             * @throws IOException in case of I/O errors
156:             */
157:            public static byte[] copyToByteArray(InputStream in)
158:                    throws IOException {
159:                ByteArrayOutputStream out = new ByteArrayOutputStream(
160:                        BUFFER_SIZE);
161:                copy(in, out);
162:                return out.toByteArray();
163:            }
164:
165:            //---------------------------------------------------------------------
166:            // Copy methods for java.io.Reader / java.io.Writer
167:            //---------------------------------------------------------------------
168:
169:            /**
170:             * Copy the contents of the given Reader to the given Writer.
171:             * Closes both when done.
172:             *
173:             * @param in  the Reader to copy from
174:             * @param out the Writer to copy to
175:             * @return the number of characters copied
176:             * @throws IOException in case of I/O errors
177:             */
178:            public static int copy(Reader in, Writer out) throws IOException {
179:                Assert.notNull(in, "No Reader specified");
180:                Assert.notNull(out, "No Writer specified");
181:                try {
182:                    int byteCount = 0;
183:                    char[] buffer = new char[BUFFER_SIZE];
184:                    int bytesRead = -1;
185:                    while ((bytesRead = in.read(buffer)) != -1) {
186:                        out.write(buffer, 0, bytesRead);
187:                        byteCount += bytesRead;
188:                    }
189:                    out.flush();
190:                    return byteCount;
191:                } finally {
192:                    try {
193:                        in.close();
194:                    } catch (IOException ex) {
195:                        log.warn("Could not close Reader", ex);
196:                    }
197:                    try {
198:                        out.close();
199:                    } catch (IOException ex) {
200:                        log.warn("Could not close Writer", ex);
201:                    }
202:                }
203:            }
204:
205:            /**
206:             * Copy the contents of the given String to the given output Writer.
207:             * Closes the write when done.
208:             *
209:             * @param in  the String to copy from
210:             * @param out the Writer to copy to
211:             * @throws IOException in case of I/O errors
212:             */
213:            public static void copy(String in, Writer out) throws IOException {
214:                Assert.notNull(in, "No input String specified");
215:                Assert.notNull(out, "No Writer specified");
216:                try {
217:                    out.write(in);
218:                } finally {
219:                    try {
220:                        out.close();
221:                    } catch (IOException ex) {
222:                        log.warn("Could not close Writer", ex);
223:                    }
224:                }
225:            }
226:
227:            /**
228:             * Copy the contents of the given Reader into a String.
229:             * Closes the reader when done.
230:             *
231:             * @param in the reader to copy from
232:             * @return the String that has been copied to
233:             * @throws IOException in case of I/O errors
234:             */
235:            public static String copyToString(Reader in) throws IOException {
236:                StringWriter out = new StringWriter();
237:                copy(in, out);
238:                return out.toString();
239:            }
240:
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.