Description
Read all lines lazily from an InputStream as a Stream .
License
Open Source License
Parameter
Parameter | Description |
---|
in | the input stream |
cs | the charset to use for decoding |
Exception
Parameter | Description |
---|
SecurityException | In the case of the default provider, and a security manager isinstalled, the SecurityManager#checkRead(String) checkRead method isinvoked to check read access to the file. |
Return
the lines from the file as a Stream
Declaration
@SuppressWarnings("resource")
public static Stream<String> lines(InputStream in, Charset cs)
Method Source Code
//package com.java2s;
//License from project: Open Source License
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
public class Main {
/**/*from w w w . j a v a2 s . com*/
* Read all lines from an {@code InputStream} as a {@code Stream}. Bytes from the input stream
* are
* decoded into characters using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset
* charset}.
*
* <p>
* This method works as if invoking it were equivalent to evaluating the expression:
*
* <pre>
* {@code
* Utils.lines(path, StandardCharsets.UTF_8)
* }
* </pre>
*
* @param in
* the input stream
*
* @return the lines from the input stream as a {@code Stream}
*
* @throws IOException
* if an I/O error occurs opening the file
* @throws SecurityException
* In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead} method is
* invoked to check read access to the file.
*
* @see Files#readAllLines(Path)
*/
public static Stream<String> lines(InputStream in) {
return lines(in, StandardCharsets.UTF_8);
}
/**
* Read all lines lazily from an {@code InputStream} as a {@code Stream}.
*
* <p>
* Bytes from the file are decoded into characters using the specified charset and the same line
* terminators as specified by {@link Files#readAllLines(Path, Charset)} are supported.
*
* <p>
* After this method returns, then any subsequent I/O exception that occurs while reading from
* the file or when a malformed or unmappable byte sequence is read, is wrapped in an
* {@link UncheckedIOException} that will be thrown from the {@link java.util.stream.Stream}
* method that caused the read to take place. In case an {@code IOException} is thrown when
* closing the file, it is also wrapped as an {@code UncheckedIOException}.
*
* <p>
* The returned stream encapsulates a {@link Reader}. If timely disposal of file system
* resources is required, the try-with-resources construct should be used to ensure that the
* stream's {@link Stream#close close} method is invoked after the stream operations are
* completed.
*
*
* @param in
* the input stream
* @param cs
* the charset to use for decoding
*
* @return the lines from the file as a {@code Stream}
*
* @throws SecurityException
* In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead} method is
* invoked to check read access to the file.
*
* @see Files#readAllLines(Path, Charset)
* @see Files#newBufferedReader(Path, Charset)
* @see java.io.BufferedReader#lines()
*/
@SuppressWarnings("resource")
public static Stream<String> lines(InputStream in, Charset cs) {
InputStreamReader reader = new InputStreamReader(in, cs);
BufferedReader br = new BufferedReader(reader);
try {
return br.lines().onClose(asUncheckedRunnable(br));
} catch (Error | RuntimeException e) {
try {
br.close();
} catch (IOException ex) {
try {
e.addSuppressed(ex);
} catch (Throwable ignore) {
// ignore
}
}
throw e;
}
}
/**
* Convert a Closeable to a Runnable by converting checked IOException
* to UncheckedIOException.
*
* Copied from {@link Files#asUncheckedRunnable}
*
* @see Files#asUncheckedRunnable
*/
private static Runnable asUncheckedRunnable(Closeable c) {
return () -> {
try {
c.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
}
}
Related
- inputstreamToString(InputStream input, CharsetDecoder decoder)
- inputStreamToString(InputStream inputStream, Charset charset)
- inputStreamToString(InputStream inputStream, String charset)
- inputStreamToString(InputStream is, Charset charset)
- isCharsetMisInterpreted(String input, String encoding)
- loadInputStream(InputStream in, Charset cs)
- processSubstitute(CharBuffer cb, String replacement, boolean endOfInput, String outputCharset, OutputStream os)
- stream2Bytes(InputStream is, Charset charset)
- streamString(InputStream in, boolean closeIn, String charset)