Source Code Cross Referenced for IOUtils.java in  » Library » apache-common-IO » org » apache » commons » io » 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 » Library » apache common IO » org.apache.commons.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *      http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:        package org.apache.commons.io;
0018:
0019:        import java.io.BufferedInputStream;
0020:        import java.io.BufferedReader;
0021:        import java.io.ByteArrayInputStream;
0022:        import java.io.CharArrayWriter;
0023:        import java.io.File;
0024:        import java.io.IOException;
0025:        import java.io.InputStream;
0026:        import java.io.InputStreamReader;
0027:        import java.io.OutputStream;
0028:        import java.io.OutputStreamWriter;
0029:        import java.io.PrintWriter;
0030:        import java.io.Reader;
0031:        import java.io.StringWriter;
0032:        import java.io.Writer;
0033:        import java.util.ArrayList;
0034:        import java.util.Collection;
0035:        import java.util.Iterator;
0036:        import java.util.List;
0037:
0038:        import org.apache.commons.io.output.ByteArrayOutputStream;
0039:
0040:        /**
0041:         * General IO stream manipulation utilities.
0042:         * <p>
0043:         * This class provides static utility methods for input/output operations.
0044:         * <ul>
0045:         * <li>closeQuietly - these methods close a stream ignoring nulls and exceptions
0046:         * <li>toXxx/read - these methods read data from a stream
0047:         * <li>write - these methods write data to a stream
0048:         * <li>copy - these methods copy all the data from one stream to another
0049:         * <li>contentEquals - these methods compare the content of two streams
0050:         * </ul>
0051:         * <p>
0052:         * The byte-to-char methods and char-to-byte methods involve a conversion step.
0053:         * Two methods are provided in each case, one that uses the platform default
0054:         * encoding and the other which allows you to specify an encoding. You are
0055:         * encouraged to always specify an encoding because relying on the platform
0056:         * default can lead to unexpected results, for example when moving from
0057:         * development to production.
0058:         * <p>
0059:         * All the methods in this class that read a stream are buffered internally.
0060:         * This means that there is no cause to use a <code>BufferedInputStream</code>
0061:         * or <code>BufferedReader</code>. The default buffer size of 4K has been shown
0062:         * to be efficient in tests.
0063:         * <p>
0064:         * Wherever possible, the methods in this class do <em>not</em> flush or close
0065:         * the stream. This is to avoid making non-portable assumptions about the
0066:         * streams' origin and further use. Thus the caller is still responsible for
0067:         * closing streams after use.
0068:         * <p>
0069:         * Origin of code: Excalibur.
0070:         *
0071:         * @author Peter Donald
0072:         * @author Jeff Turner
0073:         * @author Matthew Hawthorne
0074:         * @author Stephen Colebourne
0075:         * @author Gareth Davis
0076:         * @author Ian Springer
0077:         * @author Niall Pemberton
0078:         * @author Sandy McArthur
0079:         * @version $Id: IOUtils.java 481854 2006-12-03 18:30:07Z scolebourne $
0080:         */
0081:        public class IOUtils {
0082:            // NOTE: This class is focussed on InputStream, OutputStream, Reader and
0083:            // Writer. Each method should take at least one of these as a parameter,
0084:            // or return one of them.
0085:
0086:            /**
0087:             * The Unix directory separator character.
0088:             */
0089:            public static final char DIR_SEPARATOR_UNIX = '/';
0090:            /**
0091:             * The Windows directory separator character.
0092:             */
0093:            public static final char DIR_SEPARATOR_WINDOWS = '\\';
0094:            /**
0095:             * The system directory separator character.
0096:             */
0097:            public static final char DIR_SEPARATOR = File.separatorChar;
0098:            /**
0099:             * The Unix line separator string.
0100:             */
0101:            public static final String LINE_SEPARATOR_UNIX = "\n";
0102:            /**
0103:             * The Windows line separator string.
0104:             */
0105:            public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
0106:            /**
0107:             * The system line separator string.
0108:             */
0109:            public static final String LINE_SEPARATOR;
0110:            static {
0111:                // avoid security issues
0112:                StringWriter buf = new StringWriter(4);
0113:                PrintWriter out = new PrintWriter(buf);
0114:                out.println();
0115:                LINE_SEPARATOR = buf.toString();
0116:            }
0117:
0118:            /**
0119:             * The default buffer size to use.
0120:             */
0121:            private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
0122:
0123:            /**
0124:             * Instances should NOT be constructed in standard programming.
0125:             */
0126:            public IOUtils() {
0127:                super ();
0128:            }
0129:
0130:            //-----------------------------------------------------------------------
0131:            /**
0132:             * Unconditionally close an <code>Reader</code>.
0133:             * <p>
0134:             * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
0135:             * This is typically used in finally blocks.
0136:             *
0137:             * @param input  the Reader to close, may be null or already closed
0138:             */
0139:            public static void closeQuietly(Reader input) {
0140:                try {
0141:                    if (input != null) {
0142:                        input.close();
0143:                    }
0144:                } catch (IOException ioe) {
0145:                    // ignore
0146:                }
0147:            }
0148:
0149:            /**
0150:             * Unconditionally close a <code>Writer</code>.
0151:             * <p>
0152:             * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
0153:             * This is typically used in finally blocks.
0154:             *
0155:             * @param output  the Writer to close, may be null or already closed
0156:             */
0157:            public static void closeQuietly(Writer output) {
0158:                try {
0159:                    if (output != null) {
0160:                        output.close();
0161:                    }
0162:                } catch (IOException ioe) {
0163:                    // ignore
0164:                }
0165:            }
0166:
0167:            /**
0168:             * Unconditionally close an <code>InputStream</code>.
0169:             * <p>
0170:             * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
0171:             * This is typically used in finally blocks.
0172:             *
0173:             * @param input  the InputStream to close, may be null or already closed
0174:             */
0175:            public static void closeQuietly(InputStream input) {
0176:                try {
0177:                    if (input != null) {
0178:                        input.close();
0179:                    }
0180:                } catch (IOException ioe) {
0181:                    // ignore
0182:                }
0183:            }
0184:
0185:            /**
0186:             * Unconditionally close an <code>OutputStream</code>.
0187:             * <p>
0188:             * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
0189:             * This is typically used in finally blocks.
0190:             *
0191:             * @param output  the OutputStream to close, may be null or already closed
0192:             */
0193:            public static void closeQuietly(OutputStream output) {
0194:                try {
0195:                    if (output != null) {
0196:                        output.close();
0197:                    }
0198:                } catch (IOException ioe) {
0199:                    // ignore
0200:                }
0201:            }
0202:
0203:            // read toByteArray
0204:            //-----------------------------------------------------------------------
0205:            /**
0206:             * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
0207:             * <p>
0208:             * This method buffers the input internally, so there is no need to use a
0209:             * <code>BufferedInputStream</code>.
0210:             * 
0211:             * @param input  the <code>InputStream</code> to read from
0212:             * @return the requested byte array
0213:             * @throws NullPointerException if the input is null
0214:             * @throws IOException if an I/O error occurs
0215:             */
0216:            public static byte[] toByteArray(InputStream input)
0217:                    throws IOException {
0218:                ByteArrayOutputStream output = new ByteArrayOutputStream();
0219:                copy(input, output);
0220:                return output.toByteArray();
0221:            }
0222:
0223:            /**
0224:             * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
0225:             * using the default character encoding of the platform.
0226:             * <p>
0227:             * This method buffers the input internally, so there is no need to use a
0228:             * <code>BufferedReader</code>.
0229:             * 
0230:             * @param input  the <code>Reader</code> to read from
0231:             * @return the requested byte array
0232:             * @throws NullPointerException if the input is null
0233:             * @throws IOException if an I/O error occurs
0234:             */
0235:            public static byte[] toByteArray(Reader input) throws IOException {
0236:                ByteArrayOutputStream output = new ByteArrayOutputStream();
0237:                copy(input, output);
0238:                return output.toByteArray();
0239:            }
0240:
0241:            /**
0242:             * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
0243:             * using the specified character encoding.
0244:             * <p>
0245:             * Character encoding names can be found at
0246:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0247:             * <p>
0248:             * This method buffers the input internally, so there is no need to use a
0249:             * <code>BufferedReader</code>.
0250:             * 
0251:             * @param input  the <code>Reader</code> to read from
0252:             * @param encoding  the encoding to use, null means platform default
0253:             * @return the requested byte array
0254:             * @throws NullPointerException if the input is null
0255:             * @throws IOException if an I/O error occurs
0256:             * @since Commons IO 1.1
0257:             */
0258:            public static byte[] toByteArray(Reader input, String encoding)
0259:                    throws IOException {
0260:                ByteArrayOutputStream output = new ByteArrayOutputStream();
0261:                copy(input, output, encoding);
0262:                return output.toByteArray();
0263:            }
0264:
0265:            /**
0266:             * Get the contents of a <code>String</code> as a <code>byte[]</code>
0267:             * using the default character encoding of the platform.
0268:             * <p>
0269:             * This is the same as {@link String#getBytes()}.
0270:             * 
0271:             * @param input  the <code>String</code> to convert
0272:             * @return the requested byte array
0273:             * @throws NullPointerException if the input is null
0274:             * @throws IOException if an I/O error occurs (never occurs)
0275:             * @deprecated Use {@link String#getBytes()}
0276:             */
0277:            public static byte[] toByteArray(String input) throws IOException {
0278:                return input.getBytes();
0279:            }
0280:
0281:            // read char[]
0282:            //-----------------------------------------------------------------------
0283:            /**
0284:             * Get the contents of an <code>InputStream</code> as a character array
0285:             * using the default character encoding of the platform.
0286:             * <p>
0287:             * This method buffers the input internally, so there is no need to use a
0288:             * <code>BufferedInputStream</code>.
0289:             * 
0290:             * @param is  the <code>InputStream</code> to read from
0291:             * @return the requested character array
0292:             * @throws NullPointerException if the input is null
0293:             * @throws IOException if an I/O error occurs
0294:             * @since Commons IO 1.1
0295:             */
0296:            public static char[] toCharArray(InputStream is) throws IOException {
0297:                CharArrayWriter output = new CharArrayWriter();
0298:                copy(is, output);
0299:                return output.toCharArray();
0300:            }
0301:
0302:            /**
0303:             * Get the contents of an <code>InputStream</code> as a character array
0304:             * using the specified character encoding.
0305:             * <p>
0306:             * Character encoding names can be found at
0307:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0308:             * <p>
0309:             * This method buffers the input internally, so there is no need to use a
0310:             * <code>BufferedInputStream</code>.
0311:             * 
0312:             * @param is  the <code>InputStream</code> to read from
0313:             * @param encoding  the encoding to use, null means platform default
0314:             * @return the requested character array
0315:             * @throws NullPointerException if the input is null
0316:             * @throws IOException if an I/O error occurs
0317:             * @since Commons IO 1.1
0318:             */
0319:            public static char[] toCharArray(InputStream is, String encoding)
0320:                    throws IOException {
0321:                CharArrayWriter output = new CharArrayWriter();
0322:                copy(is, output, encoding);
0323:                return output.toCharArray();
0324:            }
0325:
0326:            /**
0327:             * Get the contents of a <code>Reader</code> as a character array.
0328:             * <p>
0329:             * This method buffers the input internally, so there is no need to use a
0330:             * <code>BufferedReader</code>.
0331:             * 
0332:             * @param input  the <code>Reader</code> to read from
0333:             * @return the requested character array
0334:             * @throws NullPointerException if the input is null
0335:             * @throws IOException if an I/O error occurs
0336:             * @since Commons IO 1.1
0337:             */
0338:            public static char[] toCharArray(Reader input) throws IOException {
0339:                CharArrayWriter sw = new CharArrayWriter();
0340:                copy(input, sw);
0341:                return sw.toCharArray();
0342:            }
0343:
0344:            // read toString
0345:            //-----------------------------------------------------------------------
0346:            /**
0347:             * Get the contents of an <code>InputStream</code> as a String
0348:             * using the default character encoding of the platform.
0349:             * <p>
0350:             * This method buffers the input internally, so there is no need to use a
0351:             * <code>BufferedInputStream</code>.
0352:             * 
0353:             * @param input  the <code>InputStream</code> to read from
0354:             * @return the requested String
0355:             * @throws NullPointerException if the input is null
0356:             * @throws IOException if an I/O error occurs
0357:             */
0358:            public static String toString(InputStream input) throws IOException {
0359:                StringWriter sw = new StringWriter();
0360:                copy(input, sw);
0361:                return sw.toString();
0362:            }
0363:
0364:            /**
0365:             * Get the contents of an <code>InputStream</code> as a String
0366:             * using the specified character encoding.
0367:             * <p>
0368:             * Character encoding names can be found at
0369:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0370:             * <p>
0371:             * This method buffers the input internally, so there is no need to use a
0372:             * <code>BufferedInputStream</code>.
0373:             * 
0374:             * @param input  the <code>InputStream</code> to read from
0375:             * @param encoding  the encoding to use, null means platform default
0376:             * @return the requested String
0377:             * @throws NullPointerException if the input is null
0378:             * @throws IOException if an I/O error occurs
0379:             */
0380:            public static String toString(InputStream input, String encoding)
0381:                    throws IOException {
0382:                StringWriter sw = new StringWriter();
0383:                copy(input, sw, encoding);
0384:                return sw.toString();
0385:            }
0386:
0387:            /**
0388:             * Get the contents of a <code>Reader</code> as a String.
0389:             * <p>
0390:             * This method buffers the input internally, so there is no need to use a
0391:             * <code>BufferedReader</code>.
0392:             * 
0393:             * @param input  the <code>Reader</code> to read from
0394:             * @return the requested String
0395:             * @throws NullPointerException if the input is null
0396:             * @throws IOException if an I/O error occurs
0397:             */
0398:            public static String toString(Reader input) throws IOException {
0399:                StringWriter sw = new StringWriter();
0400:                copy(input, sw);
0401:                return sw.toString();
0402:            }
0403:
0404:            /**
0405:             * Get the contents of a <code>byte[]</code> as a String
0406:             * using the default character encoding of the platform.
0407:             * 
0408:             * @param input the byte array to read from
0409:             * @return the requested String
0410:             * @throws NullPointerException if the input is null
0411:             * @throws IOException if an I/O error occurs (never occurs)
0412:             * @deprecated Use {@link String#String(byte[])}
0413:             */
0414:            public static String toString(byte[] input) throws IOException {
0415:                return new String(input);
0416:            }
0417:
0418:            /**
0419:             * Get the contents of a <code>byte[]</code> as a String
0420:             * using the specified character encoding.
0421:             * <p>
0422:             * Character encoding names can be found at
0423:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0424:             * 
0425:             * @param input the byte array to read from
0426:             * @param encoding  the encoding to use, null means platform default
0427:             * @return the requested String
0428:             * @throws NullPointerException if the input is null
0429:             * @throws IOException if an I/O error occurs (never occurs)
0430:             * @deprecated Use {@link String#String(byte[],String)}
0431:             */
0432:            public static String toString(byte[] input, String encoding)
0433:                    throws IOException {
0434:                if (encoding == null) {
0435:                    return new String(input);
0436:                } else {
0437:                    return new String(input, encoding);
0438:                }
0439:            }
0440:
0441:            // readLines
0442:            //-----------------------------------------------------------------------
0443:            /**
0444:             * Get the contents of an <code>InputStream</code> as a list of Strings,
0445:             * one entry per line, using the default character encoding of the platform.
0446:             * <p>
0447:             * This method buffers the input internally, so there is no need to use a
0448:             * <code>BufferedInputStream</code>.
0449:             *
0450:             * @param input  the <code>InputStream</code> to read from, not null
0451:             * @return the list of Strings, never null
0452:             * @throws NullPointerException if the input is null
0453:             * @throws IOException if an I/O error occurs
0454:             * @since Commons IO 1.1
0455:             */
0456:            public static List readLines(InputStream input) throws IOException {
0457:                InputStreamReader reader = new InputStreamReader(input);
0458:                return readLines(reader);
0459:            }
0460:
0461:            /**
0462:             * Get the contents of an <code>InputStream</code> as a list of Strings,
0463:             * one entry per line, using the specified character encoding.
0464:             * <p>
0465:             * Character encoding names can be found at
0466:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0467:             * <p>
0468:             * This method buffers the input internally, so there is no need to use a
0469:             * <code>BufferedInputStream</code>.
0470:             *
0471:             * @param input  the <code>InputStream</code> to read from, not null
0472:             * @param encoding  the encoding to use, null means platform default
0473:             * @return the list of Strings, never null
0474:             * @throws NullPointerException if the input is null
0475:             * @throws IOException if an I/O error occurs
0476:             * @since Commons IO 1.1
0477:             */
0478:            public static List readLines(InputStream input, String encoding)
0479:                    throws IOException {
0480:                if (encoding == null) {
0481:                    return readLines(input);
0482:                } else {
0483:                    InputStreamReader reader = new InputStreamReader(input,
0484:                            encoding);
0485:                    return readLines(reader);
0486:                }
0487:            }
0488:
0489:            /**
0490:             * Get the contents of a <code>Reader</code> as a list of Strings,
0491:             * one entry per line.
0492:             * <p>
0493:             * This method buffers the input internally, so there is no need to use a
0494:             * <code>BufferedReader</code>.
0495:             *
0496:             * @param input  the <code>Reader</code> to read from, not null
0497:             * @return the list of Strings, never null
0498:             * @throws NullPointerException if the input is null
0499:             * @throws IOException if an I/O error occurs
0500:             * @since Commons IO 1.1
0501:             */
0502:            public static List readLines(Reader input) throws IOException {
0503:                BufferedReader reader = new BufferedReader(input);
0504:                List list = new ArrayList();
0505:                String line = reader.readLine();
0506:                while (line != null) {
0507:                    list.add(line);
0508:                    line = reader.readLine();
0509:                }
0510:                return list;
0511:            }
0512:
0513:            // lineIterator
0514:            //-----------------------------------------------------------------------
0515:            /**
0516:             * Return an Iterator for the lines in a <code>Reader</code>.
0517:             * <p>
0518:             * <code>LineIterator</code> holds a reference to the open
0519:             * <code>Reader</code> specified here. When you have finished with the
0520:             * iterator you should close the reader to free internal resources.
0521:             * This can be done by closing the reader directly, or by calling
0522:             * {@link LineIterator#close()} or {@link LineIterator#closeQuietly(LineIterator)}.
0523:             * <p>
0524:             * The recommended usage pattern is:
0525:             * <pre>
0526:             * try {
0527:             *   LineIterator it = IOUtils.lineIterator(reader);
0528:             *   while (it.hasNext()) {
0529:             *     String line = it.nextLine();
0530:             *     /// do something with line
0531:             *   }
0532:             * } finally {
0533:             *   IOUtils.closeQuietly(reader);
0534:             * }
0535:             * </pre>
0536:             *
0537:             * @param reader  the <code>Reader</code> to read from, not null
0538:             * @return an Iterator of the lines in the reader, never null
0539:             * @throws IllegalArgumentException if the reader is null
0540:             * @since Commons IO 1.2
0541:             */
0542:            public static LineIterator lineIterator(Reader reader) {
0543:                return new LineIterator(reader);
0544:            }
0545:
0546:            /**
0547:             * Return an Iterator for the lines in an <code>InputStream</code>, using
0548:             * the character encoding specified (or default encoding if null).
0549:             * <p>
0550:             * <code>LineIterator</code> holds a reference to the open
0551:             * <code>InputStream</code> specified here. When you have finished with
0552:             * the iterator you should close the stream to free internal resources.
0553:             * This can be done by closing the stream directly, or by calling
0554:             * {@link LineIterator#close()} or {@link LineIterator#closeQuietly(LineIterator)}.
0555:             * <p>
0556:             * The recommended usage pattern is:
0557:             * <pre>
0558:             * try {
0559:             *   LineIterator it = IOUtils.lineIterator(stream, "UTF-8");
0560:             *   while (it.hasNext()) {
0561:             *     String line = it.nextLine();
0562:             *     /// do something with line
0563:             *   }
0564:             * } finally {
0565:             *   IOUtils.closeQuietly(stream);
0566:             * }
0567:             * </pre>
0568:             *
0569:             * @param input  the <code>InputStream</code> to read from, not null
0570:             * @param encoding  the encoding to use, null means platform default
0571:             * @return an Iterator of the lines in the reader, never null
0572:             * @throws IllegalArgumentException if the input is null
0573:             * @throws IOException if an I/O error occurs, such as if the encoding is invalid
0574:             * @since Commons IO 1.2
0575:             */
0576:            public static LineIterator lineIterator(InputStream input,
0577:                    String encoding) throws IOException {
0578:                Reader reader = null;
0579:                if (encoding == null) {
0580:                    reader = new InputStreamReader(input);
0581:                } else {
0582:                    reader = new InputStreamReader(input, encoding);
0583:                }
0584:                return new LineIterator(reader);
0585:            }
0586:
0587:            //-----------------------------------------------------------------------
0588:            /**
0589:             * Convert the specified string to an input stream, encoded as bytes
0590:             * using the default character encoding of the platform.
0591:             *
0592:             * @param input the string to convert
0593:             * @return an input stream
0594:             * @since Commons IO 1.1
0595:             */
0596:            public static InputStream toInputStream(String input) {
0597:                byte[] bytes = input.getBytes();
0598:                return new ByteArrayInputStream(bytes);
0599:            }
0600:
0601:            /**
0602:             * Convert the specified string to an input stream, encoded as bytes
0603:             * using the specified character encoding.
0604:             * <p>
0605:             * Character encoding names can be found at
0606:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0607:             *
0608:             * @param input the string to convert
0609:             * @param encoding the encoding to use, null means platform default
0610:             * @throws IOException if the encoding is invalid
0611:             * @return an input stream
0612:             * @since Commons IO 1.1
0613:             */
0614:            public static InputStream toInputStream(String input,
0615:                    String encoding) throws IOException {
0616:                byte[] bytes = encoding != null ? input.getBytes(encoding)
0617:                        : input.getBytes();
0618:                return new ByteArrayInputStream(bytes);
0619:            }
0620:
0621:            // write byte[]
0622:            //-----------------------------------------------------------------------
0623:            /**
0624:             * Writes bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
0625:             * 
0626:             * @param data  the byte array to write, do not modify during output,
0627:             * null ignored
0628:             * @param output  the <code>OutputStream</code> to write to
0629:             * @throws NullPointerException if output is null
0630:             * @throws IOException if an I/O error occurs
0631:             * @since Commons IO 1.1
0632:             */
0633:            public static void write(byte[] data, OutputStream output)
0634:                    throws IOException {
0635:                if (data != null) {
0636:                    output.write(data);
0637:                }
0638:            }
0639:
0640:            /**
0641:             * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
0642:             * using the default character encoding of the platform.
0643:             * <p>
0644:             * This method uses {@link String#String(byte[])}.
0645:             * 
0646:             * @param data  the byte array to write, do not modify during output,
0647:             * null ignored
0648:             * @param output  the <code>Writer</code> to write to
0649:             * @throws NullPointerException if output is null
0650:             * @throws IOException if an I/O error occurs
0651:             * @since Commons IO 1.1
0652:             */
0653:            public static void write(byte[] data, Writer output)
0654:                    throws IOException {
0655:                if (data != null) {
0656:                    output.write(new String(data));
0657:                }
0658:            }
0659:
0660:            /**
0661:             * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
0662:             * using the specified character encoding.
0663:             * <p>
0664:             * Character encoding names can be found at
0665:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0666:             * <p>
0667:             * This method uses {@link String#String(byte[], String)}.
0668:             * 
0669:             * @param data  the byte array to write, do not modify during output,
0670:             * null ignored
0671:             * @param output  the <code>Writer</code> to write to
0672:             * @param encoding  the encoding to use, null means platform default
0673:             * @throws NullPointerException if output is null
0674:             * @throws IOException if an I/O error occurs
0675:             * @since Commons IO 1.1
0676:             */
0677:            public static void write(byte[] data, Writer output, String encoding)
0678:                    throws IOException {
0679:                if (data != null) {
0680:                    if (encoding == null) {
0681:                        write(data, output);
0682:                    } else {
0683:                        output.write(new String(data, encoding));
0684:                    }
0685:                }
0686:            }
0687:
0688:            // write char[]
0689:            //-----------------------------------------------------------------------
0690:            /**
0691:             * Writes chars from a <code>char[]</code> to a <code>Writer</code>
0692:             * using the default character encoding of the platform.
0693:             * 
0694:             * @param data  the char array to write, do not modify during output,
0695:             * null ignored
0696:             * @param output  the <code>Writer</code> to write to
0697:             * @throws NullPointerException if output is null
0698:             * @throws IOException if an I/O error occurs
0699:             * @since Commons IO 1.1
0700:             */
0701:            public static void write(char[] data, Writer output)
0702:                    throws IOException {
0703:                if (data != null) {
0704:                    output.write(data);
0705:                }
0706:            }
0707:
0708:            /**
0709:             * Writes chars from a <code>char[]</code> to bytes on an
0710:             * <code>OutputStream</code>.
0711:             * <p>
0712:             * This method uses {@link String#String(char[])} and
0713:             * {@link String#getBytes()}.
0714:             * 
0715:             * @param data  the char array to write, do not modify during output,
0716:             * null ignored
0717:             * @param output  the <code>OutputStream</code> to write to
0718:             * @throws NullPointerException if output is null
0719:             * @throws IOException if an I/O error occurs
0720:             * @since Commons IO 1.1
0721:             */
0722:            public static void write(char[] data, OutputStream output)
0723:                    throws IOException {
0724:                if (data != null) {
0725:                    output.write(new String(data).getBytes());
0726:                }
0727:            }
0728:
0729:            /**
0730:             * Writes chars from a <code>char[]</code> to bytes on an
0731:             * <code>OutputStream</code> using the specified character encoding.
0732:             * <p>
0733:             * Character encoding names can be found at
0734:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0735:             * <p>
0736:             * This method uses {@link String#String(char[])} and
0737:             * {@link String#getBytes(String)}.
0738:             * 
0739:             * @param data  the char array to write, do not modify during output,
0740:             * null ignored
0741:             * @param output  the <code>OutputStream</code> to write to
0742:             * @param encoding  the encoding to use, null means platform default
0743:             * @throws NullPointerException if output is null
0744:             * @throws IOException if an I/O error occurs
0745:             * @since Commons IO 1.1
0746:             */
0747:            public static void write(char[] data, OutputStream output,
0748:                    String encoding) throws IOException {
0749:                if (data != null) {
0750:                    if (encoding == null) {
0751:                        write(data, output);
0752:                    } else {
0753:                        output.write(new String(data).getBytes(encoding));
0754:                    }
0755:                }
0756:            }
0757:
0758:            // write String
0759:            //-----------------------------------------------------------------------
0760:            /**
0761:             * Writes chars from a <code>String</code> to a <code>Writer</code>.
0762:             * 
0763:             * @param data  the <code>String</code> to write, null ignored
0764:             * @param output  the <code>Writer</code> to write to
0765:             * @throws NullPointerException if output is null
0766:             * @throws IOException if an I/O error occurs
0767:             * @since Commons IO 1.1
0768:             */
0769:            public static void write(String data, Writer output)
0770:                    throws IOException {
0771:                if (data != null) {
0772:                    output.write(data);
0773:                }
0774:            }
0775:
0776:            /**
0777:             * Writes chars from a <code>String</code> to bytes on an
0778:             * <code>OutputStream</code> using the default character encoding of the
0779:             * platform.
0780:             * <p>
0781:             * This method uses {@link String#getBytes()}.
0782:             * 
0783:             * @param data  the <code>String</code> to write, null ignored
0784:             * @param output  the <code>OutputStream</code> to write to
0785:             * @throws NullPointerException if output is null
0786:             * @throws IOException if an I/O error occurs
0787:             * @since Commons IO 1.1
0788:             */
0789:            public static void write(String data, OutputStream output)
0790:                    throws IOException {
0791:                if (data != null) {
0792:                    output.write(data.getBytes());
0793:                }
0794:            }
0795:
0796:            /**
0797:             * Writes chars from a <code>String</code> to bytes on an
0798:             * <code>OutputStream</code> using the specified character encoding.
0799:             * <p>
0800:             * Character encoding names can be found at
0801:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0802:             * <p>
0803:             * This method uses {@link String#getBytes(String)}.
0804:             * 
0805:             * @param data  the <code>String</code> to write, null ignored
0806:             * @param output  the <code>OutputStream</code> to write to
0807:             * @param encoding  the encoding to use, null means platform default
0808:             * @throws NullPointerException if output is null
0809:             * @throws IOException if an I/O error occurs
0810:             * @since Commons IO 1.1
0811:             */
0812:            public static void write(String data, OutputStream output,
0813:                    String encoding) throws IOException {
0814:                if (data != null) {
0815:                    if (encoding == null) {
0816:                        write(data, output);
0817:                    } else {
0818:                        output.write(data.getBytes(encoding));
0819:                    }
0820:                }
0821:            }
0822:
0823:            // write StringBuffer
0824:            //-----------------------------------------------------------------------
0825:            /**
0826:             * Writes chars from a <code>StringBuffer</code> to a <code>Writer</code>.
0827:             * 
0828:             * @param data  the <code>StringBuffer</code> to write, null ignored
0829:             * @param output  the <code>Writer</code> to write to
0830:             * @throws NullPointerException if output is null
0831:             * @throws IOException if an I/O error occurs
0832:             * @since Commons IO 1.1
0833:             */
0834:            public static void write(StringBuffer data, Writer output)
0835:                    throws IOException {
0836:                if (data != null) {
0837:                    output.write(data.toString());
0838:                }
0839:            }
0840:
0841:            /**
0842:             * Writes chars from a <code>StringBuffer</code> to bytes on an
0843:             * <code>OutputStream</code> using the default character encoding of the
0844:             * platform.
0845:             * <p>
0846:             * This method uses {@link String#getBytes()}.
0847:             * 
0848:             * @param data  the <code>StringBuffer</code> to write, null ignored
0849:             * @param output  the <code>OutputStream</code> to write to
0850:             * @throws NullPointerException if output is null
0851:             * @throws IOException if an I/O error occurs
0852:             * @since Commons IO 1.1
0853:             */
0854:            public static void write(StringBuffer data, OutputStream output)
0855:                    throws IOException {
0856:                if (data != null) {
0857:                    output.write(data.toString().getBytes());
0858:                }
0859:            }
0860:
0861:            /**
0862:             * Writes chars from a <code>StringBuffer</code> to bytes on an
0863:             * <code>OutputStream</code> using the specified character encoding.
0864:             * <p>
0865:             * Character encoding names can be found at
0866:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0867:             * <p>
0868:             * This method uses {@link String#getBytes(String)}.
0869:             * 
0870:             * @param data  the <code>StringBuffer</code> to write, null ignored
0871:             * @param output  the <code>OutputStream</code> to write to
0872:             * @param encoding  the encoding to use, null means platform default
0873:             * @throws NullPointerException if output is null
0874:             * @throws IOException if an I/O error occurs
0875:             * @since Commons IO 1.1
0876:             */
0877:            public static void write(StringBuffer data, OutputStream output,
0878:                    String encoding) throws IOException {
0879:                if (data != null) {
0880:                    if (encoding == null) {
0881:                        write(data, output);
0882:                    } else {
0883:                        output.write(data.toString().getBytes(encoding));
0884:                    }
0885:                }
0886:            }
0887:
0888:            // writeLines
0889:            //-----------------------------------------------------------------------
0890:            /**
0891:             * Writes the <code>toString()</code> value of each item in a collection to
0892:             * an <code>OutputStream</code> line by line, using the default character
0893:             * encoding of the platform and the specified line ending.
0894:             *
0895:             * @param lines  the lines to write, null entries produce blank lines
0896:             * @param lineEnding  the line separator to use, null is system default
0897:             * @param output  the <code>OutputStream</code> to write to, not null, not closed
0898:             * @throws NullPointerException if the output is null
0899:             * @throws IOException if an I/O error occurs
0900:             * @since Commons IO 1.1
0901:             */
0902:            public static void writeLines(Collection lines, String lineEnding,
0903:                    OutputStream output) throws IOException {
0904:                if (lines == null) {
0905:                    return;
0906:                }
0907:                if (lineEnding == null) {
0908:                    lineEnding = LINE_SEPARATOR;
0909:                }
0910:                for (Iterator it = lines.iterator(); it.hasNext();) {
0911:                    Object line = it.next();
0912:                    if (line != null) {
0913:                        output.write(line.toString().getBytes());
0914:                    }
0915:                    output.write(lineEnding.getBytes());
0916:                }
0917:            }
0918:
0919:            /**
0920:             * Writes the <code>toString()</code> value of each item in a collection to
0921:             * an <code>OutputStream</code> line by line, using the specified character
0922:             * encoding and the specified line ending.
0923:             * <p>
0924:             * Character encoding names can be found at
0925:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0926:             *
0927:             * @param lines  the lines to write, null entries produce blank lines
0928:             * @param lineEnding  the line separator to use, null is system default
0929:             * @param output  the <code>OutputStream</code> to write to, not null, not closed
0930:             * @param encoding  the encoding to use, null means platform default
0931:             * @throws NullPointerException if the output is null
0932:             * @throws IOException if an I/O error occurs
0933:             * @since Commons IO 1.1
0934:             */
0935:            public static void writeLines(Collection lines, String lineEnding,
0936:                    OutputStream output, String encoding) throws IOException {
0937:                if (encoding == null) {
0938:                    writeLines(lines, lineEnding, output);
0939:                } else {
0940:                    if (lines == null) {
0941:                        return;
0942:                    }
0943:                    if (lineEnding == null) {
0944:                        lineEnding = LINE_SEPARATOR;
0945:                    }
0946:                    for (Iterator it = lines.iterator(); it.hasNext();) {
0947:                        Object line = it.next();
0948:                        if (line != null) {
0949:                            output.write(line.toString().getBytes(encoding));
0950:                        }
0951:                        output.write(lineEnding.getBytes(encoding));
0952:                    }
0953:                }
0954:            }
0955:
0956:            /**
0957:             * Writes the <code>toString()</code> value of each item in a collection to
0958:             * a <code>Writer</code> line by line, using the specified line ending.
0959:             *
0960:             * @param lines  the lines to write, null entries produce blank lines
0961:             * @param lineEnding  the line separator to use, null is system default
0962:             * @param writer  the <code>Writer</code> to write to, not null, not closed
0963:             * @throws NullPointerException if the input is null
0964:             * @throws IOException if an I/O error occurs
0965:             * @since Commons IO 1.1
0966:             */
0967:            public static void writeLines(Collection lines, String lineEnding,
0968:                    Writer writer) throws IOException {
0969:                if (lines == null) {
0970:                    return;
0971:                }
0972:                if (lineEnding == null) {
0973:                    lineEnding = LINE_SEPARATOR;
0974:                }
0975:                for (Iterator it = lines.iterator(); it.hasNext();) {
0976:                    Object line = it.next();
0977:                    if (line != null) {
0978:                        writer.write(line.toString());
0979:                    }
0980:                    writer.write(lineEnding);
0981:                }
0982:            }
0983:
0984:            // copy from InputStream
0985:            //-----------------------------------------------------------------------
0986:            /**
0987:             * Copy bytes from an <code>InputStream</code> to an
0988:             * <code>OutputStream</code>.
0989:             * <p>
0990:             * This method buffers the input internally, so there is no need to use a
0991:             * <code>BufferedInputStream</code>.
0992:             * <p>
0993:             * Large streams (over 2GB) will return a bytes copied value of
0994:             * <code>-1</code> after the copy has completed since the correct
0995:             * number of bytes cannot be returned as an int. For large streams
0996:             * use the <code>copyLarge(InputStream, OutputStream)</code> method.
0997:             * 
0998:             * @param input  the <code>InputStream</code> to read from
0999:             * @param output  the <code>OutputStream</code> to write to
1000:             * @return the number of bytes copied
1001:             * @throws NullPointerException if the input or output is null
1002:             * @throws IOException if an I/O error occurs
1003:             * @throws ArithmeticException if the byte count is too large
1004:             * @since Commons IO 1.1
1005:             */
1006:            public static int copy(InputStream input, OutputStream output)
1007:                    throws IOException {
1008:                long count = copyLarge(input, output);
1009:                if (count > Integer.MAX_VALUE) {
1010:                    return -1;
1011:                }
1012:                return (int) count;
1013:            }
1014:
1015:            /**
1016:             * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
1017:             * <code>OutputStream</code>.
1018:             * <p>
1019:             * This method buffers the input internally, so there is no need to use a
1020:             * <code>BufferedInputStream</code>.
1021:             * 
1022:             * @param input  the <code>InputStream</code> to read from
1023:             * @param output  the <code>OutputStream</code> to write to
1024:             * @return the number of bytes copied
1025:             * @throws NullPointerException if the input or output is null
1026:             * @throws IOException if an I/O error occurs
1027:             * @since Commons IO 1.3
1028:             */
1029:            public static long copyLarge(InputStream input, OutputStream output)
1030:                    throws IOException {
1031:                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
1032:                long count = 0;
1033:                int n = 0;
1034:                while (-1 != (n = input.read(buffer))) {
1035:                    output.write(buffer, 0, n);
1036:                    count += n;
1037:                }
1038:                return count;
1039:            }
1040:
1041:            /**
1042:             * Copy bytes from an <code>InputStream</code> to chars on a
1043:             * <code>Writer</code> using the default character encoding of the platform.
1044:             * <p>
1045:             * This method buffers the input internally, so there is no need to use a
1046:             * <code>BufferedInputStream</code>.
1047:             * <p>
1048:             * This method uses {@link InputStreamReader}.
1049:             *
1050:             * @param input  the <code>InputStream</code> to read from
1051:             * @param output  the <code>Writer</code> to write to
1052:             * @throws NullPointerException if the input or output is null
1053:             * @throws IOException if an I/O error occurs
1054:             * @since Commons IO 1.1
1055:             */
1056:            public static void copy(InputStream input, Writer output)
1057:                    throws IOException {
1058:                InputStreamReader in = new InputStreamReader(input);
1059:                copy(in, output);
1060:            }
1061:
1062:            /**
1063:             * Copy bytes from an <code>InputStream</code> to chars on a
1064:             * <code>Writer</code> using the specified character encoding.
1065:             * <p>
1066:             * This method buffers the input internally, so there is no need to use a
1067:             * <code>BufferedInputStream</code>.
1068:             * <p>
1069:             * Character encoding names can be found at
1070:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
1071:             * <p>
1072:             * This method uses {@link InputStreamReader}.
1073:             *
1074:             * @param input  the <code>InputStream</code> to read from
1075:             * @param output  the <code>Writer</code> to write to
1076:             * @param encoding  the encoding to use, null means platform default
1077:             * @throws NullPointerException if the input or output is null
1078:             * @throws IOException if an I/O error occurs
1079:             * @since Commons IO 1.1
1080:             */
1081:            public static void copy(InputStream input, Writer output,
1082:                    String encoding) throws IOException {
1083:                if (encoding == null) {
1084:                    copy(input, output);
1085:                } else {
1086:                    InputStreamReader in = new InputStreamReader(input,
1087:                            encoding);
1088:                    copy(in, output);
1089:                }
1090:            }
1091:
1092:            // copy from Reader
1093:            //-----------------------------------------------------------------------
1094:            /**
1095:             * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
1096:             * <p>
1097:             * This method buffers the input internally, so there is no need to use a
1098:             * <code>BufferedReader</code>.
1099:             * <p>
1100:             * Large streams (over 2GB) will return a chars copied value of
1101:             * <code>-1</code> after the copy has completed since the correct
1102:             * number of chars cannot be returned as an int. For large streams
1103:             * use the <code>copyLarge(Reader, Writer)</code> method.
1104:             *
1105:             * @param input  the <code>Reader</code> to read from
1106:             * @param output  the <code>Writer</code> to write to
1107:             * @return the number of characters copied
1108:             * @throws NullPointerException if the input or output is null
1109:             * @throws IOException if an I/O error occurs
1110:             * @throws ArithmeticException if the character count is too large
1111:             * @since Commons IO 1.1
1112:             */
1113:            public static int copy(Reader input, Writer output)
1114:                    throws IOException {
1115:                long count = copyLarge(input, output);
1116:                if (count > Integer.MAX_VALUE) {
1117:                    return -1;
1118:                }
1119:                return (int) count;
1120:            }
1121:
1122:            /**
1123:             * Copy chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>.
1124:             * <p>
1125:             * This method buffers the input internally, so there is no need to use a
1126:             * <code>BufferedReader</code>.
1127:             *
1128:             * @param input  the <code>Reader</code> to read from
1129:             * @param output  the <code>Writer</code> to write to
1130:             * @return the number of characters copied
1131:             * @throws NullPointerException if the input or output is null
1132:             * @throws IOException if an I/O error occurs
1133:             * @since Commons IO 1.3
1134:             */
1135:            public static long copyLarge(Reader input, Writer output)
1136:                    throws IOException {
1137:                char[] buffer = new char[DEFAULT_BUFFER_SIZE];
1138:                long count = 0;
1139:                int n = 0;
1140:                while (-1 != (n = input.read(buffer))) {
1141:                    output.write(buffer, 0, n);
1142:                    count += n;
1143:                }
1144:                return count;
1145:            }
1146:
1147:            /**
1148:             * Copy chars from a <code>Reader</code> to bytes on an
1149:             * <code>OutputStream</code> using the default character encoding of the
1150:             * platform, and calling flush.
1151:             * <p>
1152:             * This method buffers the input internally, so there is no need to use a
1153:             * <code>BufferedReader</code>.
1154:             * <p>
1155:             * Due to the implementation of OutputStreamWriter, this method performs a
1156:             * flush.
1157:             * <p>
1158:             * This method uses {@link OutputStreamWriter}.
1159:             *
1160:             * @param input  the <code>Reader</code> to read from
1161:             * @param output  the <code>OutputStream</code> to write to
1162:             * @throws NullPointerException if the input or output is null
1163:             * @throws IOException if an I/O error occurs
1164:             * @since Commons IO 1.1
1165:             */
1166:            public static void copy(Reader input, OutputStream output)
1167:                    throws IOException {
1168:                OutputStreamWriter out = new OutputStreamWriter(output);
1169:                copy(input, out);
1170:                // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
1171:                // have to flush here.
1172:                out.flush();
1173:            }
1174:
1175:            /**
1176:             * Copy chars from a <code>Reader</code> to bytes on an
1177:             * <code>OutputStream</code> using the specified character encoding, and
1178:             * calling flush.
1179:             * <p>
1180:             * This method buffers the input internally, so there is no need to use a
1181:             * <code>BufferedReader</code>.
1182:             * <p>
1183:             * Character encoding names can be found at
1184:             * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
1185:             * <p>
1186:             * Due to the implementation of OutputStreamWriter, this method performs a
1187:             * flush.
1188:             * <p>
1189:             * This method uses {@link OutputStreamWriter}.
1190:             *
1191:             * @param input  the <code>Reader</code> to read from
1192:             * @param output  the <code>OutputStream</code> to write to
1193:             * @param encoding  the encoding to use, null means platform default
1194:             * @throws NullPointerException if the input or output is null
1195:             * @throws IOException if an I/O error occurs
1196:             * @since Commons IO 1.1
1197:             */
1198:            public static void copy(Reader input, OutputStream output,
1199:                    String encoding) throws IOException {
1200:                if (encoding == null) {
1201:                    copy(input, output);
1202:                } else {
1203:                    OutputStreamWriter out = new OutputStreamWriter(output,
1204:                            encoding);
1205:                    copy(input, out);
1206:                    // XXX Unless anyone is planning on rewriting OutputStreamWriter,
1207:                    // we have to flush here.
1208:                    out.flush();
1209:                }
1210:            }
1211:
1212:            // content equals
1213:            //-----------------------------------------------------------------------
1214:            /**
1215:             * Compare the contents of two Streams to determine if they are equal or
1216:             * not.
1217:             * <p>
1218:             * This method buffers the input internally using
1219:             * <code>BufferedInputStream</code> if they are not already buffered.
1220:             *
1221:             * @param input1  the first stream
1222:             * @param input2  the second stream
1223:             * @return true if the content of the streams are equal or they both don't
1224:             * exist, false otherwise
1225:             * @throws NullPointerException if either input is null
1226:             * @throws IOException if an I/O error occurs
1227:             */
1228:            public static boolean contentEquals(InputStream input1,
1229:                    InputStream input2) throws IOException {
1230:                if (!(input1 instanceof  BufferedInputStream)) {
1231:                    input1 = new BufferedInputStream(input1);
1232:                }
1233:                if (!(input2 instanceof  BufferedInputStream)) {
1234:                    input2 = new BufferedInputStream(input2);
1235:                }
1236:
1237:                int ch = input1.read();
1238:                while (-1 != ch) {
1239:                    int ch2 = input2.read();
1240:                    if (ch != ch2) {
1241:                        return false;
1242:                    }
1243:                    ch = input1.read();
1244:                }
1245:
1246:                int ch2 = input2.read();
1247:                return (ch2 == -1);
1248:            }
1249:
1250:            /**
1251:             * Compare the contents of two Readers to determine if they are equal or
1252:             * not.
1253:             * <p>
1254:             * This method buffers the input internally using
1255:             * <code>BufferedReader</code> if they are not already buffered.
1256:             *
1257:             * @param input1  the first reader
1258:             * @param input2  the second reader
1259:             * @return true if the content of the readers are equal or they both don't
1260:             * exist, false otherwise
1261:             * @throws NullPointerException if either input is null
1262:             * @throws IOException if an I/O error occurs
1263:             * @since Commons IO 1.1
1264:             */
1265:            public static boolean contentEquals(Reader input1, Reader input2)
1266:                    throws IOException {
1267:                if (!(input1 instanceof  BufferedReader)) {
1268:                    input1 = new BufferedReader(input1);
1269:                }
1270:                if (!(input2 instanceof  BufferedReader)) {
1271:                    input2 = new BufferedReader(input2);
1272:                }
1273:
1274:                int ch = input1.read();
1275:                while (-1 != ch) {
1276:                    int ch2 = input2.read();
1277:                    if (ch != ch2) {
1278:                        return false;
1279:                    }
1280:                    ch = input1.read();
1281:                }
1282:
1283:                int ch2 = input2.read();
1284:                return (ch2 == -1);
1285:            }
1286:
1287:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.