Example usage for java.lang Readable read

List of usage examples for java.lang Readable read

Introduction

In this page you can find the example usage for java.lang Readable read.

Prototype

public int read(java.nio.CharBuffer cb) throws IOException;

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:org.omnaest.utils.structure.container.ByteArrayContainer.java

/**
 * Copies the content from a {@link Readable} using the given encoding
 * /*from   w w  w .  ja va  2 s .  c  om*/
 * @param readable
 * @param encoding
 * @return this
 */
public ByteArrayContainer copyFrom(Readable readable, String encoding) {
    //
    this.isContentInvalid = false;
    if (readable != null) {
        //
        encoding = StringUtils.defaultString(encoding, ENCODING_UTF8);

        //
        try {
            //
            final StringBuffer stringBuffer = new StringBuffer();
            final CharBuffer charBuffer = CharBuffer.wrap(new char[1000]);
            for (int read = 0; read >= 0;) {
                //
                charBuffer.clear();
                read = readable.read(charBuffer);
                charBuffer.flip();
                if (read > 0) {
                    stringBuffer.append(charBuffer, 0, read);
                }
            }

            this.copyFrom(stringBuffer, encoding);
        } catch (IOException e) {
            //
            this.isContentInvalid = true;

            //
            this.handleException(e);
        }
    }

    //
    return this;
}