Example usage for io.netty.buffer ByteBufOutputStream buffer

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

Introduction

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

Prototype

ByteBuf buffer

To view the source code for io.netty.buffer ByteBufOutputStream buffer.

Click Source Link

Usage

From source file:org.msgpack.rpc.loop.netty.NettyTcpClientTransport.java

License:Apache License

@Override
protected void flushPendingBuffer(ByteBufOutputStream b, Channel c) {
    c.writeAndFlush(b.buffer());
    b.buffer().clear();

}

From source file:org.msgpack.rpc.loop.netty.NettyTcpClientTransport.java

License:Apache License

@Override
protected void closePendingBuffer(ByteBufOutputStream b) {
    b.buffer().clear();

}

From source file:uk.co.thinkofdeath.thinkcraft.bukkit.web.InternalWebServer.java

License:Apache License

@Override
public void handle(ChannelHandlerContext context, URI uri, FullHttpRequest request) throws Exception {
    if (request.getMethod() != HttpMethod.GET) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED));
        return;//from   w  w  w  . j  a  va  2s  . co  m
    }
    String modified = request.headers().get(IF_MODIFIED_SINCE);
    if (modified != null && !modified.isEmpty()) {
        Date modifiedDate = format.parse(modified);

        if (modifiedDate.equals(plugin.getStartUpDate())) {
            sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED));
            return;
        }
    }

    String path = uri.getPath();
    if (path.equals("/")) {
        path = "/index.html";
    }

    ByteBuf buffer;
    try (InputStream stream = this.getClass().getClassLoader().getResourceAsStream("www" + path)) {
        if (stream == null) {
            sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND));
            return;
        }
        ByteBufOutputStream out = new ByteBufOutputStream(context.alloc().buffer());
        IOUtils.copy(stream, out);
        buffer = out.buffer();
        out.close();
    }

    if (path.equals("/index.html")) {
        String page = buffer.toString(Charsets.UTF_8);
        page = page.replaceAll("%SERVERPORT%", Integer.toString(plugin.getConfiguration().getPort()));
        buffer.release();
        buffer = Unpooled.wrappedBuffer(page.getBytes(Charsets.UTF_8));
    }

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, buffer);

    response.headers().set(DATE, format.format(new Date()));
    response.headers().set(LAST_MODIFIED, format.format(plugin.getStartUpDate()));

    String ext = path.substring(path.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);
    sendHttpResponse(context, request, response);
}

From source file:uk.co.thinkofdeath.thinkcraft.bukkit.web.ResourcesServer.java

License:Apache License

@Override
public void handle(ChannelHandlerContext context, URI uri, FullHttpRequest request) throws Exception {
    File file = new File(plugin.getResourceDir(), uri.getPath().substring("/resources/".length()));
    if (!file.exists()) {
        sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND));
        return;// w  ww  .  ja  v a 2 s  . c  om
    }

    String modified = request.headers().get(IF_MODIFIED_SINCE);
    if (modified != null && !modified.isEmpty()) {
        Date modifiedDate = format.parse(modified);

        if (modifiedDate.equals(plugin.getStartUpDate())) {
            sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED));
            return;
        }
    }

    ByteBuf buffer;
    try (InputStream stream = new FileInputStream(file)) {
        ByteBufOutputStream out = new ByteBufOutputStream(context.alloc().buffer());
        IOUtils.copy(stream, out);
        buffer = out.buffer();
        out.close();
    }

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, buffer);

    response.headers().set(DATE, format.format(new Date()));
    response.headers().set(LAST_MODIFIED, format.format(plugin.getStartUpDate()));

    if (uri.getPath().startsWith("/resources/assets")) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, 1);
        response.headers().set(EXPIRES, format.format(calendar.getTime()));
        response.headers().set(CACHE_CONTROL, "public, max-age=2592000");
    }

    String path = uri.getPath();
    String ext = path.substring(path.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);
    response.headers().add("Access-Control-Allow-Origin", "*");
    sendHttpResponse(context, request, response);
}