Example usage for java.io LineNumberReader read

List of usage examples for java.io LineNumberReader read

Introduction

In this page you can find the example usage for java.io LineNumberReader read.

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:org.apache.james.core.MimeMessageWrapper.java

/**
 * Corrects JavaMail 1.1 version which always returns -1. Only corrected for
 * content less than 5000 bytes, to avoid memory hogging.
 *//*from   ww  w .  jav  a  2s  .  co m*/
@Override
public int getLineCount() throws MessagingException {
    InputStream in;
    try {
        in = getContentStream();
    } catch (Exception e) {
        return -1;
    }
    if (in == null) {
        return -1;
    }
    // Wrap input stream in LineNumberReader
    // Not sure what encoding to use really...
    InputStreamReader isr = null;
    LineNumberReader counter = null;
    try {
        if (getEncoding() != null) {
            isr = new InputStreamReader(in, getEncoding());
            counter = new LineNumberReader(isr);
        } else {
            isr = new InputStreamReader(in);
            counter = new LineNumberReader(isr);
        }
        // Read through all the data
        char[] block = new char[4096];
        while (counter.read(block) > -1) {
            // Just keep reading
        }
        return counter.getLineNumber();
    } catch (IOException ioe) {
        return -1;
    } finally {
        IOUtils.closeQuietly(counter);
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(in);
    }
}