org.modeshape.common.util
Class IoUtil

java.lang.Object
  extended by org.modeshape.common.util.IoUtil

@Immutable
public class IoUtil
extends Object

A set of utilities for more easily performing I/O.


Method Summary
static InputStream getResourceAsStream(String resourcePath, ClassLoader classLoader, Class<?> clazz)
          Get the input stream to the resource given by the supplied path.
static boolean isSame(InputStream input1, InputStream input2)
          Write the entire contents of the supplied string to the given stream.
static String read(File file)
          Read and return the entire contents of the supplied File.
static String read(InputStream stream)
          Read and return the entire contents of the supplied InputStream.
static String read(Reader reader)
          Read and return the entire contents of the supplied Reader.
static byte[] readBytes(File file)
          Read and return the entire contents of the supplied file.
static byte[] readBytes(InputStream stream)
          Read and return the entire contents of the supplied stream.
static void write(InputStream input, OutputStream stream)
          Write the entire contents of the supplied string to the given stream.
static void write(Reader input, Writer writer)
          Write the entire contents of the supplied string to the given writer.
static void write(String content, File file)
          Write the entire contents of the supplied string to the given file.
static void write(String content, OutputStream stream)
          Write the entire contents of the supplied string to the given stream.
static void write(String content, Writer writer)
          Write the entire contents of the supplied string to the given writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readBytes

public static byte[] readBytes(InputStream stream)
                        throws IOException
Read and return the entire contents of the supplied stream. This method always closes the stream when finished reading.

Parameters:
stream - the stream to the contents; may be null
Returns:
the contents, or an empty byte array if the supplied reader is null
Throws:
IOException - if there is an error reading the content

readBytes

public static byte[] readBytes(File file)
                        throws IOException
Read and return the entire contents of the supplied file.

Parameters:
file - the file containing the contents; may be null
Returns:
the contents, or an empty byte array if the supplied file is null
Throws:
IOException - if there is an error reading the content

read

public static String read(Reader reader)
                   throws IOException
Read and return the entire contents of the supplied Reader. This method always closes the reader when finished reading.

Parameters:
reader - the reader of the contents; may be null
Returns:
the contents, or an empty string if the supplied reader is null
Throws:
IOException - if there is an error reading the content

read

public static String read(InputStream stream)
                   throws IOException
Read and return the entire contents of the supplied InputStream. This method always closes the stream when finished reading.

Parameters:
stream - the streamed contents; may be null
Returns:
the contents, or an empty string if the supplied stream is null
Throws:
IOException - if there is an error reading the content

read

public static String read(File file)
                   throws IOException
Read and return the entire contents of the supplied File.

Parameters:
file - the file containing the information to be read; may be null
Returns:
the contents, or an empty string if the supplied reader is null
Throws:
IOException - if there is an error reading the content

write

public static void write(String content,
                         File file)
                  throws IOException
Write the entire contents of the supplied string to the given file.

Parameters:
content - the content to write to the stream; may be null
file - the file to which the content is to be written
Throws:
IOException
IllegalArgumentException - if the stream is null

write

public static void write(String content,
                         OutputStream stream)
                  throws IOException
Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when finished.

Parameters:
content - the content to write to the stream; may be null
stream - the stream to which the content is to be written
Throws:
IOException
IllegalArgumentException - if the stream is null

write

public static void write(String content,
                         Writer writer)
                  throws IOException
Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when finished.

Parameters:
content - the content to write to the writer; may be null
writer - the writer to which the content is to be written
Throws:
IOException
IllegalArgumentException - if the writer is null

write

public static void write(InputStream input,
                         OutputStream stream)
                  throws IOException
Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when finished.

Parameters:
input - the content to write to the stream; may be null
stream - the stream to which the content is to be written
Throws:
IOException
IllegalArgumentException - if the stream is null

write

public static void write(Reader input,
                         Writer writer)
                  throws IOException
Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when finished.

Parameters:
input - the content to write to the writer; may be null
writer - the writer to which the content is to be written
Throws:
IOException
IllegalArgumentException - if the writer is null

isSame

public static boolean isSame(InputStream input1,
                             InputStream input2)
                      throws IOException
Write the entire contents of the supplied string to the given stream. This method always flushes and closes the stream when finished.

Parameters:
input1 - the first stream
input2 - the second stream
Returns:
true if the streams contain the same content, or false otherwise
Throws:
IOException
IllegalArgumentException - if the stream is null

getResourceAsStream

public static InputStream getResourceAsStream(String resourcePath,
                                              ClassLoader classLoader,
                                              Class<?> clazz)
Get the input stream to the resource given by the supplied path. If a class loader is supplied, the method attempts to resolve the resource using the ClassLoader.getResourceAsStream(String) method; if the result is non-null, it is returned. Otherwise, if a class is supplied, this method attempts to resolve the resource using the Class.getResourceAsStream(String) method; if the result is non-null, it is returned. Otherwise, this method then uses the Class' ClassLoader to load the resource; if non-null, it is returned . Otherwise, this method looks for an existing and readable file at the path; if found, a buffered stream to that file is returned. Otherwise, this method attempts to parse the resource path into a valid URL; if this succeeds, the method attempts to open a stream to that URL. If all of these fail, this method returns null.

Parameters:
resourcePath - the logical path to the classpath, file, or URL resource
clazz - the class that should be used to load the resource as a stream; may be null
classLoader - the classloader that should be used to load the resource as a stream; may be null
Returns:
an input stream to the resource; or null if the resource could not be found
Throws:
IllegalArgumentException - if the resource path is null or empty


Copyright © 2008-2012 JBoss, a division of Red Hat. All Rights Reserved.