Example usage for io.netty.util CharsetUtil UTF_8

List of usage examples for io.netty.util CharsetUtil UTF_8

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil UTF_8.

Prototype

Charset UTF_8

To view the source code for io.netty.util CharsetUtil UTF_8.

Click Source Link

Document

8-bit UTF (UCS Transformation Format)

Usage

From source file:bzh.ygu.fun.chitchat.HttpChitChatServerHandler.java

License:Apache License

private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
    // Decide whether to close the connection or not.
    boolean keepAlive = HttpHeaders.isKeepAlive(request);
    // Build the response object.
    FullHttpResponse response;/* w w w .  j av  a 2s .  c om*/
    if (currentAction.equals("Add"))
        response = new DefaultFullHttpResponse(HTTP_1_1,
                currentObj.decoderResult().isSuccess() ? CREATED : BAD_REQUEST);
    else if (currentAction.equals("Latest")) {
        response = new DefaultFullHttpResponse(HTTP_1_1,
                currentObj.decoderResult().isSuccess() ? OK : BAD_REQUEST,
                Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
        response.headers().set(CONTENT_TYPE, "application/json; charset=utf-8");
    } else
        response = new DefaultFullHttpResponse(HTTP_1_1,
                currentObj.decoderResult().isSuccess() ? OK : BAD_REQUEST,
                Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

    // response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }

    // Write the response.
    ctx.write(response);

    return keepAlive;

}

From source file:c5db.log.LogTestUtil.java

License:Apache License

public static OLogEntry makeEntry(long seqNum, long term, String stringData) {
    return makeEntry(seqNum, term, ByteBuffer.wrap(stringData.getBytes(CharsetUtil.UTF_8)));
}

From source file:c5db.replication.InRamTest.java

License:Apache License

private static List<ByteBuffer> someData() {
    return Lists.newArrayList(ByteBuffer.wrap("test".getBytes(CharsetUtil.UTF_8)));
}

From source file:c5db.replication.NioQuorumFileReaderWriter.java

License:Apache License

@Override
public List<String> readQuorumFile(String quorumId, String fileName) throws IOException {
    Path filePath = basePath.resolve(quorumId).resolve(fileName);

    try {//from  w  ww  . j a  v a  2  s  .c  om
        return Files.readAllLines(filePath, CharsetUtil.UTF_8);
    } catch (NoSuchFileException ex) {
        return new ArrayList<>();
    }
}

From source file:c5db.replication.NioQuorumFileReaderWriter.java

License:Apache License

@Override
public void writeQuorumFile(String quorumId, String fileName, List<String> data) throws IOException {
    Path dirPath = basePath.resolve(quorumId);

    Files.createDirectories(dirPath);

    Path filePath = dirPath.resolve(fileName);
    Files.write(filePath, data, CharsetUtil.UTF_8);
}

From source file:c5db.replication.ReplicatorTestUtil.java

License:Apache License

public static LogEntry makeProtostuffEntry(long index, long term, String stringData) {
    return makeProtostuffEntry(index, term, ByteBuffer.wrap(stringData.getBytes(CharsetUtil.UTF_8)));
}

From source file:c5db.util.CrcInputStreamTest.java

License:Apache License

private static byte[] getUtf8Bytes(String string) {
    return string.getBytes(CharsetUtil.UTF_8);
}

From source file:ca.lambtoncollege.netty.webSocket.ClientHandlerWebSocket.java

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();//from   w  w  w  .  j  ava  2 s .c  o m
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        System.out.println("WebSocket Client connected!");
        handshakeFuture.setSuccess();
        return;
    }

    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus()
                + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }

    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        System.out.println("WebSocket Client received message: " + textFrame.text());
    } else if (frame instanceof PongWebSocketFrame) {
        System.out.println("WebSocket Client received pong");
    } else if (frame instanceof CloseWebSocketFrame) {
        System.out.println("WebSocket Client received closing");
        ch.close();
    }
}

From source file:ca.lambtoncollege.netty.webSocket.ServerHandlerWebSocket.java

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);//from  w  w w . j  a  va2s  . c o m
        buf.release();
        HttpHeaders.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:catacumba.websocket.WebSockets.java

License:Apache License

/**
 * Sets up a websocket that sends the published Strings to a client.
 * <p>/*from   w  w  w.j  ava2s. co  m*/
 * This takes the place of a {@link Context#stream(Publisher)} call.
 *
 * @param context the request handling context
 * @param broadcaster a {@link Publisher} of Strings to send to the websocket client
 */
public static void websocketBroadcast(final Context context, final Publisher<String> broadcaster) {
    ByteBufAllocator bufferAllocator = context.get(ByteBufAllocator.class);
    websocketByteBufBroadcast(context, Streams.map(broadcaster,
            s -> ByteBufUtil.encodeString(bufferAllocator, CharBuffer.wrap(s), CharsetUtil.UTF_8)));
}