Example usage for org.apache.http.nio.reactor SessionInputBuffer readLine

List of usage examples for org.apache.http.nio.reactor SessionInputBuffer readLine

Introduction

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

Prototype

boolean readLine(CharArrayBuffer dst, boolean endOfStream) throws CharacterCodingException;

Source Link

Document

Attempts to transfer a complete line of characters up to a line delimiter from this buffer to the destination buffer.

Usage

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

@Test
public void testReadLineChunks() throws Exception {
    SessionInputBuffer inbuf = new SMTPInputBuffer(16, 16);

    ReadableByteChannel channel = newChannel("O\ne\r\nTwo\r\nTh\ree");

    inbuf.fill(channel);//w  ww . ja  v  a2 s  .c  om

    CharArrayBuffer line = new CharArrayBuffer(64);

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("O", line.toString());
    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("e", line.toString());

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("Two", line.toString());

    line.clear();
    Assert.assertFalse(inbuf.readLine(line, false));

    channel = newChannel("\r\nFour");
    inbuf.fill(channel);

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, false));
    Assert.assertEquals("Th\ree", line.toString());

    inbuf.fill(channel);

    line.clear();
    Assert.assertTrue(inbuf.readLine(line, true));
    Assert.assertEquals("Four", line.toString());

    line.clear();
    Assert.assertFalse(inbuf.readLine(line, true));
}

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

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

    SessionInputBuffer buf = this.iobuffers.getInbuf();

    synchronized (sessionState) {
        boolean hasData = true;
        while (hasData && !this.dataReceived) {
            int bytesRead = buf.fill(iosession.channel());
            if (buf.readLine(this.lineBuf, bytesRead == -1)) {

                processLine();//from   w  w  w  .  jav a  2s.c o  m

                if (!this.dataReceived) {
                    this.contentBuf.writeLine(this.lineBuf);
                }
                this.lineBuf.clear();
            } else {
                hasData = false;
            }
            if (this.dataReceived || this.contentBuf.length() > 4 * 1024 || bytesRead == -1) {
                this.contentBuf.flush(this.fileStore.channel());
            }
            if (bytesRead == -1) {
                throw new UnexpectedEndOfStreamException();
            }
        }
        if (this.contentBuf.hasData()) {
            this.contentBuf.flush(this.fileStore.channel());
        }
        if (this.dataReceived && this.pendingDelivery == null) {
            this.fileStore.reset();

            File file = this.fileStore.getFile();
            SMTPContent<ReadableByteChannel> content = new FileSource(file);
            DeliveryRequest deliveryRequest = new BasicDeliveryRequest(sessionState.getSender(),
                    sessionState.getRecipients(), content);
            String messageId = sessionState.getMessageId();
            this.pendingDelivery = this.handler.handle(messageId, deliveryRequest,
                    new OutputTrigger<DeliveryResult>(sessionState, iosession));
        }
    }
}