Example usage for io.netty.buffer Unpooled copiedBuffer

List of usage examples for io.netty.buffer Unpooled copiedBuffer

Introduction

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

Prototype

public static ByteBuf copiedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new buffer whose content is a merged copy of the specified buffers ' slices.

Usage

From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java

License:Open Source License

/**
 * Gets the integer chunk./*from   w w w .j  a v a  2 s  . c  o m*/
 *
 * @param chunk
 *          the chunk
 * @return the integer chunk
 */
protected static int getIntegerChunk(byte[] chunk) {
    ByteBuf buf = Unpooled.copiedBuffer(chunk);

    return buf.getInt(0);
}

From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java

License:Open Source License

/**
 * Gets the short chunk.//from  www . j  av a 2 s.  com
 *
 * @param chunk
 *          the chunk
 * @return the short chunk
 */
protected static int getShortChunk(byte[] chunk) {
    ByteBuf buf = Unpooled.copiedBuffer(chunk);

    return buf.getShort(0);
}

From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java

License:Open Source License

/**
 * Gets the long chunk./*from w  w  w . j av  a  2 s  . c o  m*/
 *
 * @param chunk
 *          the chunk
 * @return the long chunk
 */
protected static long getLongChunk(byte[] chunk) {
    ByteBuf buf = Unpooled.copiedBuffer(chunk);

    return buf.getLong(0);
}

From source file:com.github.rmannibucau.featuredmock.http.FeaturedHandler.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest request) throws Exception {
    if (!request.getDecoderResult().isSuccess()) {
        sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        return;/*from  w w w  .jav  a 2  s .c  om*/
    }

    InputStream stream = null;
    String type = null;

    final String accept = request.headers().get(HttpHeaders.Names.ACCEPT);
    if (accept != null) {
        for (final String a : accept.split(",")) {
            if (mappers != null) {
                for (final ContentTypeMapper mapper : mappers) {
                    if (mapper.handle(a)) {
                        final String extension = mapper.extension(a);
                        stream = findResource(request, extension);
                        if (stream == null && extension != null && !extension.startsWith(".")) {
                            stream = findResource(request, "." + extension);
                        }
                        if (stream != null) {
                            type = mapper.contentType(a);
                            break;
                        }
                    }
                }
            }

            if (stream == null) {
                for (final String ext : EXTENSIONS) {
                    if (a.contains(ext.substring(1))) {
                        stream = findResource(request, ext);
                        if (stream != null) {
                            type = "application/" + ext.substring(1); // remove dot
                        }
                        break;
                    }
                }
            }

            if (stream != null) {
                break;
            }
        }
    }

    if (stream == null) { // not found from content type so try all extensions
        for (final String ext : EXTENSIONS) {
            stream = findResource(request, ext);
            if (stream != null) {
                type = "application/" + ext.substring(1); // remove dot
                break;
            }
        }
    }

    if (stream == null) { // try without extension
        stream = findResource(request, "");
        if (stream != null) {
            type = "text/plain";
        }
    }

    if (stream == null) {
        sendError(ctx, HttpResponseStatus.NOT_FOUND);
        return;
    }

    final byte[] bytes = IOs.slurp(stream);
    final HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            Unpooled.copiedBuffer(bytes));
    HttpHeaders.setContentLength(response, bytes.length);
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, type);
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToStringWhenOctetStringSizeIsMinus1ReturnsEmptyString() throws Exception {
    byte[] input = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    String decodedString = ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    assertEquals("Decoded string size is not 0", 0, decodedString.length());

    inputBuffer.release();/*from  w  w  w.j  a v a 2  s .  c  o  m*/
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = StringSizeException.class)
public void testReadOctetStringToStringWhenOctetStringSizeIsOtherNegativeNumberThrowsStringSizeException()
        throws Exception {
    byte[] input = { (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00 };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    try {//from  w  ww.j  a  v a  2 s  .  com
        ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    } finally {
        inputBuffer.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = StringSizeException.class)
public void testReadOctetStringToStringWhenOctetStringSizeIsGreaterThanBufferSizeThrowsStringSizeException()
        throws Exception {
    byte[] input = { 0x00, 0x00, 0x00, 0x02, 'a' };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    try {/*from   www.  jav a2  s  .com*/
        ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    } finally {
        inputBuffer.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToString_when_octet_string_size_is_zero_returns_empty_string() throws Exception {
    byte[] input = { 0x00, 0x00, 0x00, 0x00 };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    String decodedString = ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    assertEquals("Decoded string size is not 0", 0, decodedString.length());
    inputBuffer.release();//from   w  ww  . j  a v  a  2  s.  c om
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = NotEnoughDataDecoderException.class)
public void testReadIntWhenInputBufferIsSmallThan4BytesThrowsNotEnoughDataException() throws Exception {
    byte[] input = { 0x00, 0x00, 0x00 };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);
    try {//from   w ww .  java  2 s  .c  o  m
        ChannelBufferUtils.readInt(inputBuffer);
    } finally {
        inputBuffer.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadIntWhenInputBufferCorrectlyReadInteger() throws Exception {
    byte[] input = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xf0 };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    int readInt = ChannelBufferUtils.readInt(inputBuffer);

    assertEquals("Encoded and decoded ints are not the same", -16, readInt);

    inputBuffer.release();// w w w .  j  a v  a  2 s  . c o m
}