Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract int readBytes(GatheringByteChannel out, int length) throws IOException;

Source Link

Document

Transfers this buffer's data to the specified stream starting at the current readerIndex .

Usage

From source file:alluxio.client.block.stream.LocalFilePacketWriter.java

License:Apache License

@Override
public void writePacket(final ByteBuf buf) throws IOException {
    try {// w ww  .  jav  a2 s .  c o m
        Preconditions.checkState(!mClosed, "PacketWriter is closed while writing packets.");
        int sz = buf.readableBytes();
        ensureReserved(mPos + sz);
        mPos += sz;
        Preconditions.checkState(buf.readBytes(mWriter.getChannel(), sz) == sz);
    } finally {
        buf.release();
    }
}

From source file:alluxio.worker.block.io.LocalFileBlockWriter.java

License:Apache License

@Override
public void transferFrom(ByteBuf buf) throws IOException {
    mPosition += buf.readBytes(mLocalFileChannel, buf.readableBytes());
}

From source file:alluxio.worker.block.io.MockBlockWriter.java

License:Apache License

@Override
public void transferFrom(ByteBuf buf) throws IOException {
    mPosition += buf.readBytes(getChannel(), buf.readableBytes());
}

From source file:alluxio.worker.netty.DataServerBlockWriteHandler.java

License:Apache License

@Override
protected void writeBuf(ByteBuf buf, long pos) throws Exception {
    if (mBytesReserved < pos) {
        long bytesToReserve = Math.max(FILE_BUFFER_SIZE, pos - mBytesReserved);
        // Allocate enough space in the existing temporary block for the write.
        mWorker.requestSpace(mRequest.mSessionId, mRequest.mId, bytesToReserve);
        mBytesReserved += bytesToReserve;
    }/*from w w w  . j  av  a  2 s  .c  o  m*/
    BlockWriter blockWriter = ((BlockWriteRequestInternal) mRequest).mBlockWriter;
    GatheringByteChannel outputChannel = blockWriter.getChannel();
    int sz = buf.readableBytes();
    Preconditions.checkState(buf.readBytes(outputChannel, sz) == sz);
}

From source file:alluxio.worker.netty.DataServerUFSFileWriteHandler.java

License:Apache License

@Override
protected void writeBuf(ByteBuf buf, long pos) throws Exception {
    buf.readBytes(((FileWriteRequestInternal) mRequest).mOutputStream, buf.readableBytes());
}

From source file:at.yawk.accordion.netty.Framer.java

License:Mozilla Public License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    // length of this message
    int length = msg.readableBytes();
    if (length > MAXIMUM_MESSAGE_LENGTH) {
        throw new IOException("Message too long: " + length + " bytes");
    }//from  w ww.  j a va2 s  .  c  o m
    out.ensureWritable(2 + length);

    // write
    out.writeShort(length);
    msg.readBytes(out, length);
}

From source file:buildcraft.core.network.PacketRPC.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf data) {
    id = data.readInt();// w  w w. java 2 s . c om
    int length = data.readInt();
    contents = Unpooled.buffer(length);
    data.readBytes(contents, length);
}

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

License:Apache License

protected long writeStreamsToBlock() throws IOException {
    long writtenBytes = 0;
    openWritesLock.lock();//from w  ww .  j a v  a2  s.co  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.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/*from   w  w  w  .  j ava2s  . c om*/
                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   ww w  .  ja v a  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);
                }
            });
}