Example usage for java.io UnsupportedEncodingException initCause

List of usage examples for java.io UnsupportedEncodingException initCause

Introduction

In this page you can find the example usage for java.io UnsupportedEncodingException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:com.puppycrawl.tools.checkstyle.api.FileText.java

/**
 * Creates a new file text representation.
 *
 * <p>The file will be read using the specified encoding, replacing
 * malformed input and unmappable characters with the default
 * replacement character./*from   w  w w.j av  a2  s. co  m*/
 *
 * @param file the name of the file
 * @param charsetName the encoding to use when reading the file
 * @throws NullPointerException if the text is null
 * @throws IOException if the file could not be read
 */
public FileText(File file, String charsetName) throws IOException {
    this.file = file;

    // We use our own decoder, to be sure we have complete control
    // about replacements.
    final CharsetDecoder decoder;
    try {
        charset = Charset.forName(charsetName);
        decoder = charset.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    } catch (final UnsupportedCharsetException ex) {
        final String message = "Unsupported charset: " + charsetName;
        final UnsupportedEncodingException ex2 = new UnsupportedEncodingException(message);
        ex2.initCause(ex);
        throw ex2;
    }

    fullText = readFile(file, decoder);

    // Use the BufferedReader to break down the lines as this
    // is about 30% faster than using the
    // LINE_TERMINATOR.split(fullText, -1) method
    final ArrayList<String> textLines = new ArrayList<>();
    final BufferedReader reader = new BufferedReader(new StringReader(fullText));
    while (true) {
        final String line = reader.readLine();
        if (line == null) {
            break;
        }
        textLines.add(line);
    }
    lines = textLines.toArray(new String[textLines.size()]);
}