Example usage for io.netty.buffer ByteBufUtil writeUtf8

List of usage examples for io.netty.buffer ByteBufUtil writeUtf8

Introduction

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

Prototype

public static int writeUtf8(ByteBuf buf, CharSequence seq) 

Source Link

Document

Encode a CharSequence in <a href="http://en.wikipedia.org/wiki/UTF-8">UTF-8</a> and write it to a ByteBuf .

Usage

From source file:io.grpc.netty.ProtocolNegotiatorsTest.java

License:Apache License

private static ByteBuf bb(String s, Channel c) {
    return ByteBufUtil.writeUtf8(c.alloc(), s);
}

From source file:io.hekate.network.netty.ByteBufDataWriter.java

License:Apache License

@Override
public void writeUTF(String str) throws IOException {
    if (str.isEmpty()) {
        out.writeInt(0);/*from   w w w  .  j  a  v a2  s.com*/
    } else {
        int startIdx = out.writerIndex();

        // Length placeholder.
        out.ensureWritable(Integer.BYTES).writerIndex(startIdx + Integer.BYTES);

        int len = ByteBufUtil.writeUtf8(out, str);

        out.setInt(startIdx, len);
    }
}

From source file:io.netty.example.http.websocketx.benchmarkserver.WebSocketServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    HttpResponseStatus responseStatus = res.status();
    if (responseStatus.code() != 200) {
        ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
        HttpUtil.setContentLength(res, res.content().readableBytes());
    }/* ww  w.  j a  v a 2s  .co m*/
    // Send the response and close the connection if necessary.
    boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
    HttpUtil.setKeepAlive(res, keepAlive);
    ChannelFuture future = ctx.write(res); // Flushed in channelReadComplete()
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:io.netty.example.http.websocketx.server.WebSocketIndexPageHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    HttpResponseStatus responseStatus = res.status();
    if (responseStatus.code() != 200) {
        ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
        HttpUtil.setContentLength(res, res.content().readableBytes());
    }//from w  ww .j av  a 2s  . co m
    // Send the response and close the connection if necessary.
    boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
    HttpUtil.setKeepAlive(res, keepAlive);
    ChannelFuture future = ctx.writeAndFlush(res);
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.WriteCacheTest.java

License:Apache License

@Test
public void simple() throws Exception {
    WriteCache cache = new WriteCache(allocator, 10 * 1024);

    ByteBuf entry1 = allocator.buffer(1024);
    ByteBufUtil.writeUtf8(entry1, "entry-1");
    entry1.writerIndex(entry1.capacity());

    assertTrue(cache.isEmpty());/*from  w w w  . ja v  a  2  s. com*/
    assertEquals(0, cache.count());
    assertEquals(0, cache.size());

    cache.put(1, 1, entry1);

    assertFalse(cache.isEmpty());
    assertEquals(1, cache.count());
    assertEquals(entry1.readableBytes(), cache.size());

    assertEquals(entry1, cache.get(1, 1));
    assertNull(cache.get(1, 2));
    assertNull(cache.get(2, 1));

    assertEquals(entry1, cache.getLastEntry(1));
    assertEquals(null, cache.getLastEntry(2));

    cache.clear();

    assertTrue(cache.isEmpty());
    assertEquals(0, cache.count());
    assertEquals(0, cache.size());

    entry1.release();
    cache.close();
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.CreateObjInputFactory.java

License:Open Source License

@Override
public void serialize(CreateObjInput message, ByteBuf outBuffer) {
    LOG.debug("CreateObjInputFactory - message = {}", message.toString());
    StringBuilder seq = new StringBuilder("");
    //Generate from DTO to XML string
    seq.append("<msg xmlns=");
    seq.append("\"http://uri.etsi.org/ori/002-2/v4.1.1\">");
    seq.append("<header>");
    seq.append("<msgType>REQ</msgType>");
    seq.append("<msgUID>");
    seq.append(message.getXid().toString());
    seq.append("</msgUID>");
    seq.append("</header>");
    seq.append("<body>");
    seq.append("<");
    seq.append(MESSAGE_TYPE);//from w  w  w . j  a va2  s  . com
    seq.append(">");

    //Retrieve single object Id
    seq.append("<objType objTypeID=\"");
    seq.append(message.getObjType().getValue());
    seq.append("\">");
    //Retrieve name and result of params
    if (message.getParam() != null) {
        for (Param currParam : message.getParam()) {
            seq.append("<param name=\"");
            seq.append(currParam.getName());
            seq.append("\">");
            seq.append(currParam.getValue());
            seq.append("</param>");
        }
    }
    seq.append("</objType>");

    seq.append("</");
    seq.append(MESSAGE_TYPE);
    seq.append(">");
    seq.append("</body>");
    seq.append("</msg>");

    LOG.debug("CreateObjInputFactory - composed xml-string = {}", seq);
    ByteBufUtil.writeUtf8(outBuffer, seq);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.DeleteObjInputFactory.java

License:Open Source License

@Override
public void serialize(DeleteObjInput message, ByteBuf outBuffer) {
    LOG.debug("DeleteObjInputFactory - message = {}", message.toString());
    StringBuilder seq = new StringBuilder("");
    //Generate from DTO to XML string
    seq.append("<msg xmlns=");
    seq.append("\"http://uri.etsi.org/ori/002-2/v4.1.1\">");
    seq.append("<header>");
    seq.append("<msgType>REQ</msgType>");
    seq.append("<msgUID>");
    seq.append(message.getXid().toString());
    seq.append("</msgUID>");
    seq.append("</header>");
    seq.append("<body>");
    seq.append("<");
    seq.append(MESSAGE_TYPE);/*from w  w w.jav a 2 s .co m*/
    seq.append(">");
    seq.append("<obj objID=\"");
    seq.append(message.getObjId().getValue().toString());
    seq.append("\"/>");
    seq.append("</");
    seq.append(MESSAGE_TYPE);
    seq.append(">");
    seq.append("</body>");
    seq.append("</msg>");

    LOG.debug("DeleteObjInputFactory - composed xml-string = {}", seq);
    ByteBufUtil.writeUtf8(outBuffer, seq);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.GetFaultInputFactory.java

License:Open Source License

@Override
public void serialize(GetFaultInput message, ByteBuf outBuffer) {
    LOG.debug("GetFaultInputFactory - message = {}", message.toString());
    StringBuilder seq = new StringBuilder("");
    //Generate from DTO to XML string
    seq.append("<msg xmlns=");
    seq.append("\"http://uri.etsi.org/ori/002-2/v4.1.1\">");
    seq.append("<header>");
    seq.append("<msgType>REQ</msgType>");
    seq.append("<msgUID>");
    seq.append(message.getXid().toString());
    seq.append("</msgUID>");
    seq.append("</header>");
    seq.append("<body>");
    seq.append("<");
    seq.append(MESSAGE_TYPE);//w w w. jav a  2  s .  c o  m
    seq.append(">");

    //Retrieve single object Id
    seq.append("<obj objID=\"");
    seq.append(message.getObjId().getValue().toString());
    seq.append("\"/>");

    if (message.isEventDrivenReporting()) {
        seq.append("<eventDrivenReporting>true</eventDrivenReporting>");
    } else {
        seq.append("<eventDrivenReporting>false</eventDrivenReporting>");
    }

    seq.append("</");
    seq.append(MESSAGE_TYPE);
    seq.append(">");
    seq.append("</body>");
    seq.append("</msg>");

    LOG.debug("GetFaultInputFactory - composed xml-string = {}", seq);
    ByteBufUtil.writeUtf8(outBuffer, seq);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.GetParamInputFactory.java

License:Open Source License

@Override
public void serialize(GetParamInput message, ByteBuf outBuffer) {

    LOG.debug("GetParamInputFactory - message = {}", message.toString());
    StringBuilder seq = new StringBuilder("");
    //Generate from DTO to XML string
    seq.append("<msg xmlns=");
    seq.append("\"http://uri.etsi.org/ori/002-2/v4.1.1\">");
    seq.append("<header>");
    seq.append("<msgType>REQ</msgType>");
    seq.append("<msgUID>");
    seq.append(message.getXid().toString());
    seq.append("</msgUID>");
    seq.append("</header>");
    seq.append("<body>");
    seq.append("<");
    seq.append(MESSAGE_TYPE);/*from   www . ja v  a 2s .c  o  m*/
    seq.append(">");

    //Retrieve single objId
    seq.append("<obj objID=\"");
    seq.append(message.getObjId().getValue().toString());
    seq.append("\">");
    //Retrieve value of paramName
    seq.append("<param name=\"");
    seq.append(message.getParamName().toString());
    seq.append("\"/>");
    seq.append("</obj>");

    seq.append("</");
    seq.append(MESSAGE_TYPE);
    seq.append(">");
    seq.append("</body>");
    seq.append("</msg>");

    LOG.debug("GetParamInputFactory - composed xml-string = {}", seq);
    ByteBufUtil.writeUtf8(outBuffer, seq);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.GetStateInputFactory.java

License:Open Source License

@Override
public void serialize(GetStateInput message, ByteBuf outBuffer) {
    LOG.debug("GetStateInputFactory - message = {}", message.toString());
    StringBuilder seq = new StringBuilder("");
    //Generate from DTO to XML string
    seq.append("<msg xmlns=");
    seq.append("\"http://uri.etsi.org/ori/002-2/v4.1.1\">");
    seq.append("<header>");
    seq.append("<msgType>REQ</msgType>");
    seq.append("<msgUID>");
    seq.append(message.getXid().toString());
    seq.append("</msgUID>");
    seq.append("</header>");
    seq.append("<body>");
    seq.append("<");
    seq.append(MESSAGE_TYPE);// w  w  w  . j a  v  a 2 s  . co  m
    seq.append(">");

    //Retrieve single object Id
    seq.append("<obj objID=\"");
    seq.append(message.getObjId().getValue().toString());
    seq.append("\">");
    //Retrieve single stateType
    seq.append("<state type=\"");
    seq.append(message.getStateType().toString());
    seq.append("\"/>");
    seq.append("</obj>");

    if (message.isEventDrivenReporting()) {
        seq.append("<eventDrivenReporting>true</eventDrivenReporting>");
    } else {
        seq.append("<eventDrivenReporting>false</eventDrivenReporting>");
    }

    seq.append("</");
    seq.append(MESSAGE_TYPE);
    seq.append(">");
    seq.append("</body>");
    seq.append("</msg>");

    LOG.debug("GetStateInputFactory - composed xml-string = {}", seq);
    ByteBufUtil.writeUtf8(outBuffer, seq);
}