Search in sources :

Example 61 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project ArsMagica2 by Mithion.

the class AMPacketProcessorServer method onServerPacketData.

@SubscribeEvent
public void onServerPacketData(ServerCustomPacketEvent event) {
    ByteBufInputStream bbis = new ByteBufInputStream(event.packet.payload());
    byte packetID = -1;
    try {
        if (event.packet.getTarget() != Side.SERVER) {
            return;
        }
        // constant details all packets share:  ID, player, and remaining data
        packetID = bbis.readByte();
        NetHandlerPlayServer srv = (NetHandlerPlayServer) event.packet.handler();
        EntityPlayerMP player = srv.playerEntity;
        byte[] remaining = new byte[bbis.available()];
        bbis.readFully(remaining);
        switch(packetID) {
            case AMPacketIDs.SPELL_SHAPE_GROUP_CHANGE:
                handleCastingModeChange(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.MAGIC_LEVEL_UP:
                handleMagicLevelUp(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.SYNC_BETA_PARTICLES:
                handleSyncBetaParticles(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.POSSIBLE_CLIENT_EXPROP_DESYNC:
                handlePossibleClientExpropDesync(remaining);
                break;
            case AMPacketIDs.REQUEST_BETA_PARTICLES:
                handleRequestBetaParticles(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.SPELL_CUSTOMIZE:
                handleSpellCustomize(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.SPELLBOOK_CHANGE_ACTIVE_SLOT:
                handleSpellBookChangeActiveSlot(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.SYNC_SPELL_KNOWLEDGE:
                handleSyncSpellKnowledge(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.DECO_BLOCK_UPDATE:
                handleDecoBlockUpdate(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.INSCRIPTION_TABLE_UPDATE:
                handleInscriptionTableUpdate(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.TK_DISTANCE_SYNC:
                ExtendedProperties.For((EntityPlayerMP) player).TK_Distance = new AMDataReader(remaining).getFloat();
                break;
            case AMPacketIDs.SAVE_KEYSTONE_COMBO:
                handleSaveKeystoneCombo(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.SET_KEYSTONE_COMBO:
                handleSetKeystoneCombo(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.SET_MAG_WORK_REC:
                handleSetMagiciansWorkbenchRecipe(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.RUNE_BAG_GUI_OPEN:
                handleRuneBagGUIOpen(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.M_BENCH_LOCK_RECIPE:
                handleMBenchLockRecipe(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.IMBUE_ARMOR:
                handleImbueArmor(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.REQUEST_PWR_PATHS:
                handlePowerPathSync(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.SYNC_EXTENDED_PROPS:
                handleExpropOperation(remaining, (EntityPlayerMP) player);
                break;
            case AMPacketIDs.AFFINITY_ACTIVATE:
                handleAffinityActivate(remaining, player);
                break;
        }
    } catch (Throwable t) {
        LogHelper.error("Server Packet Failed to Handle!");
        LogHelper.error("Packet Type: " + packetID);
        t.printStackTrace();
    } finally {
        try {
            if (bbis != null)
                bbis.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
Also used : EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 62 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project netty by netty.

the class ObjectDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }
    ObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver);
    try {
        return ois.readObject();
    } finally {
        ois.close();
    }
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectInputStream(java.io.ObjectInputStream)

Example 63 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project netty by netty.

the class SslContext method getCertificatesFromBuffers.

private static X509Certificate[] getCertificatesFromBuffers(ByteBuf[] certs) throws CertificateException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] x509Certs = new X509Certificate[certs.length];
    try {
        for (int i = 0; i < certs.length; i++) {
            InputStream is = new ByteBufInputStream(certs[i], false);
            try {
                x509Certs[i] = (X509Certificate) cf.generateCertificate(is);
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // This is not expected to happen, but re-throw in case it does.
                    throw new RuntimeException(e);
                }
            }
        }
    } finally {
        for (ByteBuf buf : certs) {
            buf.release();
        }
    }
    return x509Certs;
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 64 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project netty by netty.

the class DiskFileUploadTest method testSetContentFromInputStream.

@Test
public void testSetContentFromInputStream() throws Exception {
    String json = "{\"hello\":\"world\",\"foo\":\"bar\"}";
    DiskFileUpload f1 = new DiskFileUpload("file3", "file3", "application/json", null, null, 0);
    try {
        byte[] bytes = json.getBytes(CharsetUtil.UTF_8);
        ByteBuf buf = Unpooled.wrappedBuffer(bytes);
        InputStream is = new ByteBufInputStream(buf);
        try {
            f1.setContent(is);
            assertEquals(json, f1.getString());
            assertArrayEquals(bytes, f1.get());
            File file = f1.getFile();
            assertEquals((long) bytes.length, file.length());
            assertArrayEquals(bytes, doReadFile(file, bytes.length));
        } finally {
            buf.release();
            is.close();
        }
    } finally {
        f1.delete();
    }
}
Also used : FileInputStream(java.io.FileInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 65 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project netty by netty.

the class AbstractMemoryHttpDataTest method testSetContentFromStream.

/**
 * Provide content into HTTP data with input stream.
 *
 * @throws Exception In case of any exception.
 */
@Test
public void testSetContentFromStream() throws Exception {
    // definedSize=0
    TestHttpData test = new TestHttpData("test", UTF_8, 0);
    String contentStr = "foo_test";
    ByteBuf buf = Unpooled.wrappedBuffer(contentStr.getBytes(UTF_8));
    buf.markReaderIndex();
    ByteBufInputStream is = new ByteBufInputStream(buf);
    try {
        test.setContent(is);
        assertFalse(buf.isReadable());
        assertEquals(test.getString(UTF_8), contentStr);
        buf.resetReaderIndex();
        assertTrue(ByteBufUtil.equals(buf, test.getByteBuf()));
    } finally {
        is.close();
    }
    Random random = new SecureRandom();
    for (int i = 0; i < 20; i++) {
        // Generate input data bytes.
        int size = random.nextInt(Short.MAX_VALUE);
        byte[] bytes = new byte[size];
        random.nextBytes(bytes);
        // Generate parsed HTTP data block.
        TestHttpData data = new TestHttpData("name", UTF_8, 0);
        data.setContent(new ByteArrayInputStream(bytes));
        // Validate stored data.
        ByteBuf buffer = data.getByteBuf();
        assertEquals(0, buffer.readerIndex());
        assertEquals(bytes.length, buffer.writerIndex());
        assertArrayEquals(bytes, Arrays.copyOf(buffer.array(), bytes.length));
        assertArrayEquals(bytes, data.get());
    }
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureRandom(java.security.SecureRandom) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

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