Example usage for org.apache.http.nio.reactor SessionOutputBuffer length

List of usage examples for org.apache.http.nio.reactor SessionOutputBuffer length

Introduction

In this page you can find the example usage for org.apache.http.nio.reactor SessionOutputBuffer length.

Prototype

int length();

Source Link

Document

Returns the length of this buffer.

Usage

From source file:com.ok2c.lightmtp.impl.protocol.SendDataCodec.java

@Override
public void produceData(final IOSession iosession, final ClientState sessionState)
        throws IOException, SMTPProtocolException {
    Args.notNull(iosession, "IO session");
    Args.notNull(sessionState, "Session state");

    SessionOutputBuffer buf = this.iobuffers.getOutbuf();

    switch (this.codecState) {
    case CONTENT_READY:
        int bytesRead = 0;
        while (buf.length() < LIMIT) {
            if (!this.contentBuf.hasData()) {
                bytesRead = this.contentBuf.fill(this.contentChannel);
            }/*w  w  w.  j  a v  a2 s  .  c  o m*/

            boolean lineComplete = this.contentBuf.readLine(this.lineBuf, bytesRead == -1);
            if (this.maxLineLen > 0 && (this.lineBuf.length() > this.maxLineLen
                    || (!lineComplete && this.contentBuf.length() > this.maxLineLen))) {
                throw new SMTPProtocolException("Maximum line length limit exceeded");
            }
            if (lineComplete) {
                if (this.lineBuf.length() > 0 && this.lineBuf.charAt(0) == '.') {
                    buf.write(PERIOD);
                }
                buf.writeLine(this.lineBuf);
                this.lineBuf.clear();
            } else {
                bytesRead = this.contentBuf.fill(this.contentChannel);
            }
            if (bytesRead == -1 && !this.contentBuf.hasData()) {

                this.lineBuf.clear();
                this.lineBuf.append('.');
                buf.writeLine(this.lineBuf);
                this.lineBuf.clear();

                this.content.reset();
                this.contentSent = true;
                this.codecState = CodecState.CONTENT_RESPONSE_EXPECTED;
                break;
            }
            if (bytesRead == 0 && !lineComplete) {
                break;
            }
        }
    }

    if (buf.hasData()) {
        buf.flush(iosession.channel());
    }
    if (!buf.hasData() && this.contentSent) {
        iosession.clearEvent(SelectionKey.OP_WRITE);
    }
}