Example usage for io.netty.buffer ByteBuf isReadable

List of usage examples for io.netty.buffer ByteBuf isReadable

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf isReadable.

Prototype

public abstract boolean isReadable();

Source Link

Document

Returns true if and only if (this.writerIndex - this.readerIndex) is greater than 0 .

Usage

From source file:info.zhoumin.dat.AbstractDoubleArrayTrie.java

License:Apache License

@Override
public V put(K key, V value) {
    if (key == null) {
        throw new NullPointerException("Key cannot be null");
    }//ww  w .  ja va  2s.c  om

    int s = DOUBLE_ARRAY_ROOT;
    keyAnalyzer.setValue(key);
    while (keyAnalyzer.hasNext()) {
        if (daIsSeperate(s))
            break;
        int c = (int) keyAnalyzer.next() + 1;
        int next = getNext(s, c);
        if (next < 0) {
            branchInBranch(s, c, keyAnalyzer.rest(), value);
            return value;
        }
        s = next;
    }

    /* walk through tail */
    ByteBuf suffix = tail.getSuffix(getTailIndex(s));
    suffix.resetReaderIndex();
    ByteBuf tailBytes = keyAnalyzer.rest();

    int c = (keyAnalyzer.hasNext() ? keyAnalyzer.next() : 0) + 1;
    while (suffix.isReadable() && tailBytes.isReadable()) {
        byte b = suffix.readByte();
        if (b != tailBytes.readByte()) {
            branchInTail(s, c, tailBytes, value);
            return value;
        }
    }

    // , ?
    this.isDirty = true;
    return null;
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Retrieves a domain name given a buffer containing a DNS packet. If the
 * name contains a pointer, the position of the buffer will be set to
 * directly after the pointer's index after the name has been read.
 *
 * @param buf the byte buffer containing the DNS packet
 * @return the domain name for an entry//from  ww w  . ja v  a 2s . c  om
 */
public static String readName(ByteBuf buf) {
    int position = -1;
    StringBuilder name = new StringBuilder();
    for (int len = buf.readUnsignedByte(); buf.isReadable() && len != 0; len = buf.readUnsignedByte()) {
        boolean pointer = (len & 0xc0) == 0xc0;
        if (pointer) {
            if (position == -1) {
                position = buf.readerIndex() + 1;
            }
            buf.readerIndex((len & 0x3f) << 8 | buf.readUnsignedByte());
        } else {
            name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append(".");
            buf.skipBytes(len);
        }
    }
    if (position != -1) {
        buf.readerIndex(position);
    }
    if (name.length() == 0) {
        return null;
    }
    return name.substring(0, name.length() - 1);
}

From source file:io.advantageous.conekt.http.impl.AssembledFullHttpRequest.java

License:Open Source License

private static LastHttpContent toLastContent(ByteBuf buf) {
    if (buf.isReadable()) {
        return new DefaultLastHttpContent(buf, false);
    } else {//  ww w  . j ava 2 s .c  om
        return LastHttpContent.EMPTY_LAST_CONTENT;
    }
}

From source file:io.advantageous.conekt.http.impl.AssembledFullHttpResponse.java

License:Open Source License

private static LastHttpContent toLastContent(ByteBuf buf, HttpHeaders trailingHeaders, DecoderResult result) {
    if (buf.isReadable()) {
        if (trailingHeaders == null) {
            return new DefaultLastHttpContent(buf);
        } else {/*  w  w  w.  j  a  v a2s.  c om*/
            return new AssembledLastHttpContent(buf, trailingHeaders, result);
        }
    } else {
        if (trailingHeaders == null) {
            return LastHttpContent.EMPTY_LAST_CONTENT;
        } else {
            return new AssembledLastHttpContent(Unpooled.EMPTY_BUFFER, trailingHeaders, result);
        }
    }
}

From source file:io.advantageous.conekt.http.impl.HttpClientRequestImpl.java

License:Open Source License

private void write(ByteBuf buff, boolean end) {
    int readableBytes = buff.readableBytes();
    if (readableBytes == 0 && !end) {
        // nothing to write to the connection just return
        return;//from w ww  . j  a v  a2 s. c om
    }

    if (end) {
        completed = true;
    }
    if (!end && !chunked && !contentLengthSet()) {
        throw new IllegalStateException(
                "You must set the Content-Length header to be the total size of the message "
                        + "body BEFORE sending any data if you are not using HTTP chunked encoding.");
    }

    written += buff.readableBytes();
    if (conn == null) {
        if (pendingChunks == null) {
            pendingChunks = buff;
        } else {
            CompositeByteBuf pending;
            if (pendingChunks instanceof CompositeByteBuf) {
                pending = (CompositeByteBuf) pendingChunks;
            } else {
                pending = Unpooled.compositeBuffer();
                pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex());
                pendingChunks = pending;
            }
            pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
        }
        connect();
    } else {
        if (!headWritten) {
            writeHeadWithContent(buff, end);
        } else {
            if (end) {
                if (buff.isReadable()) {
                    conn.writeToChannel(new DefaultLastHttpContent(buff, false));
                } else {
                    conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT);
                }
            } else {
                conn.writeToChannel(new DefaultHttpContent(buff));
            }
        }
        if (end) {
            conn.reportBytesWritten(written);

            if (respHandler != null) {
                conn.endRequest();
            }
        }
    }
}

From source file:io.advantageous.conekt.http.impl.HttpServerResponseImpl.java

License:Open Source License

private void end0(ByteBuf data) {
    checkWritten();/*from   ww w .  j  a  v a 2  s . com*/
    bytesWritten += data.readableBytes();
    if (!headWritten) {
        // if the head was not written yet we can write out everything in one go
        // which is cheaper.
        prepareHeaders();
        FullHttpResponse resp;
        if (trailing != null) {
            resp = new AssembledFullHttpResponse(response, data, trailing.trailingHeaders(),
                    trailing.getDecoderResult());
        } else {
            resp = new AssembledFullHttpResponse(response, data);
        }
        channelFuture = conn.writeToChannel(resp);
    } else {
        if (!data.isReadable()) {
            if (trailing == null) {
                channelFuture = conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT);
            } else {
                channelFuture = conn.writeToChannel(trailing);
            }
        } else {
            LastHttpContent content;
            if (trailing != null) {
                content = new AssembledLastHttpContent(data, trailing.trailingHeaders(),
                        trailing.getDecoderResult());
            } else {
                content = new DefaultLastHttpContent(data, false);
            }
            channelFuture = conn.writeToChannel(content);
        }
    }

    if (!keepAlive) {
        closeConnAfterWrite();
        closed = true;
    }
    written = true;
    conn.responseComplete();
    if (bodyEndHandler != null) {
        bodyEndHandler.handle(null);
    }
}

From source file:io.advantageous.conekt.net.impl.ConektHandler.java

License:Open Source License

protected static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }/*from   w w w  .j  av a  2  s.  c  o  m*/
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        try {
            if (buf.isReadable()) {
                ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
                buffer.writeBytes(buf);
                return buffer;
            } else {
                return Unpooled.EMPTY_BUFFER;
            }
        } finally {
            buf.release();
        }
    }
    return buf;
}

From source file:io.airlift.drift.transport.netty.codec.HeaderCodec.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext context, Object message) {
    if (message instanceof ByteBuf) {
        ByteBuf request = (ByteBuf) message;
        if (request.isReadable()) {
            context.fireChannelRead(HeaderTransport.decodeFrame(request));
            return;
        }//from  w ww .j ava 2  s .  c om
    }
    context.fireChannelRead(message);
}

From source file:io.airlift.drift.transport.netty.codec.SimpleFrameCodec.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext context, Object message) throws Exception {
    if (message instanceof ByteBuf) {
        ByteBuf buffer = (ByteBuf) message;
        if (buffer.isReadable()) {
            context.fireChannelRead(new ThriftFrame(extractResponseSequenceId(buffer.retain()), buffer,
                    ImmutableMap.of(), transport, protocol, assumeClientsSupportOutOfOrderResponses));
            return;
        }// w  w  w  . j ava2s. c  om
    }
    context.fireChannelRead(message);
}

From source file:io.airlift.drift.transport.netty.ThriftClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext context, Object message) throws Exception {
    if (message instanceof ByteBuf && ((ByteBuf) message).isReadable()) {
        ByteBuf response = (ByteBuf) message;
        if (response.isReadable()) {
            messageReceived(context, response);
            return;
        }//from  w  w w .  j  a  v  a2  s  .  co  m
    }
    context.fireChannelRead(message);
}