001: /*
002: * $RCSfile: StringIO.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:42 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.utils.shader;
046:
047: import java.io.IOException;
048: import java.io.File;
049: import java.io.FileReader;
050: import java.io.InputStream;
051: import java.io.InputStreamReader;
052: import java.io.Reader;
053: import java.net.URL;
054:
055: /**
056: * Utility class with static methods to read the entire contents of a
057: * file, URL, InputStream, or Reader into a single String that is
058: * returned to the user.
059: *
060: * @since Java 3D 1.4
061: */
062: public class StringIO {
063: /**
064: * Read the entire contents of the specified file and return a
065: * single String object containing the contents of the file.
066: *
067: * @param fileName the name of the file from which to read
068: *
069: * @return a String containing the contents of the input file
070: *
071: * @throws IOException if the specified file cannot be opened, or
072: * if an I/O error occurs while reading the file
073: */
074: public static String readFully(String fileName) throws IOException {
075: return readFully(new File(fileName));
076: }
077:
078: /**
079: * Read the entire contents of the specified file and return a
080: * single String object containing the contents of the file.
081: * This method does not return until the end of the input file
082: * is reached.
083: *
084: * @param file a File from which to read
085: *
086: * @return a String containing the contents of the input file
087: *
088: * @throws IOException if the specified file cannot be opened, or
089: * if an I/O error occurs while reading the file
090: */
091: public static String readFully(File file) throws IOException {
092: return readFully(new FileReader(file));
093: }
094:
095: /**
096: * Read the entire contents of the specified URL and return a
097: * single String object containing the contents of the URL.
098: * This method does not return until an end of stream is reached
099: * for the URL.
100: *
101: * @param url a URL from which to read
102: *
103: * @return a String containing the contents of the input URL
104: *
105: * @throws IOException if the specified URL cannot be opened, or
106: * if an I/O error occurs while reading the URL
107: */
108: public static String readFully(URL url) throws IOException {
109: return readFully(url.openStream());
110: }
111:
112: /**
113: * Read the entire contents of the specified InputStream and return a
114: * single String object containing the contents of the InputStream.
115: * This method does not return until the end of the input
116: * stream is reached.
117: *
118: * @param stream an InputStream from which to read
119: *
120: * @return a String containing the contents of the input stream
121: *
122: * @throws IOException if an I/O error occurs while reading the input stream
123: */
124: public static String readFully(InputStream stream)
125: throws IOException {
126: return readFully(new InputStreamReader(stream));
127: }
128:
129: /**
130: * Read the entire contents of the specified Reader and return a
131: * single String object containing the contents of the InputStream.
132: * This method does not return until the end of the input file or
133: * stream is reached.
134: *
135: * @param reader a Reader from which to read
136: *
137: * @return a String containing the contents of the stream
138: *
139: * @throws IOException if an I/O error occurs while reading the input stream
140: */
141: public static String readFully(Reader reader) throws IOException {
142: char[] arr = new char[8 * 1024]; // 8K at a time
143: StringBuffer buf = new StringBuffer();
144: int numChars;
145:
146: while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
147: buf.append(arr, 0, numChars);
148: }
149:
150: return buf.toString();
151: }
152:
153: /**
154: * Do not construct an instance of this class.
155: */
156: private StringIO() {
157: }
158: }
|