List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte buffer argument as if it was an int value and returns the * result. Throws NumberFormatException if the byte array does not represent an * int quantity. The second argument specifies the radix to use when parsing * the value./*from w w w . j a v a2s . c om*/ * * @param radix the base to use for conversion. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an int quantity. */ public static int parseInt(ByteBuf bytes, int start, int length, int radix) { if (!PlatformDependent.hasUnsafe()) { return parseInt(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); int offset = start; boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-'; if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') { offset++; if (length == 1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return parseIntInternal(bytes, memoryAddress, start, length, offset, radix, negative); }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte buffer argument as if it was an long value and returns the * result. Throws NumberFormatException if the string does not represent an * long quantity. The second argument specifies the radix to use when parsing * the value./*www . j ava2s.c o m*/ * * @param bytes the string byte buffer * @param start * @param length a UTF-8 encoded string representation of a long quantity. * @param radix the base to use for conversion. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an long quantity. */ public static long parseLong(ByteBuf bytes, int start, int length, int radix) { if (!PlatformDependent.hasUnsafe()) { return parseInt(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); int offset = start; boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-'; if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') { offset++; if (length == 1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return parseLongInternal(bytes, memoryAddress, start, length, offset, radix, negative); }
From source file:org.ballerinalang.test.service.http2.sample.EchoServiceSampleTestCase.java
License:Open Source License
@Test(description = "Test echo service sample test case invoking base path") public void testEchoServiceByBasePath() throws Exception { DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "/echo"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); ByteBuf buffer = request.content().clear(); int p0 = buffer.writerIndex(); buffer.writeBytes(requestMessage.getBytes()); int p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); int send = http2Client.send(request); FullHttpResponse response = http2Client.getResponse(send); //request should be returned as response Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), requestMessage, "Message content mismatched"); }
From source file:org.ballerinalang.test.service.http2.sample.EchoServiceSampleTestCase.java
License:Open Source License
@Test(description = "Test echo service sample test case") public void testEchoServiceByResourcePath() throws Exception { DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "/echo/resource"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); ByteBuf buffer = request.content().clear(); int p0 = buffer.writerIndex(); buffer.writeBytes(requestMessage.getBytes()); int p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); int send = http2Client.send(request); FullHttpResponse response = http2Client.getResponse(send); //request should be returned as response Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), requestMessage, "Message content mismatched"); }
From source file:org.ballerinalang.test.service.http2.sample.EcommerceSampleTestCase.java
License:Open Source License
@Test(description = "Test resource POST orders in E-Commerce sample") public void testPostOrder() throws Exception { String requestMessage = "{\"Order\":{\"ID\":\"111222\",\"Name\":\"XYZ123\"}}"; DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "ecommerceservice/orders"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); ByteBuf buffer = request.content().clear(); int p0 = buffer.writerIndex(); buffer.writeBytes(requestMessage.getBytes()); int p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); int send = http2Client.send(request); FullHttpResponse response = http2Client.getResponse(send); Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), "{\"Status\":\"Order is successfully added.\"}", "Message content mismatched"); }
From source file:org.ballerinalang.test.service.http2.sample.EcommerceSampleTestCase.java
License:Open Source License
@Test(description = "Test resource POST products in E-Commerce sample") public void testPostProduct() throws Exception { String requestMessage = "{\"Product\":{\"ID\":\"123345\",\"Name\":\"PQR\"}}"; DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "/ecommerceservice/products"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); ByteBuf buffer = request.content().clear(); int p0 = buffer.writerIndex(); buffer.writeBytes(requestMessage.getBytes()); int p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); int send = http2Client.send(request); FullHttpResponse response = http2Client.getResponse(send); Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), "{\"Status\":\"Product is successfully added.\"}", "Message content mismatched"); }
From source file:org.ballerinalang.test.service.http2.sample.EcommerceSampleTestCase.java
License:Open Source License
@Test(description = "Test resource POST customers in E-Commerce sample") public void testPostCustomers() throws Exception { String requestMessage = "{\"Customer\":{\"ID\":\"97453\",\"Name\":\"ABC XYZ\"}}"; DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "/ecommerceservice/customers"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); ByteBuf buffer = request.content().clear(); int p0 = buffer.writerIndex(); buffer.writeBytes(requestMessage.getBytes()); int p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); int send = http2Client.send(request); FullHttpResponse response = http2Client.getResponse(send); Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), "{\"Status\":\"Customer is successfully added.\"}", "Message content mismatched"); }
From source file:org.ballerinalang.test.service.http2.sample.RoutingServiceSampleTestCase.java
License:Open Source License
@Test(description = "Test Content base routing sample") public void testContentBaseRouting() throws Exception { DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "/cbr"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); ByteBuf buffer = request.content().clear(); int p0 = buffer.writerIndex(); buffer.writeBytes(requestNyseMessage.getBytes()); int p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); int send = http2Client.send(request); FullHttpResponse response = http2Client.getResponse(send); Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE.toLowerCase()), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), responseNyseMessage, "Message content mismatched. " + "Routing failed for nyse"); //sending nasdaq as name request = new DefaultFullHttpRequest(HTTP_1_1, POST, "/cbr"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); buffer = request.content().clear();/*from w w w. java 2s . co m*/ p0 = buffer.writerIndex(); buffer.writeBytes(requestNasdaqMessage.getBytes()); p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); send = http2Client.send(request); response = http2Client.getResponse(send); Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE.toLowerCase()), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), responseNasdaqMessage, "Message content mismatched. " + "Routing failed for nasdaq"); }
From source file:org.ballerinalang.test.service.http2.sample.ServiceChainingSampleTestCase.java
License:Open Source License
@Test(description = "Test service chaining sample") public void testEchoServiceByBasePath() throws Exception { DefaultFullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, "/ABCBank/locator"); request.headers().set(TestConstant.HEADER_CONTENT_TYPE, TestConstant.CONTENT_TYPE_JSON); ByteBuf buffer = request.content().clear(); int p0 = buffer.writerIndex(); buffer.writeBytes(requestMessage.getBytes()); int p1 = buffer.writerIndex(); request.headers().set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(p1 - p0)); int send = http2Client.send(request); FullHttpResponse response = http2Client.getResponse(send); Assert.assertEquals(response.getStatus().code(), 200, "Response code mismatched"); Assert.assertEquals(response.headers().get(TestConstant.HEADER_CONTENT_TYPE.toLowerCase()), TestConstant.CONTENT_TYPE_JSON, "Content-Type mismatched"); Assert.assertEquals(getResponse(response), responseMessage, "Message content mismatched"); }
From source file:org.bgp4j.netty.protocol.BGPv4Packet.java
License:Apache License
/** * build a binary representation of the protocol packet * // w w w . j av a2s. com * @param buffer The buffer to store the binary packet representation into */ public void encodePacket(ByteBuf buffer) { for (int i = 0; i < BGPv4Constants.BGP_PACKET_MARKER_LENGTH; i++) buffer.writeByte(0xff); int headerHeaderIndex = buffer.writerIndex(); buffer.writeShort(BGPv4Constants.BGP_PACKET_HEADER_LENGTH); buffer.writeByte(getType()); encodePayload(buffer); buffer.setShort(headerHeaderIndex, BGPv4Constants.BGP_PACKET_HEADER_LENGTH + (buffer.writerIndex() - headerHeaderIndex) - 3); }