Search in sources :

Example 41 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project Almura by AlmuraDev.

the class ClientboundBiomeInformationPacket method readFrom.

@Override
public void readFrom(final ChannelBuf buf) {
    final int size = buf.readInteger();
    this.biomes = new HashMap<>(size);
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            final int length = buf.readInteger();
            ByteBufInputStream byteBufInputStream = null;
            ObjectInputStream objectInputStream = null;
            try {
                final ByteBuf buffer = Unpooled.buffer(length);
                buffer.writeBytes(buf.readBytes(length));
                byteBufInputStream = new ByteBufInputStream(buffer, length);
                objectInputStream = new ObjectInputStream(byteBufInputStream);
                final BiomeConfig biomeConfig = (BiomeConfig) objectInputStream.readObject();
                this.biomes.put(biomeConfig.getServerBiomeId(), biomeConfig);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (byteBufInputStream != null) {
                    try {
                        byteBufInputStream.close();
                    } catch (IOException ignored) {
                    }
                }
                if (objectInputStream != null) {
                    try {
                        objectInputStream.close();
                    } catch (IOException ignored) {
                    }
                }
            }
        }
    }
}
Also used : BiomeConfig(com.almuradev.almura.feature.biome.BiomeConfig) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ObjectInputStream(java.io.ObjectInputStream)

Example 42 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project pravega by pravega.

the class CommandDecoder method parseCommand.

@VisibleForTesting
public static WireCommand parseCommand(ByteBuf in) throws IOException {
    @Cleanup ByteBufInputStream is = new ByteBufInputStream(in);
    int readableBytes = in.readableBytes();
    if (readableBytes < WireCommands.TYPE_PLUS_LENGTH_SIZE) {
        throw new InvalidMessageException("Not enough bytes to read.");
    }
    WireCommandType type = readType(is);
    int length = readLength(is, readableBytes);
    WireCommand command = type.readFrom(is, length);
    return command;
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Cleanup(lombok.Cleanup) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 43 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project pravega by pravega.

the class WireCommandsTest method testCommandFromByteArray.

private void testCommandFromByteArray(byte[] bytes, WireCommand compatibleCommand) throws IOException {
    WireCommand read = compatibleCommand.getType().readFrom(new ByteBufInputStream(Unpooled.wrappedBuffer(bytes)), bytes.length);
    assertEquals(compatibleCommand, read);
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 44 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project pravega by pravega.

the class WireCommandsTest method testCommand.

private void testCommand(WireCommand command) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    command.writeFields(new DataOutputStream(bout));
    byte[] array = bout.toByteArray();
    WireCommand read = command.getType().readFrom(new ByteBufInputStream(Unpooled.wrappedBuffer(array)), array.length);
    assertEquals(command, read);
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 45 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project drill by axbaretto.

the class UserServerRequestHandler method handle.

@Override
public void handle(BitToUserConnection connection, int rpcType, ByteBuf pBody, ByteBuf dBody, ResponseSender responseSender) throws RpcException {
    switch(rpcType) {
        case RpcType.RUN_QUERY_VALUE:
            logger.debug("Received query to run.  Returning query handle.");
            try {
                final RunQuery query = RunQuery.PARSER.parseFrom(new ByteBufInputStream(pBody));
                final QueryId queryId = worker.submitWork(connection, query);
                responseSender.send(new Response(RpcType.QUERY_HANDLE, queryId));
                break;
            } catch (InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding RunQuery body.", e);
            }
        case RpcType.CANCEL_QUERY_VALUE:
            try {
                final QueryId queryId = QueryId.PARSER.parseFrom(new ByteBufInputStream(pBody));
                final Ack ack = worker.cancelQuery(queryId);
                responseSender.send(new Response(RpcType.ACK, ack));
                break;
            } catch (InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding QueryId body.", e);
            }
        case RpcType.RESUME_PAUSED_QUERY_VALUE:
            try {
                final QueryId queryId = QueryId.PARSER.parseFrom(new ByteBufInputStream(pBody));
                final Ack ack = worker.resumeQuery(queryId);
                responseSender.send(new Response(RpcType.ACK, ack));
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding QueryId body.", e);
            }
        case RpcType.GET_QUERY_PLAN_FRAGMENTS_VALUE:
            try {
                final GetQueryPlanFragments req = GetQueryPlanFragments.PARSER.parseFrom(new ByteBufInputStream(pBody));
                responseSender.send(new Response(RpcType.QUERY_PLAN_FRAGMENTS, worker.getQueryPlan(connection, req)));
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetQueryPlanFragments body.", e);
            }
        case RpcType.GET_CATALOGS_VALUE:
            try {
                final GetCatalogsReq req = GetCatalogsReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitCatalogMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetCatalogsReq body.", e);
            }
        case RpcType.GET_SCHEMAS_VALUE:
            try {
                final GetSchemasReq req = GetSchemasReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitSchemasMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetSchemasReq body.", e);
            }
        case RpcType.GET_TABLES_VALUE:
            try {
                final GetTablesReq req = GetTablesReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitTablesMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetTablesReq body.", e);
            }
        case RpcType.GET_COLUMNS_VALUE:
            try {
                final GetColumnsReq req = GetColumnsReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitColumnsMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetColumnsReq body.", e);
            }
        case RpcType.CREATE_PREPARED_STATEMENT_VALUE:
            try {
                final CreatePreparedStatementReq req = CreatePreparedStatementReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitPreparedStatementWork(connection, req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding CreatePreparedStatementReq body.", e);
            }
        case RpcType.GET_SERVER_META_VALUE:
            try {
                final GetServerMetaReq req = GetServerMetaReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitServerMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding CreatePreparedStatementReq body.", e);
            }
        default:
            throw new UnsupportedOperationException(String.format("UserServerRequestHandler received rpc of unknown type. Type was %d.", rpcType));
    }
}
Also used : RunQuery(org.apache.drill.exec.proto.UserProtos.RunQuery) QueryId(org.apache.drill.exec.proto.UserBitShared.QueryId) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Ack(org.apache.drill.exec.proto.GeneralRPCProtos.Ack) GetQueryPlanFragments(org.apache.drill.exec.proto.UserProtos.GetQueryPlanFragments) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) CreatePreparedStatementReq(org.apache.drill.exec.proto.UserProtos.CreatePreparedStatementReq) GetCatalogsReq(org.apache.drill.exec.proto.UserProtos.GetCatalogsReq) Response(org.apache.drill.exec.rpc.Response) GetSchemasReq(org.apache.drill.exec.proto.UserProtos.GetSchemasReq) GetServerMetaReq(org.apache.drill.exec.proto.UserProtos.GetServerMetaReq) RpcException(org.apache.drill.exec.rpc.RpcException) GetColumnsReq(org.apache.drill.exec.proto.UserProtos.GetColumnsReq) GetTablesReq(org.apache.drill.exec.proto.UserProtos.GetTablesReq)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)69 ByteBuf (io.netty.buffer.ByteBuf)22 IOException (java.io.IOException)22 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 InputStream (java.io.InputStream)10 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)6 POST (javax.ws.rs.POST)6 Test (org.junit.jupiter.api.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)4 RpcException (org.apache.drill.exec.rpc.RpcException)4 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 ObjectOutputStream (java.io.ObjectOutputStream)3