Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

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

Prototype

public abstract int read(char cbuf[], int off, int len) throws IOException;

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:it.doqui.index.ecmengine.business.personalization.encryption.content.DecryptingContentReaderDecorator.java

public String getContentString(int length) throws ContentIOException {
    logger.debug("[DecryptingContentReaderDecorator::getContentString] BEGIN");

    Reader localReader = null;
    try {//from   w w  w  .ja va2 s. com
        if (length < 0 || length > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("Character count must be positive and within range");
        }

        // just create buffer of the required size
        char[] buffer = new char[length];

        String encoding = getEncoding();

        // create a reader from the input stream
        localReader = (encoding == null) ? new InputStreamReader(getContentInputStream())
                : new InputStreamReader(getContentInputStream(), getEncoding());

        // read it all, if possible
        int count = localReader.read(buffer, 0, length);

        // there may have been fewer characters - create a new string as the result
        return ((count != -1) ? new String(buffer, 0, count) : "");
    } catch (IOException e) {
        logger.error(
                "[DecryptingContentReaderDecorator::getContentString] I/O Error reading from: " + localReader,
                e);
        throw new ContentIOException(
                "Failed to copy content to string: \n" + "   accessor: " + this + "\n" + "   length: " + length,
                e);
    } finally {
        if (localReader != null) {
            try {
                localReader.close();
            } catch (Throwable e) {
                logger.warn("[DecryptingContentReaderDecorator::getContentString] Error closing local reader: "
                        + localReader, e);
            }
        }
        logger.debug("[DecryptingContentReaderDecorator::getContentString] END");
    }
}