Example usage for io.netty.buffer ByteBufOutputStream ByteBufOutputStream

List of usage examples for io.netty.buffer ByteBufOutputStream ByteBufOutputStream

Introduction

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

Prototype

public ByteBufOutputStream(ByteBuf buffer) 

Source Link

Document

Creates a new stream which writes data to the specified buffer .

Usage

From source file:de.sanandrew.core.manpack.network.NetworkManager.java

License:Creative Commons License

private static void sendPacketTo(String modId, short packet, EnumPacketDirections direction, Tuple dirData,
        Tuple packetData) {//from  ww  w. j  a  va 2  s. c  o  m
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(Unpooled.buffer())) {
        bbos.writeShort(packet);
        IPacket pktInst = getPacketProcessor(modId).getPacketCls(packet).getConstructor().newInstance();
        FMLEventChannel channel = getPacketChannel(modId);
        pktInst.writeData(bbos, packetData);
        FMLProxyPacket proxyPacket = new FMLProxyPacket(bbos.buffer(), getPacketChannelName(modId));
        switch (direction) {
        case TO_SERVER:
            channel.sendToServer(proxyPacket);
            break;
        case TO_ALL:
            channel.sendToAll(proxyPacket);
            break;
        case TO_PLAYER:
            channel.sendTo(proxyPacket, (EntityPlayerMP) dirData.getValue(0));
            break;
        case TO_ALL_IN_RANGE:
            channel.sendToAllAround(proxyPacket,
                    new NetworkRegistry.TargetPoint((int) dirData.getValue(0), (double) dirData.getValue(1),
                            (double) dirData.getValue(2), (double) dirData.getValue(3),
                            (double) dirData.getValue(4)));
            break;
        case TO_ALL_IN_DIMENSION:
            channel.sendToDimension(proxyPacket, (int) dirData.getValue(0));
            break;
        }
    } catch (IOException ioe) {
        ManPackLoadingPlugin.MOD_LOG.log(Level.ERROR, "The packet ID %d from %s cannot be processed!", packet,
                modId);
        ioe.printStackTrace();
    } catch (IllegalAccessException | InstantiationException rex) {
        ManPackLoadingPlugin.MOD_LOG.log(Level.ERROR, "The packet ID %d from %s cannot be instantiated!",
                packet, modId);
        rex.printStackTrace();
    } catch (NoSuchMethodException | InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:de.sanandrew.mods.particledeco.network.PacketProcessor.java

License:Creative Commons License

private static void sendPacketTo(short packetId, PacketDirections direction, Tuple dirData, Tuple packetData) {
    try (ByteBufOutputStream bbos = new ByteBufOutputStream(Unpooled.buffer())) {
        if (ID_TO_PACKET_MAP_.containsKey(packetId)) {
            bbos.writeShort(packetId);//from w ww .ja  v  a 2 s .c  o  m
            IPacket pktInst = ID_TO_PACKET_MAP_.get(packetId).newInstance();
            pktInst.writeData(bbos, packetData);
            FMLProxyPacket packet = new FMLProxyPacket(bbos.buffer(), PDM_Main.MOD_CHANNEL);
            switch (direction) {
            case TO_SERVER:
                PDM_Main.channel.sendToServer(packet);
                break;
            case TO_ALL:
                PDM_Main.channel.sendToAll(packet);
                break;
            case TO_PLAYER:
                PDM_Main.channel.sendTo(packet, (EntityPlayerMP) dirData.getValue(0));
                break;
            case TO_ALL_IN_RANGE:
                PDM_Main.channel.sendToAllAround(packet,
                        new NetworkRegistry.TargetPoint((int) dirData.getValue(0), (double) dirData.getValue(1),
                                (double) dirData.getValue(2), (double) dirData.getValue(3),
                                (double) dirData.getValue(4)));
                break;
            case TO_ALL_IN_DIMENSION:
                PDM_Main.channel.sendToDimension(packet, (int) dirData.getValue(0));
                break;
            }
        }
    } catch (IOException ioe) {
        FMLLog.log(PDM_Main.MOD_LOG, Level.ERROR, "The packet with the ID %d cannot be processed!", packetId);
        ioe.printStackTrace();
    } catch (IllegalAccessException | InstantiationException rex) {
        FMLLog.log(PDM_Main.MOD_LOG, Level.ERROR, "The packet with the ID %d cannot be instantiated!",
                packetId);
        rex.printStackTrace();
    }
}

From source file:deathcap.wsmc.web.HTTPHandler.java

License:Apache License

public void httpRequest(ChannelHandlerContext context, FullHttpRequest request) throws IOException {
    if (!request.getDecoderResult().isSuccess()) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;/* w  w  w .ja  va  2s .com*/
    }
    if (request.getUri().equals("/server")) {
        context.fireChannelRead(request);
        return;
    }

    if ((request.getMethod() == OPTIONS || request.getMethod() == HEAD) && request.getUri().equals("/chunk")) {
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
        response.headers().add("Access-Control-Allow-Origin", "*");
        response.headers().add("Access-Control-Allow-Methods", "POST");
        if (request.getMethod() == OPTIONS) {
            response.headers().add("Access-Control-Allow-Headers", "origin, content-type, accept");
        }
        sendHttpResponse(context, request, response);
    }

    if (request.getMethod() != GET) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return;
    }

    // TODO: send browserified page
    if (request.getUri().equals("/")) {
        request.setUri("/index.html");
    }

    InputStream stream = null;
    /*
    if (request.getUri().startsWith("/resources/")) {
    File file = new File(
            plugin.getResourceDir(),
            request.getUri().substring("/resources/".length())
    );
    stream = new FileInputStream(file);
    } else {
    */
    stream = this.getClass().getClassLoader().getResourceAsStream("www" + request.getUri());
    if (stream == null) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND));
        return;
    }
    ByteBufOutputStream out = new ByteBufOutputStream(Unpooled.buffer());
    copyStream(stream, out);
    stream.close();
    out.close();

    ByteBuf buffer = out.buffer();
    if (request.getUri().equals("/index.html")) {
        String page = buffer.toString(CharsetUtil.UTF_8);
        page = page.replaceAll("%SERVERPORT%", Integer.toString(this.port));
        buffer.release();
        buffer = Unpooled.wrappedBuffer(page.getBytes(CharsetUtil.UTF_8));
    }

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, buffer);
    if (request.getUri().startsWith("/resources/")) {
        response.headers().add("Access-Control-Allow-Origin", "*");
    }

    String ext = request.getUri().substring(request.getUri().lastIndexOf('.') + 1);
    String type = mimeTypes.containsKey(ext) ? mimeTypes.get(ext) : "text/plain";
    if (type.startsWith("text/")) {
        type += "; charset=UTF-8";
    }
    response.headers().set(CONTENT_TYPE, type);
    setContentLength(response, response.content().readableBytes());
    sendHttpResponse(context, request, response);

}

From source file:hellfirepvp.astralsorcery.common.util.ByteBufUtils.java

License:Open Source License

public static void writeNBTTag(ByteBuf byteBuf, @Nonnull NBTTagCompound tag) {
    try (DataOutputStream dos = new DataOutputStream(new ByteBufOutputStream(byteBuf))) {
        CompressedStreamTools.write(tag, dos);
    } catch (Exception exc) {
    }// www .j a v a2s.  c  om
}

From source file:herddb.log.LogEntry.java

License:Apache License

public ByteBuf serializeAsByteBuf() {
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.directBuffer(DEFAULT_BUFFER_SIZE);
    try (ExtendedDataOutputStream doo = new ExtendedDataOutputStream(new ByteBufOutputStream(buffer))) {
        serialize(doo);//from  w  w w .ja  va 2s .c om
        return buffer;
    } catch (IOException err) {
        throw new RuntimeException(err);
    }
}

From source file:holon.internal.http.netty.NettyOutput.java

License:Open Source License

@Override
public Writer asWriter() {
    buffer = channel.alloc().buffer();/* w w w  .  jav a2s.c  om*/
    return new OutputStreamWriter(new ByteBufOutputStream(buffer), UTF_8);
}

From source file:io.liveoak.common.codec.html.HTMLEncoder.java

License:Open Source License

@Override
public void initialize(ByteBuf buffer) throws Exception {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    this.writer = factory.createXMLEventWriter(new ByteBufOutputStream(buffer));
    this.eventFactory = XMLEventFactory.newFactory();

    startTag("html");
    startTag("head");
    startTag("title");
    text("LiveOak");
    endTag("title");

    startTag("link");
    attribute("rel", "stylesheet");
    attribute("type", "text/css");
    attribute("href", "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css");
    endTag("link");

    startTag("style");
    text(getCss());/*from www  . j a v a  2 s.  co  m*/
    endTag("style");

    endTag("head");
    startTag("body");
}

From source file:io.liveoak.common.codec.json.JSONEncoder.java

License:Open Source License

@Override
public void initialize(ByteBuf buffer) throws Exception {
    JsonFactory factory = new JsonFactory();
    ByteBufOutputStream out = new ByteBufOutputStream(buffer);
    this.generator = factory.createGenerator(out);
    this.generator.setPrettyPrinter(new DefaultPrettyPrinter("\\n"));
}

From source file:io.liveoak.container.BasicServerTest.java

License:Open Source License

protected ResourceState decode(HttpResponse response) throws Exception {
    ByteBuf buffer = Unpooled.buffer();// ww  w  .ja va 2  s .c o  m
    ByteBufOutputStream out = new ByteBufOutputStream(buffer);
    response.getEntity().writeTo(out);
    out.flush();
    out.close();
    System.err.println("===================");
    System.err.println(buffer.toString(Charset.defaultCharset()));
    System.err.println("===================");
    return system.codecManager().decode(MediaType.JSON, buffer);
}

From source file:io.mycat.netty.ProtocolTransport.java

License:Apache License

public ProtocolTransport(Channel channel, ByteBuf in) {
    this.channel = channel;
    this.in = in;
    this.out = channel.alloc().buffer(DEFAULT_BUFFER_SIZE);
    this.input = new ByteBufInputStream(in);
    this.output = new ByteBufOutputStream(out);
}