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 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(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(InputStream source)
          Read all bytes from the specified input stream.
static List<String> readAllLines(File source)
          Read all lines from the specified file.
static List<String> readAllLines(FileRef source)
          Read all lines from the specified file.
static List<String> readAllLines(FileRef source, String csn)
          Read all lines from the specified file.
static List<String> readAllLines(File source, String csn)
          Read all lines from the specified file.
static List<String> readAllLines(InputStream source)
          Read all lines from the specified input stream.
static List<String> readAllLines(InputStream source, String csn)
          Read all lines from the specified input stream.
static List<String> readAllLines(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(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 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:
IOException - If an I/O error occurs
OutOfMemoryError - If the required array size is too large

readAllBytes

public static byte[] readAllBytes(File source)
                           throws 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:
IOException - If an I/O error occurs
OutOfMemoryError - If the required array size is too large

readAllBytes

public static byte[] readAllBytes(InputStream source)
                           throws 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:
IOException - If an I/O error occurs
OutOfMemoryError - If the required array size is too large

readAllLines

public static List<String> readAllLines(FileRef source,
                                        String csn)
                                 throws 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:
UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
MalformedInputException - If the file contains a byte sequence that is not legal for the charset
IOException - If an I/O error occurs

readAllLines

public static List<String> readAllLines(FileRef source)
                                 throws 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:
MalformedInputException - If the file contains a byte sequence that is not legal for the default charset
IOException - If an I/O error occurs

readAllLines

public static List<String> readAllLines(File source,
                                        String csn)
                                 throws 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:
UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
MalformedInputException - If the file contains a byte sequence that is not legal for the charset
IOException - If an I/O error occurs

readAllLines

public static List<String> readAllLines(File source)
                                 throws 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:
MalformedInputException - If the file contains a byte sequence that is not legal for the default charset
IOException - If an I/O error occurs

readAllLines

public static List<String> readAllLines(InputStream source,
                                        String csn)
                                 throws 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:
UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
MalformedInputException - If a byte sequence that is not legal for the charset is read from the input
IOException - If an I/O error occurs

readAllLines

public static List<String> readAllLines(InputStream source)
                                 throws 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:
MalformedInputException - If a byte sequence that is not legal for the default charset is read from the input
IOException - If an I/O error occurs

readAllLines

public static List<String> readAllLines(Readable source)
                                 throws 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:
IOException - If an I/O error occurs

NIO2 API

Copyright © 2007, 2011, Oracle and/or its affiliates. All rights reserved.