NIO2 API

org.classpath.icedtea.java.io
Class Inputs

java.lang.Object
  extended by org.classpath.icedtea.java.io.Inputs

public final class Inputs
extends java.lang.Object

This class consists exclusively of static methods that operate on input sources.

The methods to read lines of text defined by this class recognize the following as Unicode line terminators:

Since:
1.7

Method Summary
static void closeUnchecked(java.io.Closeable source)
          Closes the given data source by invoking its close method.
static byte[] readAllBytes(File source)
          Read all bytes from the specified file.
static byte[] readAllBytes(FileRef source)
          Read all bytes from the specified file.
static byte[] readAllBytes(java.io.InputStream source)
          Read all bytes from the specified input stream.
static java.util.List<java.lang.String> readAllLines(File source)
          Read all lines from the specified file.
static java.util.List<java.lang.String> readAllLines(FileRef source)
          Read all lines from the specified file.
static java.util.List<java.lang.String> readAllLines(FileRef source, java.lang.String csn)
          Read all lines from the specified file.
static java.util.List<java.lang.String> readAllLines(File source, java.lang.String csn)
          Read all lines from the specified file.
static java.util.List<java.lang.String> readAllLines(java.io.InputStream source)
          Read all lines from the specified input stream.
static java.util.List<java.lang.String> readAllLines(java.io.InputStream source, java.lang.String csn)
          Read all lines from the specified input stream.
static java.util.List<java.lang.String> readAllLines(java.lang.Readable source)
          Read all lines from the from the specified source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

closeUnchecked

public static void closeUnchecked(java.io.Closeable source)
Closes the given data source by invoking its close method. If the close method throws an IOException then it is silently ignored. If the source is already closed then invoking this method has no effect.

This method should not be used to close destinations or output streams that may be buffered. An I/O error may occur when flushing buffered data.

Parameters:
source - The data source

readAllBytes

public static byte[] readAllBytes(FileRef source)
                           throws java.io.IOException
Read all bytes from the specified file. When all bytes have been read, or an I/O error occurs, then the file is closed.

Parameters:
source - The data source
Returns:
A byte array containing the bytes read from the file
Throws:
java.io.IOException - If an I/O error occurs
java.lang.OutOfMemoryError - If the required array size is too large

readAllBytes

public static byte[] readAllBytes(File source)
                           throws java.io.IOException
Read all bytes from the specified file. When all bytes have been read, or an I/O error occurs, then the file is closed.

Parameters:
source - The data source
Returns:
A byte array containing the bytes read from the file
Throws:
java.io.IOException - If an I/O error occurs
java.lang.OutOfMemoryError - If the required array size is too large

readAllBytes

public static byte[] readAllBytes(java.io.InputStream source)
                           throws java.io.IOException
Read all bytes from the specified input stream.

Usage Example: Suppose we want to open a connection to a resource identified by a URI, and read all bytes:

   URI uri = ...
   byte[] content = InputOutput.readAllBytes(uri.toURL().openStream());
 

On return, the input stream will be at end of stream.

Parameters:
source - The data source
Returns:
A byte array containing the bytes read from the source
Throws:
java.io.IOException - If an I/O error occurs
java.lang.OutOfMemoryError - If the required array size is too large

readAllLines

public static java.util.List<java.lang.String> readAllLines(FileRef source,
                                                            java.lang.String csn)
                                                     throws java.io.IOException
Read all lines from the specified file. Bytes from the file are converted into characters using the specified charset. When all lines have been read, or an I/O error occurs, then the file is closed.

Parameters:
source - The data source
csn - The name of the charset to be used
Throws:
java.nio.charset.UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
java.nio.charset.MalformedInputException - If the file contains a byte sequence that is not legal for the charset
java.io.IOException - If an I/O error occurs

readAllLines

public static java.util.List<java.lang.String> readAllLines(FileRef source)
                                                     throws java.io.IOException
Read all lines from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset. When all lines have been read, or an I/O error occurs, then the file is closed.

Parameters:
source - The data source
Throws:
java.nio.charset.MalformedInputException - If the file contains a byte sequence that is not legal for the default charset
java.io.IOException - If an I/O error occurs

readAllLines

public static java.util.List<java.lang.String> readAllLines(File source,
                                                            java.lang.String csn)
                                                     throws java.io.IOException
Read all lines from the specified file. Bytes from the file are converted into characters using the specified charset. When all lines have been read, or an I/O error occurs, then the file is closed.

Parameters:
source - The data source
csn - The name of the charset to be used
Throws:
java.nio.charset.UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
java.nio.charset.MalformedInputException - If the file contains a byte sequence that is not legal for the charset
java.io.IOException - If an I/O error occurs

readAllLines

public static java.util.List<java.lang.String> readAllLines(File source)
                                                     throws java.io.IOException
Read all lines from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset. When all lines have been read, or an I/O error occurs, then the file is closed.

Parameters:
source - The data source
Throws:
java.nio.charset.MalformedInputException - If the file contains a byte sequence that is not legal for the default charset
java.io.IOException - If an I/O error occurs

readAllLines

public static java.util.List<java.lang.String> readAllLines(java.io.InputStream source,
                                                            java.lang.String csn)
                                                     throws java.io.IOException
Read all lines from the specified input stream. Bytes from the stream are converted into characters using the specified charset.

On return, the input stream will be at end of stream.

Parameters:
source - The input stream to read from
csn - The name of the charset to be used
Throws:
java.nio.charset.UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
java.nio.charset.MalformedInputException - If a byte sequence that is not legal for the charset is read from the input
java.io.IOException - If an I/O error occurs

readAllLines

public static java.util.List<java.lang.String> readAllLines(java.io.InputStream source)
                                                     throws java.io.IOException
Read all lines from the specified input stream. Bytes from the stream are converted into characters using the underlying platform's default charset.

On return, the input stream will be at end of stream.

Parameters:
source - The input stream to read from
Returns:
An unmodifiable list of the lines read from the input stream
Throws:
java.nio.charset.MalformedInputException - If a byte sequence that is not legal for the default charset is read from the input
java.io.IOException - If an I/O error occurs

readAllLines

public static java.util.List<java.lang.String> readAllLines(java.lang.Readable source)
                                                     throws java.io.IOException
Read all lines from the from the specified source.

On return, the input source will be at end of stream.

Parameters:
source - The input stream to read from
Returns:
An unmodifiable list of the lines read from source
Throws:
java.io.IOException - If an I/O error occurs

NIO2 API

Submit a bug or feature

Copyright 2010 Sun Microsystems, Inc. All rights reserved. Use is subject to the terms of the GNU General Public License.