0001: /*
0002: * JFox - The most lightweight Java EE Application Server!
0003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
0004: *
0005: * JFox is licenced and re-distributable under GNU LGPL.
0006: */
0007: package org.jfox.util;
0008:
0009: import java.io.BufferedInputStream;
0010: import java.io.BufferedReader;
0011: import java.io.ByteArrayInputStream;
0012: import java.io.ByteArrayOutputStream;
0013: import java.io.CharArrayWriter;
0014: import java.io.File;
0015: import java.io.IOException;
0016: import java.io.InputStream;
0017: import java.io.InputStreamReader;
0018: import java.io.OutputStream;
0019: import java.io.OutputStreamWriter;
0020: import java.io.PrintWriter;
0021: import java.io.Reader;
0022: import java.io.StringWriter;
0023: import java.io.Writer;
0024: import java.util.ArrayList;
0025: import java.util.Collection;
0026: import java.util.Iterator;
0027: import java.util.List;
0028:
0029: /**
0030: * IO �作帮助类,�自 jakarta commons-io
0031: * ä»…åˆ é™¤äº†ä¸¤ä¸ªéœ€è¦?用到 LineIterator 的方法
0032: *
0033: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
0034: */
0035:
0036: public class IOUtils {
0037:
0038: // NOTE: This class is focussed on InputStream, OutputStream, Reader and
0039: // Writer. Each method should take at least one of these as a parameter,
0040: // or return one of them.
0041:
0042: /**
0043: * The Unix directory separator character.
0044: */
0045: public static final char DIR_SEPARATOR_UNIX = '/';
0046: /**
0047: * The Windows directory separator character.
0048: */
0049: public static final char DIR_SEPARATOR_WINDOWS = '\\';
0050: /**
0051: * The system directory separator character.
0052: */
0053: public static final char DIR_SEPARATOR = File.separatorChar;
0054: /**
0055: * The Unix line separator string.
0056: */
0057: public static final String LINE_SEPARATOR_UNIX = "\n";
0058: /**
0059: * The Windows line separator string.
0060: */
0061: public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
0062: /**
0063: * The system line separator string.
0064: */
0065: public static final String LINE_SEPARATOR;
0066: static {
0067: // avoid security issues
0068: StringWriter buf = new StringWriter(4);
0069: PrintWriter out = new PrintWriter(buf);
0070: out.println();
0071: LINE_SEPARATOR = buf.toString();
0072: }
0073:
0074: /**
0075: * The default buffer size to use.
0076: */
0077: private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
0078:
0079: /**
0080: * Instances should NOT be constructed in standard programming.
0081: */
0082: private IOUtils() {
0083: super ();
0084: }
0085:
0086: //-----------------------------------------------------------------------
0087: /**
0088: * Unconditionally close an <code>Reader</code>.
0089: * <p>
0090: * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
0091: * This is typically used in finally blocks.
0092: *
0093: * @param input the Reader to close, may be null or already closed
0094: */
0095: public static void closeQuietly(Reader input) {
0096: try {
0097: if (input != null) {
0098: input.close();
0099: }
0100: } catch (IOException ioe) {
0101: // ignore
0102: }
0103: }
0104:
0105: /**
0106: * Unconditionally close a <code>Writer</code>.
0107: * <p>
0108: * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
0109: * This is typically used in finally blocks.
0110: *
0111: * @param output the Writer to close, may be null or already closed
0112: */
0113: public static void closeQuietly(Writer output) {
0114: try {
0115: if (output != null) {
0116: output.close();
0117: }
0118: } catch (IOException ioe) {
0119: // ignore
0120: }
0121: }
0122:
0123: /**
0124: * Unconditionally close an <code>InputStream</code>.
0125: * <p>
0126: * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
0127: * This is typically used in finally blocks.
0128: *
0129: * @param input the InputStream to close, may be null or already closed
0130: */
0131: public static void closeQuietly(InputStream input) {
0132: try {
0133: if (input != null) {
0134: input.close();
0135: }
0136: } catch (IOException ioe) {
0137: // ignore
0138: }
0139: }
0140:
0141: /**
0142: * Unconditionally close an <code>OutputStream</code>.
0143: * <p>
0144: * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
0145: * This is typically used in finally blocks.
0146: *
0147: * @param output the OutputStream to close, may be null or already closed
0148: */
0149: public static void closeQuietly(OutputStream output) {
0150: try {
0151: if (output != null) {
0152: output.close();
0153: }
0154: } catch (IOException ioe) {
0155: // ignore
0156: }
0157: }
0158:
0159: // read toByteArray
0160: //-----------------------------------------------------------------------
0161: /**
0162: * Get the contents of an <code>InputStream</code> as a <code>byte[]</code>.
0163: * <p>
0164: * This method buffers the input internally, so there is no need to use a
0165: * <code>BufferedInputStream</code>.
0166: *
0167: * @param input the <code>InputStream</code> to read from
0168: * @return the requested byte array
0169: * @throws NullPointerException if the input is null
0170: * @throws IOException if an I/O error occurs
0171: */
0172: public static byte[] toByteArray(InputStream input)
0173: throws IOException {
0174: ByteArrayOutputStream output = new ByteArrayOutputStream();
0175: copy(input, output);
0176: return output.toByteArray();
0177: }
0178:
0179: /**
0180: * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
0181: * using the default character encoding of the platform.
0182: * <p>
0183: * This method buffers the input internally, so there is no need to use a
0184: * <code>BufferedReader</code>.
0185: *
0186: * @param input the <code>Reader</code> to read from
0187: * @return the requested byte array
0188: * @throws NullPointerException if the input is null
0189: * @throws IOException if an I/O error occurs
0190: */
0191: public static byte[] toByteArray(Reader input) throws IOException {
0192: ByteArrayOutputStream output = new ByteArrayOutputStream();
0193: copy(input, output);
0194: return output.toByteArray();
0195: }
0196:
0197: /**
0198: * Get the contents of a <code>Reader</code> as a <code>byte[]</code>
0199: * using the specified character encoding.
0200: * <p>
0201: * Character encoding names can be found at
0202: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0203: * <p>
0204: * This method buffers the input internally, so there is no need to use a
0205: * <code>BufferedReader</code>.
0206: *
0207: * @param input the <code>Reader</code> to read from
0208: * @param encoding the encoding to use, null means platform default
0209: * @return the requested byte array
0210: * @throws NullPointerException if the input is null
0211: * @throws IOException if an I/O error occurs
0212: * @since Commons IO 1.1
0213: */
0214: public static byte[] toByteArray(Reader input, String encoding)
0215: throws IOException {
0216: ByteArrayOutputStream output = new ByteArrayOutputStream();
0217: copy(input, output, encoding);
0218: return output.toByteArray();
0219: }
0220:
0221: /**
0222: * Get the contents of a <code>String</code> as a <code>byte[]</code>
0223: * using the default character encoding of the platform.
0224: * <p>
0225: * This is the same as {@link String#getBytes()}.
0226: *
0227: * @param input the <code>String</code> to convert
0228: * @return the requested byte array
0229: * @throws NullPointerException if the input is null
0230: * @throws IOException if an I/O error occurs (never occurs)
0231: * @deprecated Use {@link String#getBytes()}
0232: */
0233: public static byte[] toByteArray(String input) throws IOException {
0234: return input.getBytes();
0235: }
0236:
0237: // read char[]
0238: //-----------------------------------------------------------------------
0239: /**
0240: * Get the contents of an <code>InputStream</code> as a character array
0241: * using the default character encoding of the platform.
0242: * <p>
0243: * This method buffers the input internally, so there is no need to use a
0244: * <code>BufferedInputStream</code>.
0245: *
0246: * @param is the <code>InputStream</code> to read from
0247: * @return the requested character array
0248: * @throws NullPointerException if the input is null
0249: * @throws IOException if an I/O error occurs
0250: * @since Commons IO 1.1
0251: */
0252: public static char[] toCharArray(InputStream is) throws IOException {
0253: CharArrayWriter output = new CharArrayWriter();
0254: copy(is, output);
0255: return output.toCharArray();
0256: }
0257:
0258: /**
0259: * Get the contents of an <code>InputStream</code> as a character array
0260: * using the specified character encoding.
0261: * <p>
0262: * Character encoding names can be found at
0263: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0264: * <p>
0265: * This method buffers the input internally, so there is no need to use a
0266: * <code>BufferedInputStream</code>.
0267: *
0268: * @param is the <code>InputStream</code> to read from
0269: * @param encoding the encoding to use, null means platform default
0270: * @return the requested character array
0271: * @throws NullPointerException if the input is null
0272: * @throws IOException if an I/O error occurs
0273: * @since Commons IO 1.1
0274: */
0275: public static char[] toCharArray(InputStream is, String encoding)
0276: throws IOException {
0277: CharArrayWriter output = new CharArrayWriter();
0278: copy(is, output, encoding);
0279: return output.toCharArray();
0280: }
0281:
0282: /**
0283: * Get the contents of a <code>Reader</code> as a character array.
0284: * <p>
0285: * This method buffers the input internally, so there is no need to use a
0286: * <code>BufferedReader</code>.
0287: *
0288: * @param input the <code>Reader</code> to read from
0289: * @return the requested character array
0290: * @throws NullPointerException if the input is null
0291: * @throws IOException if an I/O error occurs
0292: * @since Commons IO 1.1
0293: */
0294: public static char[] toCharArray(Reader input) throws IOException {
0295: CharArrayWriter sw = new CharArrayWriter();
0296: copy(input, sw);
0297: return sw.toCharArray();
0298: }
0299:
0300: // read toString
0301: //-----------------------------------------------------------------------
0302: /**
0303: * Get the contents of an <code>InputStream</code> as a String
0304: * using the default character encoding of the platform.
0305: * <p>
0306: * This method buffers the input internally, so there is no need to use a
0307: * <code>BufferedInputStream</code>.
0308: *
0309: * @param input the <code>InputStream</code> to read from
0310: * @return the requested String
0311: * @throws NullPointerException if the input is null
0312: * @throws IOException if an I/O error occurs
0313: */
0314: public static String toString(InputStream input) throws IOException {
0315: StringWriter sw = new StringWriter();
0316: copy(input, sw);
0317: return sw.toString();
0318: }
0319:
0320: /**
0321: * Get the contents of an <code>InputStream</code> as a String
0322: * using the specified character encoding.
0323: * <p>
0324: * Character encoding names can be found at
0325: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0326: * <p>
0327: * This method buffers the input internally, so there is no need to use a
0328: * <code>BufferedInputStream</code>.
0329: *
0330: * @param input the <code>InputStream</code> to read from
0331: * @param encoding the encoding to use, null means platform default
0332: * @return the requested String
0333: * @throws NullPointerException if the input is null
0334: * @throws IOException if an I/O error occurs
0335: */
0336: public static String toString(InputStream input, String encoding)
0337: throws IOException {
0338: StringWriter sw = new StringWriter();
0339: copy(input, sw, encoding);
0340: return sw.toString();
0341: }
0342:
0343: /**
0344: * Get the contents of a <code>Reader</code> as a String.
0345: * <p>
0346: * This method buffers the input internally, so there is no need to use a
0347: * <code>BufferedReader</code>.
0348: *
0349: * @param input the <code>Reader</code> to read from
0350: * @return the requested String
0351: * @throws NullPointerException if the input is null
0352: * @throws IOException if an I/O error occurs
0353: */
0354: public static String toString(Reader input) throws IOException {
0355: StringWriter sw = new StringWriter();
0356: copy(input, sw);
0357: return sw.toString();
0358: }
0359:
0360: /**
0361: * Get the contents of a <code>byte[]</code> as a String
0362: * using the default character encoding of the platform.
0363: *
0364: * @param input the byte array to read from
0365: * @return the requested String
0366: * @throws NullPointerException if the input is null
0367: * @throws IOException if an I/O error occurs (never occurs)
0368: * @deprecated Use {@link String#String(byte[])}
0369: */
0370: public static String toString(byte[] input) throws IOException {
0371: return new String(input);
0372: }
0373:
0374: /**
0375: * Get the contents of a <code>byte[]</code> as a String
0376: * using the specified character encoding.
0377: * <p>
0378: * Character encoding names can be found at
0379: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0380: *
0381: * @param input the byte array to read from
0382: * @param encoding the encoding to use, null means platform default
0383: * @return the requested String
0384: * @throws NullPointerException if the input is null
0385: * @throws IOException if an I/O error occurs (never occurs)
0386: * @deprecated Use {@link String#String(byte[],String)}
0387: */
0388: public static String toString(byte[] input, String encoding)
0389: throws IOException {
0390: if (encoding == null) {
0391: return new String(input);
0392: } else {
0393: return new String(input, encoding);
0394: }
0395: }
0396:
0397: // readLines
0398: //-----------------------------------------------------------------------
0399: /**
0400: * Get the contents of an <code>InputStream</code> as a list of Strings,
0401: * one entry per line, using the default character encoding of the platform.
0402: * <p>
0403: * This method buffers the input internally, so there is no need to use a
0404: * <code>BufferedInputStream</code>.
0405: *
0406: * @param input the <code>InputStream</code> to read from, not null
0407: * @return the list of Strings, never null
0408: * @throws NullPointerException if the input is null
0409: * @throws IOException if an I/O error occurs
0410: * @since Commons IO 1.1
0411: */
0412: public static List readLines(InputStream input) throws IOException {
0413: InputStreamReader reader = new InputStreamReader(input);
0414: return readLines(reader);
0415: }
0416:
0417: /**
0418: * Get the contents of an <code>InputStream</code> as a list of Strings,
0419: * one entry per line, using the specified character encoding.
0420: * <p>
0421: * Character encoding names can be found at
0422: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0423: * <p>
0424: * This method buffers the input internally, so there is no need to use a
0425: * <code>BufferedInputStream</code>.
0426: *
0427: * @param input the <code>InputStream</code> to read from, not null
0428: * @param encoding the encoding to use, null means platform default
0429: * @return the list of Strings, never null
0430: * @throws NullPointerException if the input is null
0431: * @throws IOException if an I/O error occurs
0432: * @since Commons IO 1.1
0433: */
0434: public static List readLines(InputStream input, String encoding)
0435: throws IOException {
0436: if (encoding == null) {
0437: return readLines(input);
0438: } else {
0439: InputStreamReader reader = new InputStreamReader(input,
0440: encoding);
0441: return readLines(reader);
0442: }
0443: }
0444:
0445: /**
0446: * Get the contents of a <code>Reader</code> as a list of Strings,
0447: * one entry per line.
0448: * <p>
0449: * This method buffers the input internally, so there is no need to use a
0450: * <code>BufferedReader</code>.
0451: *
0452: * @param input the <code>Reader</code> to read from, not null
0453: * @return the list of Strings, never null
0454: * @throws NullPointerException if the input is null
0455: * @throws IOException if an I/O error occurs
0456: * @since Commons IO 1.1
0457: */
0458: public static List readLines(Reader input) throws IOException {
0459: BufferedReader reader = new BufferedReader(input);
0460: List list = new ArrayList();
0461: String line = reader.readLine();
0462: while (line != null) {
0463: list.add(line);
0464: line = reader.readLine();
0465: }
0466: return list;
0467: }
0468:
0469: //-----------------------------------------------------------------------
0470: /**
0471: * Convert the specified string to an input stream, encoded as bytes
0472: * using the default character encoding of the platform.
0473: *
0474: * @param input the string to convert
0475: * @return an input stream
0476: * @since Commons IO 1.1
0477: */
0478: public static InputStream toInputStream(String input) {
0479: byte[] bytes = input.getBytes();
0480: return new ByteArrayInputStream(bytes);
0481: }
0482:
0483: /**
0484: * Convert the specified string to an input stream, encoded as bytes
0485: * using the specified character encoding.
0486: * <p>
0487: * Character encoding names can be found at
0488: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0489: *
0490: * @param input the string to convert
0491: * @param encoding the encoding to use, null means platform default
0492: * @throws IOException if the encoding is invalid
0493: * @return an input stream
0494: * @since Commons IO 1.1
0495: */
0496: public static InputStream toInputStream(String input,
0497: String encoding) throws IOException {
0498: byte[] bytes = encoding != null ? input.getBytes(encoding)
0499: : input.getBytes();
0500: return new ByteArrayInputStream(bytes);
0501: }
0502:
0503: // write byte[]
0504: //-----------------------------------------------------------------------
0505: /**
0506: * Writes bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
0507: *
0508: * @param data the byte array to write, do not modify during output,
0509: * null ignored
0510: * @param output the <code>OutputStream</code> to write to
0511: * @throws NullPointerException if output is null
0512: * @throws IOException if an I/O error occurs
0513: * @since Commons IO 1.1
0514: */
0515: public static void write(byte[] data, OutputStream output)
0516: throws IOException {
0517: if (data != null) {
0518: output.write(data);
0519: }
0520: }
0521:
0522: /**
0523: * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
0524: * using the default character encoding of the platform.
0525: * <p>
0526: * This method uses {@link String#String(byte[])}.
0527: *
0528: * @param data the byte array to write, do not modify during output,
0529: * null ignored
0530: * @param output the <code>Writer</code> to write to
0531: * @throws NullPointerException if output is null
0532: * @throws IOException if an I/O error occurs
0533: * @since Commons IO 1.1
0534: */
0535: public static void write(byte[] data, Writer output)
0536: throws IOException {
0537: if (data != null) {
0538: output.write(new String(data));
0539: }
0540: }
0541:
0542: /**
0543: * Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
0544: * using the specified character encoding.
0545: * <p>
0546: * Character encoding names can be found at
0547: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0548: * <p>
0549: * This method uses {@link String#String(byte[], String)}.
0550: *
0551: * @param data the byte array to write, do not modify during output,
0552: * null ignored
0553: * @param output the <code>Writer</code> to write to
0554: * @param encoding the encoding to use, null means platform default
0555: * @throws NullPointerException if output is null
0556: * @throws IOException if an I/O error occurs
0557: * @since Commons IO 1.1
0558: */
0559: public static void write(byte[] data, Writer output, String encoding)
0560: throws IOException {
0561: if (data != null) {
0562: if (encoding == null) {
0563: write(data, output);
0564: } else {
0565: output.write(new String(data, encoding));
0566: }
0567: }
0568: }
0569:
0570: // write char[]
0571: //-----------------------------------------------------------------------
0572: /**
0573: * Writes chars from a <code>char[]</code> to a <code>Writer</code>
0574: * using the default character encoding of the platform.
0575: *
0576: * @param data the char array to write, do not modify during output,
0577: * null ignored
0578: * @param output the <code>Writer</code> to write to
0579: * @throws NullPointerException if output is null
0580: * @throws IOException if an I/O error occurs
0581: * @since Commons IO 1.1
0582: */
0583: public static void write(char[] data, Writer output)
0584: throws IOException {
0585: if (data != null) {
0586: output.write(data);
0587: }
0588: }
0589:
0590: /**
0591: * Writes chars from a <code>char[]</code> to bytes on an
0592: * <code>OutputStream</code>.
0593: * <p>
0594: * This method uses {@link String#String(char[])} and
0595: * {@link String#getBytes()}.
0596: *
0597: * @param data the char array to write, do not modify during output,
0598: * null ignored
0599: * @param output the <code>OutputStream</code> to write to
0600: * @throws NullPointerException if output is null
0601: * @throws IOException if an I/O error occurs
0602: * @since Commons IO 1.1
0603: */
0604: public static void write(char[] data, OutputStream output)
0605: throws IOException {
0606: if (data != null) {
0607: output.write(new String(data).getBytes());
0608: }
0609: }
0610:
0611: /**
0612: * Writes chars from a <code>char[]</code> to bytes on an
0613: * <code>OutputStream</code> using the specified character encoding.
0614: * <p>
0615: * Character encoding names can be found at
0616: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0617: * <p>
0618: * This method uses {@link String#String(char[])} and
0619: * {@link String#getBytes(String)}.
0620: *
0621: * @param data the char array to write, do not modify during output,
0622: * null ignored
0623: * @param output the <code>OutputStream</code> to write to
0624: * @param encoding the encoding to use, null means platform default
0625: * @throws NullPointerException if output is null
0626: * @throws IOException if an I/O error occurs
0627: * @since Commons IO 1.1
0628: */
0629: public static void write(char[] data, OutputStream output,
0630: String encoding) throws IOException {
0631: if (data != null) {
0632: if (encoding == null) {
0633: write(data, output);
0634: } else {
0635: output.write(new String(data).getBytes(encoding));
0636: }
0637: }
0638: }
0639:
0640: // write String
0641: //-----------------------------------------------------------------------
0642: /**
0643: * Writes chars from a <code>String</code> to a <code>Writer</code>.
0644: *
0645: * @param data the <code>String</code> to write, null ignored
0646: * @param output the <code>Writer</code> to write to
0647: * @throws NullPointerException if output is null
0648: * @throws IOException if an I/O error occurs
0649: * @since Commons IO 1.1
0650: */
0651: public static void write(String data, Writer output)
0652: throws IOException {
0653: if (data != null) {
0654: output.write(data);
0655: }
0656: }
0657:
0658: /**
0659: * Writes chars from a <code>String</code> to bytes on an
0660: * <code>OutputStream</code> using the default character encoding of the
0661: * platform.
0662: * <p>
0663: * This method uses {@link String#getBytes()}.
0664: *
0665: * @param data the <code>String</code> to write, null ignored
0666: * @param output the <code>OutputStream</code> to write to
0667: * @throws NullPointerException if output is null
0668: * @throws IOException if an I/O error occurs
0669: * @since Commons IO 1.1
0670: */
0671: public static void write(String data, OutputStream output)
0672: throws IOException {
0673: if (data != null) {
0674: output.write(data.getBytes());
0675: }
0676: }
0677:
0678: /**
0679: * Writes chars from a <code>String</code> to bytes on an
0680: * <code>OutputStream</code> using the specified character encoding.
0681: * <p>
0682: * Character encoding names can be found at
0683: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0684: * <p>
0685: * This method uses {@link String#getBytes(String)}.
0686: *
0687: * @param data the <code>String</code> to write, null ignored
0688: * @param output the <code>OutputStream</code> to write to
0689: * @param encoding the encoding to use, null means platform default
0690: * @throws NullPointerException if output is null
0691: * @throws IOException if an I/O error occurs
0692: * @since Commons IO 1.1
0693: */
0694: public static void write(String data, OutputStream output,
0695: String encoding) throws IOException {
0696: if (data != null) {
0697: if (encoding == null) {
0698: write(data, output);
0699: } else {
0700: output.write(data.getBytes(encoding));
0701: }
0702: }
0703: }
0704:
0705: // write StringBuffer
0706: //-----------------------------------------------------------------------
0707: /**
0708: * Writes chars from a <code>StringBuffer</code> to a <code>Writer</code>.
0709: *
0710: * @param data the <code>StringBuffer</code> to write, null ignored
0711: * @param output the <code>Writer</code> to write to
0712: * @throws NullPointerException if output is null
0713: * @throws IOException if an I/O error occurs
0714: * @since Commons IO 1.1
0715: */
0716: public static void write(StringBuffer data, Writer output)
0717: throws IOException {
0718: if (data != null) {
0719: output.write(data.toString());
0720: }
0721: }
0722:
0723: /**
0724: * Writes chars from a <code>StringBuffer</code> to bytes on an
0725: * <code>OutputStream</code> using the default character encoding of the
0726: * platform.
0727: * <p>
0728: * This method uses {@link String#getBytes()}.
0729: *
0730: * @param data the <code>StringBuffer</code> to write, null ignored
0731: * @param output the <code>OutputStream</code> to write to
0732: * @throws NullPointerException if output is null
0733: * @throws IOException if an I/O error occurs
0734: * @since Commons IO 1.1
0735: */
0736: public static void write(StringBuffer data, OutputStream output)
0737: throws IOException {
0738: if (data != null) {
0739: output.write(data.toString().getBytes());
0740: }
0741: }
0742:
0743: /**
0744: * Writes chars from a <code>StringBuffer</code> to bytes on an
0745: * <code>OutputStream</code> using the specified character encoding.
0746: * <p>
0747: * Character encoding names can be found at
0748: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0749: * <p>
0750: * This method uses {@link String#getBytes(String)}.
0751: *
0752: * @param data the <code>StringBuffer</code> to write, null ignored
0753: * @param output the <code>OutputStream</code> to write to
0754: * @param encoding the encoding to use, null means platform default
0755: * @throws NullPointerException if output is null
0756: * @throws IOException if an I/O error occurs
0757: * @since Commons IO 1.1
0758: */
0759: public static void write(StringBuffer data, OutputStream output,
0760: String encoding) throws IOException {
0761: if (data != null) {
0762: if (encoding == null) {
0763: write(data, output);
0764: } else {
0765: output.write(data.toString().getBytes(encoding));
0766: }
0767: }
0768: }
0769:
0770: // writeLines
0771: //-----------------------------------------------------------------------
0772: /**
0773: * Writes the <code>toString()</code> value of each item in a collection to
0774: * an <code>OutputStream</code> line by line, using the default character
0775: * encoding of the platform and the specified line ending.
0776: *
0777: * @param lines the lines to write, null entries produce blank lines
0778: * @param lineEnding the line separator to use, null is system default
0779: * @param output the <code>OutputStream</code> to write to, not null, not closed
0780: * @throws NullPointerException if the output is null
0781: * @throws IOException if an I/O error occurs
0782: * @since Commons IO 1.1
0783: */
0784: public static void writeLines(Collection lines, String lineEnding,
0785: OutputStream output) throws IOException {
0786: if (lines == null) {
0787: return;
0788: }
0789: if (lineEnding == null) {
0790: lineEnding = LINE_SEPARATOR;
0791: }
0792: for (Iterator it = lines.iterator(); it.hasNext();) {
0793: Object line = it.next();
0794: if (line != null) {
0795: output.write(line.toString().getBytes());
0796: }
0797: output.write(lineEnding.getBytes());
0798: }
0799: }
0800:
0801: /**
0802: * Writes the <code>toString()</code> value of each item in a collection to
0803: * an <code>OutputStream</code> line by line, using the specified character
0804: * encoding and the specified line ending.
0805: * <p>
0806: * Character encoding names can be found at
0807: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0808: *
0809: * @param lines the lines to write, null entries produce blank lines
0810: * @param lineEnding the line separator to use, null is system default
0811: * @param output the <code>OutputStream</code> to write to, not null, not closed
0812: * @param encoding the encoding to use, null means platform default
0813: * @throws NullPointerException if the output is null
0814: * @throws IOException if an I/O error occurs
0815: * @since Commons IO 1.1
0816: */
0817: public static void writeLines(Collection lines, String lineEnding,
0818: OutputStream output, String encoding) throws IOException {
0819: if (encoding == null) {
0820: writeLines(lines, lineEnding, output);
0821: } else {
0822: if (lines == null) {
0823: return;
0824: }
0825: if (lineEnding == null) {
0826: lineEnding = LINE_SEPARATOR;
0827: }
0828: for (Iterator it = lines.iterator(); it.hasNext();) {
0829: Object line = it.next();
0830: if (line != null) {
0831: output.write(line.toString().getBytes(encoding));
0832: }
0833: output.write(lineEnding.getBytes(encoding));
0834: }
0835: }
0836: }
0837:
0838: /**
0839: * Writes the <code>toString()</code> value of each item in a collection to
0840: * a <code>Writer</code> line by line, using the specified line ending.
0841: *
0842: * @param lines the lines to write, null entries produce blank lines
0843: * @param lineEnding the line separator to use, null is system default
0844: * @param writer the <code>Writer</code> to write to, not null, not closed
0845: * @throws NullPointerException if the input is null
0846: * @throws IOException if an I/O error occurs
0847: * @since Commons IO 1.1
0848: */
0849: public static void writeLines(Collection lines, String lineEnding,
0850: Writer writer) throws IOException {
0851: if (lines == null) {
0852: return;
0853: }
0854: if (lineEnding == null) {
0855: lineEnding = LINE_SEPARATOR;
0856: }
0857: for (Iterator it = lines.iterator(); it.hasNext();) {
0858: Object line = it.next();
0859: if (line != null) {
0860: writer.write(line.toString());
0861: }
0862: writer.write(lineEnding);
0863: }
0864: }
0865:
0866: // copy from InputStream
0867: //-----------------------------------------------------------------------
0868: /**
0869: * Copy bytes from an <code>InputStream</code> to an
0870: * <code>OutputStream</code>.
0871: * <p>
0872: * This method buffers the input internally, so there is no need to use a
0873: * <code>BufferedInputStream</code>.
0874: *
0875: * @param input the <code>InputStream</code> to read from
0876: * @param output the <code>OutputStream</code> to write to
0877: * @return the number of bytes copied
0878: * @throws NullPointerException if the input or output is null
0879: * @throws IOException if an I/O error occurs
0880: * @since Commons IO 1.1
0881: */
0882: public static int copy(InputStream input, OutputStream output)
0883: throws IOException {
0884: byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
0885: int count = 0;
0886: int n = 0;
0887: while (-1 != (n = input.read(buffer))) {
0888: output.write(buffer, 0, n);
0889: count += n;
0890: }
0891: return count;
0892: }
0893:
0894: /**
0895: * Copy bytes from an <code>InputStream</code> to chars on a
0896: * <code>Writer</code> using the default character encoding of the platform.
0897: * <p>
0898: * This method buffers the input internally, so there is no need to use a
0899: * <code>BufferedInputStream</code>.
0900: * <p>
0901: * This method uses {@link InputStreamReader}.
0902: *
0903: * @param input the <code>InputStream</code> to read from
0904: * @param output the <code>Writer</code> to write to
0905: * @throws NullPointerException if the input or output is null
0906: * @throws IOException if an I/O error occurs
0907: * @since Commons IO 1.1
0908: */
0909: public static void copy(InputStream input, Writer output)
0910: throws IOException {
0911: InputStreamReader in = new InputStreamReader(input);
0912: copy(in, output);
0913: }
0914:
0915: /**
0916: * Copy bytes from an <code>InputStream</code> to chars on a
0917: * <code>Writer</code> using the specified character encoding.
0918: * <p>
0919: * This method buffers the input internally, so there is no need to use a
0920: * <code>BufferedInputStream</code>.
0921: * <p>
0922: * Character encoding names can be found at
0923: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
0924: * <p>
0925: * This method uses {@link InputStreamReader}.
0926: *
0927: * @param input the <code>InputStream</code> to read from
0928: * @param output the <code>Writer</code> to write to
0929: * @param encoding the encoding to use, null means platform default
0930: * @throws NullPointerException if the input or output is null
0931: * @throws IOException if an I/O error occurs
0932: * @since Commons IO 1.1
0933: */
0934: public static void copy(InputStream input, Writer output,
0935: String encoding) throws IOException {
0936: if (encoding == null) {
0937: copy(input, output);
0938: } else {
0939: InputStreamReader in = new InputStreamReader(input,
0940: encoding);
0941: copy(in, output);
0942: }
0943: }
0944:
0945: // copy from Reader
0946: //-----------------------------------------------------------------------
0947: /**
0948: * Copy chars from a <code>Reader</code> to a <code>Writer</code>.
0949: * <p>
0950: * This method buffers the input internally, so there is no need to use a
0951: * <code>BufferedReader</code>.
0952: *
0953: * @param input the <code>Reader</code> to read from
0954: * @param output the <code>Writer</code> to write to
0955: * @return the number of characters copied
0956: * @throws NullPointerException if the input or output is null
0957: * @throws IOException if an I/O error occurs
0958: * @since Commons IO 1.1
0959: */
0960: public static int copy(Reader input, Writer output)
0961: throws IOException {
0962: char[] buffer = new char[DEFAULT_BUFFER_SIZE];
0963: int count = 0;
0964: int n = 0;
0965: while (-1 != (n = input.read(buffer))) {
0966: output.write(buffer, 0, n);
0967: count += n;
0968: }
0969: return count;
0970: }
0971:
0972: /**
0973: * Copy chars from a <code>Reader</code> to bytes on an
0974: * <code>OutputStream</code> using the default character encoding of the
0975: * platform, and calling flush.
0976: * <p>
0977: * This method buffers the input internally, so there is no need to use a
0978: * <code>BufferedReader</code>.
0979: * <p>
0980: * Due to the implementation of OutputStreamWriter, this method performs a
0981: * flush.
0982: * <p>
0983: * This method uses {@link OutputStreamWriter}.
0984: *
0985: * @param input the <code>Reader</code> to read from
0986: * @param output the <code>OutputStream</code> to write to
0987: * @throws NullPointerException if the input or output is null
0988: * @throws IOException if an I/O error occurs
0989: * @since Commons IO 1.1
0990: */
0991: public static void copy(Reader input, OutputStream output)
0992: throws IOException {
0993: OutputStreamWriter out = new OutputStreamWriter(output);
0994: copy(input, out);
0995: // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
0996: // have to flush here.
0997: out.flush();
0998: }
0999:
1000: /**
1001: * Copy chars from a <code>Reader</code> to bytes on an
1002: * <code>OutputStream</code> using the specified character encoding, and
1003: * calling flush.
1004: * <p>
1005: * This method buffers the input internally, so there is no need to use a
1006: * <code>BufferedReader</code>.
1007: * <p>
1008: * Character encoding names can be found at
1009: * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
1010: * <p>
1011: * Due to the implementation of OutputStreamWriter, this method performs a
1012: * flush.
1013: * <p>
1014: * This method uses {@link OutputStreamWriter}.
1015: *
1016: * @param input the <code>Reader</code> to read from
1017: * @param output the <code>OutputStream</code> to write to
1018: * @param encoding the encoding to use, null means platform default
1019: * @throws NullPointerException if the input or output is null
1020: * @throws IOException if an I/O error occurs
1021: * @since Commons IO 1.1
1022: */
1023: public static void copy(Reader input, OutputStream output,
1024: String encoding) throws IOException {
1025: if (encoding == null) {
1026: copy(input, output);
1027: } else {
1028: OutputStreamWriter out = new OutputStreamWriter(output,
1029: encoding);
1030: copy(input, out);
1031: // XXX Unless anyone is planning on rewriting OutputStreamWriter,
1032: // we have to flush here.
1033: out.flush();
1034: }
1035: }
1036:
1037: // content equals
1038: //-----------------------------------------------------------------------
1039: /**
1040: * Compare the contents of two Streams to determine if they are equal or
1041: * not.
1042: * <p>
1043: * This method buffers the input internally using
1044: * <code>BufferedInputStream</code> if they are not already buffered.
1045: *
1046: * @param input1 the first stream
1047: * @param input2 the second stream
1048: * @return true if the content of the streams are equal or they both don't
1049: * exist, false otherwise
1050: * @throws NullPointerException if either input is null
1051: * @throws IOException if an I/O error occurs
1052: */
1053: public static boolean contentEquals(InputStream input1,
1054: InputStream input2) throws IOException {
1055: if (!(input1 instanceof BufferedInputStream)) {
1056: input1 = new BufferedInputStream(input1);
1057: }
1058: if (!(input2 instanceof BufferedInputStream)) {
1059: input2 = new BufferedInputStream(input2);
1060: }
1061:
1062: int ch = input1.read();
1063: while (-1 != ch) {
1064: int ch2 = input2.read();
1065: if (ch != ch2) {
1066: return false;
1067: }
1068: ch = input1.read();
1069: }
1070:
1071: int ch2 = input2.read();
1072: return (ch2 == -1);
1073: }
1074:
1075: /**
1076: * Compare the contents of two Readers to determine if they are equal or
1077: * not.
1078: * <p>
1079: * This method buffers the input internally using
1080: * <code>BufferedReader</code> if they are not already buffered.
1081: *
1082: * @param input1 the first reader
1083: * @param input2 the second reader
1084: * @return true if the content of the readers are equal or they both don't
1085: * exist, false otherwise
1086: * @throws NullPointerException if either input is null
1087: * @throws IOException if an I/O error occurs
1088: * @since Commons IO 1.1
1089: */
1090: public static boolean contentEquals(Reader input1, Reader input2)
1091: throws IOException {
1092: if (!(input1 instanceof BufferedReader)) {
1093: input1 = new BufferedReader(input1);
1094: }
1095: if (!(input2 instanceof BufferedReader)) {
1096: input2 = new BufferedReader(input2);
1097: }
1098:
1099: int ch = input1.read();
1100: while (-1 != ch) {
1101: int ch2 = input2.read();
1102: if (ch != ch2) {
1103: return false;
1104: }
1105: ch = input1.read();
1106: }
1107:
1108: int ch2 = input2.read();
1109: return (ch2 == -1);
1110: }
1111:
1112: }
|