Example usage for io.netty.buffer ByteBuf readableBytes

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

Introduction

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

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

From source file:com.addthis.meshy.TargetHandler.java

License:Apache License

public int send(ByteBuf buffer, SendWatcher watcher) {
    if (log.isTraceEnabled()) {
        log.trace("{} send b={} l={}", this, buffer, buffer.readableBytes());
    }//w w w .  j  a va  2  s. c  om
    int length = buffer.readableBytes();
    channelState.send(buffer, watcher, length);
    return length;
}

From source file:com.addthis.muxy.MuxStreamDirectory.java

License:Apache License

protected long writeStreamsToBlock() throws IOException {
    long writtenBytes = 0;
    openWritesLock.lock();/*  www. j  ava2s.  c  o  m*/
    try {
        /* yes, this could be optimized for concurrency by writing after lock is released, etc */
        if (openWriteBytes.get() == 0) {
            return 0;
        }
        List<TempData> streamsWithData = new ArrayList<>(openStreamWrites.size());
        for (StreamOut out : openStreamWrites.values()) {
            synchronized (out) {
                StreamOut pendingOut = pendingStreamCloses.get(out.meta.streamId);
                if (pendingOut != null) {
                    pendingOut.outputBuffer.writeBytes(out.outputBuffer);
                    assert out.outputBuffer.readableBytes() == 0;
                    out.outputBuffer.discardSomeReadBytes();
                } else if (out.output.buffer().readableBytes() > 0) {
                    streamsWithData.add(new TempData(out));
                    out.outputBuffer.retain();
                }
            }
        }
        for (StreamOut out : pendingStreamCloses.values()) { // guarded by openStreamWrites
            streamsWithData.add(new TempData(out));
        }
        pendingStreamCloses.clear();
        if (streamsWithData.isEmpty()) {
            return 0;
        }
        for (TempData td : streamsWithData) {
            writtenBytes += td.snapshotLength;
        }
        int streams = streamsWithData.size();
        publishEvent(MuxyStreamEvent.BLOCK_FILE_WRITE, streams);
        int currentFileOffset = (int) openWriteFile.size();
        /* write out IDs in this block */
        ByteBuf metaBuffer = PooledByteBufAllocator.DEFAULT.directBuffer(2 + 4 * streams + 4 + 8 * streams);
        metaBuffer.writeShort(streams);
        int bodyOutputSize = 0;
        for (TempData out : streamsWithData) {
            metaBuffer.writeInt(out.meta.streamId);
            /* (4) chunk body offset (4) chunk length (n) chunk bytes */
            bodyOutputSize += 8 + out.snapshotLength;
        }
        /* write remainder size for rest of block data so that readers can skip if desired ID isn't present */
        metaBuffer.writeInt(bodyOutputSize);
        /* write offsets and lengths for each stream id */
        int bodyOffset = streamsWithData.size() * 8;
        for (TempData out : streamsWithData) {
            metaBuffer.writeInt(bodyOffset); //TODO - reconsider how frequently this shortcut is placed on disk
            metaBuffer.writeInt(out.snapshotLength);
            bodyOffset += out.snapshotLength;
        }
        while (metaBuffer.readableBytes() > 0) {
            metaBuffer.readBytes(openWriteFile, metaBuffer.readableBytes());
        }
        metaBuffer.release();
        /* write bytes for each stream id */
        for (TempData out : streamsWithData) {
            synchronized (out.stream) { // need less confusing variable names for concurrency
                int toWrite = out.snapshotLength;
                while (toWrite > 0) {
                    int numBytesRead = out.data.readBytes(openWriteFile, toWrite);
                    assert numBytesRead > 0;
                    toWrite -= numBytesRead;
                }
                openWriteBytes.addAndGet((long) -out.snapshotLength);
                eventListener.reportWrite((long) -out.snapshotLength);
                out.meta.endFile = streamDirectoryConfig.currentFile.get();
                out.meta.endFileBlockOffset = currentFileOffset;
                if (out.meta.startFile == 0) {
                    out.meta.startFile = out.meta.endFile;
                    out.meta.startFileBlockOffset = out.meta.endFileBlockOffset;
                }
                if (!out.data.release()) { // release the pending writes that did not get an extra retain
                    out.data.discardSomeReadBytes();
                }
            }
        }
        /* check for rolling current file on size threshold */
        if (openWriteFile.size() > streamDirectoryConfig.maxFileSize) {
            openWriteFile.close();
            openWriteFile = FileChannel.open(getFileByID(bumpCurrentFile()), APPEND, CREATE);
            publishEvent(MuxyStreamEvent.BLOCK_FILE_WRITE_ROLL, streamDirectoryConfig.currentFile);
        }
    } finally {
        openWritesLock.unlock();
    }
    return writtenBytes;
}

From source file:com.adobe.acs.livereload.impl.WebSocketServerHandler.java

License:Apache License

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    // Handle a bad request.
    if (!req.getDecoderResult().isSuccess()) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;//from   w  ww.j av a2s. c  o  m
    }

    // Allow only GET methods.
    if (req.getMethod() != GET) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return;
    }

    if ("/".equals(req.getUri()) || "/favicon.ico".equals(req.getUri())) {
        FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
        sendHttpResponse(ctx, req, res);
        return;
    }

    if (req.getUri().startsWith("/livereload.js")) {
        InputStream is = getClass().getResourceAsStream("/livereload.js");
        byte[] data = IOUtils.toByteArray(is);
        ByteBuf content = Unpooled.wrappedBuffer(data);

        FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content);

        res.headers().set(CONTENT_TYPE, "application/javascript");
        setContentLength(res, content.readableBytes());

        sendHttpResponse(ctx, req, res);
        return;
    }

    // Handshake
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req),
            null, false);
    handshaker = wsFactory.newHandshaker(req);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
    } else {
        handshaker.handshake(ctx.channel(), req);
    }
}

From source file:com.aerofs.baseline.http.EmptyEntityInputStream.java

License:Apache License

@Override
public void addBuffer(ByteBuf content, boolean last) throws IOException {
    Preconditions.checkArgument(last && (content.readableBytes() == 0),
            "cannot add content to an empty entity input stream");
}

From source file:com.aerofs.baseline.http.EntityInputStream.java

License:Apache License

@Override
public synchronized int read(@Nonnull byte[] b, int off, int len) throws IOException {
    // basic argument checks
    // pulled straight from InputStream
    if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }// ww w  .j ava  2  s. c o  m

    // if the sender already closed
    // the stream then we're going to
    // abort, *even if* the sender
    // has already sent the bytes we
    // care about
    throwIfClosed();

    // we may have to send a 100 CONTINUE
    // if the client specifically requests
    // it. to prevent the server from buffering
    // bytes we only send it the moment the
    // app thread starts reading from the
    // stream. this can be changed easily
    sendContinueIfRequested();

    int initialOffset = off;
    int bufferRemaining = len;

    while (true) {
        // get bytes from the network
        getNetworkBytes();

        // how many bytes can we read?
        int remaining = inputCompleted ? (int) Math.min(readable, bufferRemaining) : bufferRemaining;
        if (remaining == 0) {
            break;
        }

        Preconditions.checkState(!buffers.isEmpty(), "readable bytes, but no buffers");

        // read as much as required
        // from the top buffer
        ByteBuf head = buffers.get(0);
        int chunkReadable = Math.min(head.readableBytes(), remaining);
        head.readBytes(b, off, chunkReadable);

        // bookkeeping
        readable -= chunkReadable;
        off += chunkReadable;
        bufferRemaining -= chunkReadable;

        // remove the top buffer if it's exhausted
        if (head.readableBytes() == 0) {
            buffers.remove(0);
            head.release();
        }
    }

    return off == initialOffset ? -1 : off - initialOffset;
}

From source file:com.aerofs.baseline.http.EntityInputStream.java

License:Apache License

synchronized void addBuffer(ByteBuf content, boolean last) throws IOException {
    throwIfClosed();//  w  w w.  j a  va  2 s  .c o  m

    if (content.readableBytes() > 0) {
        // crucial for reading
        buffers.add(content);
        readable += content.readableBytes();

        // for metrics use only
        total += content.readableBytes();
    }

    if (last) {
        // we're done
        // only update the input size histogram now
        // NOTE: don't trigger additional reading -
        // that will be done by the jersey runtime's
        // call to commit()
        inputCompleted = true;
        INPUT_SIZE_HISTOGRAM.update(total);
    } else {
        // continue reading unless we hit
        // the limit of max cached bytes
        if (readable <= com.aerofs.baseline.http.Constants.ENTITY_UNREAD_BYTES_HIGH_WATERMARK) {
            ctx.channel().read();
        } else {
            readChoked = true;
        }
    }

    // let any waiters know that there's more to consume
    notifyAll();
}

From source file:com.alibaba.dubbo.qos.server.handler.QosProcessHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;// www. j  a  va  2s .c om
    }

    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());

    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}

From source file:com.almuradev.almurasdk.server.network.play.S00PacketPermissions.java

License:MIT License

@Override
public void fromBytes(ByteBuf buf) {
    container = ReplicatedPermissionsContainer.fromBytes(buf.readBytes(buf.readableBytes()).array());
}

From source file:com.amazonaws.http.StaxRxNettyResponseHandler.java

License:Apache License

@Override
public Observable<AmazonWebServiceResponse<T>> handle(HttpClientResponse<ByteBuf> response) throws Exception {
    return response.getContent().reduce(new ByteArrayOutputStream(),
            new Func2<ByteArrayOutputStream, ByteBuf, ByteArrayOutputStream>() {
                @Override/* w ww  .j av a  2  s .  com*/
                public ByteArrayOutputStream call(ByteArrayOutputStream out, ByteBuf bb) {
                    try {
                        bb.readBytes(out, bb.readableBytes());
                        return out;
                    } catch (java.io.IOException e) {
                        throw new RuntimeException(e);
                    } finally {
                        ReferenceCountUtil.safeRelease(bb);
                    }
                }
            }).observeOn(RxSchedulers.computation()).flatMap(out -> {
                byte[] bytes = out.toByteArray();
                if (bytes.length == 0)
                    bytes = "<eof/>".getBytes();
                ByteArrayInputStream in = new ByteArrayInputStream(bytes);
                XMLEventReader reader = null;
                try {
                    reader = xmlInputFactory.createXMLEventReader(in);
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Map<String, String> responseHeaders = new HashMap<String, String>();
                    for (String k : response.getHeaders().names()) {
                        // TODO: comma seperated?
                        responseHeaders.put(k, response.getHeaders().get(k));
                    }
                    AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>();
                    StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(reader,
                            responseHeaders);
                    unmarshallerContext.registerMetadataExpression("ResponseMetadata/RequestId", 2,
                            ResponseMetadata.AWS_REQUEST_ID);
                    unmarshallerContext.registerMetadataExpression("requestId", 2,
                            ResponseMetadata.AWS_REQUEST_ID);
                    registerAdditionalMetadataExpressions(unmarshallerContext);

                    T result = responseUnmarshaller.unmarshall(unmarshallerContext);
                    awsResponse.setResult(result);

                    Map<String, String> metadata = unmarshallerContext.getMetadata();
                    if (responseHeaders != null) {
                        if (responseHeaders.get("x-amzn-RequestId") != null) {
                            metadata.put(ResponseMetadata.AWS_REQUEST_ID,
                                    responseHeaders.get("x-amzn-RequestId"));
                        }
                    }
                    awsResponse.setResponseMetadata(new ResponseMetadata(metadata));

                    return Observable.just(awsResponse);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    try {
                        reader.close();
                    } catch (XMLStreamException e) {
                        log.warn("Error closing xml parser", e);
                    }
                }
            });
}

From source file:com.amazonaws.http.XmlRxNettyErrorResponseHandler.java

License:Apache License

@Override
public Observable<AmazonServiceException> handle(HttpClientResponse<ByteBuf> response) throws Exception {
    // Try to read the error response
    return response.getContent().reduce(new ByteArrayOutputStream(),
            new Func2<ByteArrayOutputStream, ByteBuf, ByteArrayOutputStream>() {
                @Override//from w  ww.  j  ava  2  s .c  o  m
                public ByteArrayOutputStream call(ByteArrayOutputStream out, ByteBuf bb) {
                    try {
                        bb.readBytes(out, bb.readableBytes());
                        return out;
                    } catch (java.io.IOException e) {
                        throw newAmazonServiceException("Unable to unmarshall error response", response, e);
                    } finally {
                        ReferenceCountUtil.safeRelease(bb);
                    }
                }
            }).observeOn(RxSchedulers.computation()).map(new Func1<ByteArrayOutputStream, Document>() {
                @Override
                public Document call(ByteArrayOutputStream out) {
                    String content = new String(out.toByteArray());
                    try {
                        return XpathUtils.documentFrom(content);
                    } catch (Exception e) {
                        throw newAmazonServiceException(
                                String.format("Unable to unmarshall error response (%s)", content), response,
                                e);
                    }
                }
            }).map(new Func1<Document, AmazonServiceException>() {
                @Override
                public AmazonServiceException call(Document document) {
                    for (Unmarshaller<AmazonServiceException, Node> unmarshaller : unmarshallerList) {
                        try {
                            AmazonServiceException ase = unmarshaller.unmarshall(document);
                            if (ase != null) {
                                ase.setStatusCode(response.getStatus().code());
                                return ase;
                            }
                        } catch (Exception e) {
                        }
                    }
                    throw new AmazonClientException("Unable to unmarshall error response from service");
                }
            }).onErrorResumeNext(new Func1<Throwable, Observable<AmazonServiceException>>() {
                @Override
                public Observable<AmazonServiceException> call(Throwable t) {
                    if (t instanceof AmazonServiceException)
                        return Observable.just((AmazonServiceException) t);
                    else
                        return Observable.error(t);
                }
            });
}