List of usage examples for io.netty.buffer ByteBuf slice
public abstract ByteBuf slice(int index, int length);
From source file:com.tesora.dve.db.mysql.portal.protocol.MSPComPrepareStmtRequestMessage.java
License:Open Source License
private ByteBuf getRemainingBuf() { ByteBuf readBuf = readBuffer(); ByteBuf remainingBuf = readBuf.slice(1, readBuf.readableBytes() - 1);//skip over type field. return remainingBuf; }
From source file:com.tesora.dve.db.mysql.portal.protocol.MSPComStmtExecuteRequestMessage.java
License:Open Source License
public void readParameterMetadata(MyPreparedStatement<String> pStmt) throws PEException { //TODO: this belongs in unmarshall, but requires knowledge about the expected number of parameters, provided by a previous prepare response. ParsedData cachedParse = readState(); ByteBuf backingData = readBuffer(); int lengthOfMetadata = backingData.readableBytes() - cachedParse.metadataOffset; ByteBuf in = backingData.slice(cachedParse.metadataOffset, lengthOfMetadata).order(ByteOrder.LITTLE_ENDIAN); if (pStmt.getNumParams() > 0) { int nullBitmapLength = (pStmt.getNumParams() + 7) / 8; MyNullBitmap nullBitmap = new MyNullBitmap(MysqlAPIUtils.readBytes(in, nullBitmapLength), pStmt.getNumParams(), MyNullBitmap.BitmapType.EXECUTE_REQUEST); if (in.readByte() == 1) { // this is new params bound flag; only // =1 on first stmt_execute pStmt.clearParameters();// w ww .j av a 2 s. co m for (int i = 0; i < pStmt.getNumParams(); i++) { pStmt.addParameter(new MyParameter(MyFieldType.fromByte(in.readByte()))); in.skipBytes(1); } } for (int paramNum = 1; paramNum <= pStmt.getNumParams(); paramNum++) { MyParameter parameter = pStmt.getParameter(paramNum); if (nullBitmap.getBit(paramNum)) { parameter.setValue(null); } else { //TODO - may need to figure out how to get the proper flags and length into this call parameter.setValue(DBTypeBasedUtils.getMysqlTypeFunc(parameter.getType()).readObject(in)); } } } }
From source file:com.tesora.dve.db.mysql.portal.protocol.MysqlClientAuthenticationHandler.java
License:Open Source License
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out, boolean isLastBytes) throws Exception { ByteBuf leBuf = in.order(ByteOrder.LITTLE_ENDIAN); leBuf.markReaderIndex();/*from ww w . java2 s .c om*/ boolean messageProcessed = false; try { if (leBuf.readableBytes() > MESSAGE_HEADER_LEN) { int payloadLen = leBuf.readMedium(); leBuf.readByte(); // seq if (leBuf.readableBytes() >= payloadLen) { ByteBuf payload = leBuf.slice(leBuf.readerIndex(), payloadLen).order(ByteOrder.LITTLE_ENDIAN); Byte protocolVersion = leBuf.readByte(); leBuf.skipBytes(payloadLen - 1); messageProcessed = true; if (state == AuthenticationState.AWAIT_GREETING) processGreeting(ctx, payload, protocolVersion); else processAcknowlegement(ctx, payload); } } } catch (Throwable t) { enterState(AuthenticationState.FAILURE); log.warn("Unexpected problem on outbound mysql connection.", t); throw t; } finally { if (!messageProcessed) leBuf.resetReaderIndex(); if (isLastBytes && (state == AuthenticationState.AWAIT_ACKNOWLEGEMENT || state == AuthenticationState.AWAIT_GREETING)) { //we are waiting for handshake packets from mysql, but no more packets will ever arrive. release blocked callers. Channel channel = ctx.channel(); SocketAddress addr = (channel == null ? null : channel.remoteAddress()); log.warn("Socket closed in middle of authentication handshake on socket " + addr); enterState(AuthenticationState.FAILURE); } } }
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationOverlap() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4) + (4 * 40); //four full packets and a little change, divisible by 4. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4; Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:com.tesora.dve.sql.LargeMaxPktTest.java
License:Open Source License
@Test public void testComQueryMessageContinuationExact() throws Exception { int defaultMaxPacket = 0xFFFFFF; int payloadSize = (defaultMaxPacket * 4); //four full packets exactly, requires an empty trailing packet. ByteBuf source = Unpooled.buffer(payloadSize, payloadSize); Random rand = new Random(239873L); while (source.isWritable()) source.writeInt(rand.nextInt()); Assert.assertEquals(source.writableBytes(), 0, "Oops, I intended to fill up the source buffer"); ByteBuf dest = Unpooled.buffer(payloadSize); MSPComQueryRequestMessage outboundMessage = MSPComQueryRequestMessage.newMessage(source.array()); Packet.encodeFullMessage((byte) 0, outboundMessage, dest); int lengthOfNonUserdata = 5 + 4 + 4 + 4 + 4;//last packet has zero length payload Assert.assertEquals(dest.readableBytes(), payloadSize + lengthOfNonUserdata, "Number of bytes in destination buffer is wrong"); Assert.assertEquals(dest.getUnsignedMedium(0), defaultMaxPacket, "First length should be maximum"); Assert.assertEquals(dest.getByte(3), (byte) 0, "First sequenceID should be zero"); Assert.assertEquals(dest.getByte(4), (byte) MSPComQueryRequestMessage.TYPE_IDENTIFIER, "First byte of payload should be MSPComQueryRequestMessage.TYPE_IDENTIFIER"); ByteBuf sliceFirstPayload = dest.slice(5, (0xFFFFFF - 1)); Assert.assertEquals(sliceFirstPayload, source.slice(0, 0xFFFFFF - 1)); }
From source file:com.twitter.http2.HttpFrameDecoderTest.java
License:Apache License
@Test public void testHttpDataFrame() throws Exception { int length = RANDOM.nextInt() & 0x3FFF; byte flags = 0; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf frame = dataFrame(length, flags, streamId); writeRandomData(frame, length);/*from w w w . ja v a2 s. c om*/ decoder.decode(frame); InOrder inOrder = inOrder(delegate); for (int i = 0; i < length; i += 8192) { // data frames do not exceed maxChunkSize int off = HTTP_FRAME_HEADER_SIZE + i; int len = Math.min(length - i, 8192); inOrder.verify(delegate).readDataFrame(streamId, false, false, frame.slice(off, len)); } verifyNoMoreInteractions(delegate); }
From source file:com.twitter.http2.HttpFrameDecoderTest.java
License:Apache License
@Test public void testPaddedHttpDataFrame() throws Exception { int length = RANDOM.nextInt() & 0x3FFF | 0x01; byte flags = 0x08; // PADDED int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int padding = Math.min(RANDOM.nextInt() & 0xFF, length - 1); ByteBuf frame = dataFrame(length, flags, streamId); frame.writeByte(padding);//from w ww . jav a 2s . c o m writeRandomData(frame, length - 1); decoder.decode(frame); InOrder inOrder = inOrder(delegate); inOrder.verify(delegate).readDataFramePadding(streamId, false, padding + 1); int dataLength = length - 1 - padding; if (dataLength == 0) { inOrder.verify(delegate).readDataFrame(streamId, false, false, Unpooled.EMPTY_BUFFER); } else { for (int i = 0; i < dataLength; i += 8192) { // data frames do not exceed maxChunkSize int off = HTTP_FRAME_HEADER_SIZE + 1 + i; int len = Math.min(dataLength - i, 8192); inOrder.verify(delegate).readDataFrame(streamId, false, false, frame.slice(off, len)); } } verifyNoMoreInteractions(delegate); }
From source file:com.twitter.http2.HttpFrameDecoderTest.java
License:Apache License
@Test public void testHttpHeadersFrame() throws Exception { int headerBlockLength = 16; int length = 5 + headerBlockLength; byte flags = 0x04 | 0x20; // END_HEADERS | PRIORITY int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; boolean exclusive = RANDOM.nextBoolean(); int dependency = RANDOM.nextInt() & 0x7FFFFFFF; int weight = RANDOM.nextInt() & 0xFF; ByteBuf frame = headersFrame(length, flags, streamId); writePriorityFields(frame, exclusive, dependency, weight); writeRandomData(frame, headerBlockLength); decoder.decode(frame);//from w w w. j ava 2 s . c o m InOrder inOrder = inOrder(delegate); inOrder.verify(delegate).readHeadersFrame(streamId, false, false, exclusive, dependency, weight + 1); inOrder.verify(delegate).readHeaderBlock(frame.slice(HTTP_FRAME_HEADER_SIZE + 5, headerBlockLength)); inOrder.verify(delegate).readHeaderBlockEnd(); verifyNoMoreInteractions(delegate); }
From source file:com.twitter.http2.HttpFrameDecoderTest.java
License:Apache License
@Test public void testContinuedHttpHeadersFrame() throws Exception { int headerBlockLength = 16; int length = 5; byte flags = 0x20; // PRIORITY int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; boolean exclusive = RANDOM.nextBoolean(); int dependency = RANDOM.nextInt() & 0x7FFFFFFF; int weight = RANDOM.nextInt() & 0xFF; ByteBuf headersFrame = headersFrame(length, flags, streamId); writePriorityFields(headersFrame, exclusive, dependency, weight); ByteBuf continuationFrame = continuationFrame(headerBlockLength, (byte) 0x04, streamId); // END_HEADERS writeRandomData(continuationFrame, headerBlockLength); decoder.decode(releaseLater(Unpooled.wrappedBuffer(headersFrame, continuationFrame))); InOrder inOrder = inOrder(delegate); inOrder.verify(delegate).readHeadersFrame(streamId, false, false, exclusive, dependency, weight + 1); inOrder.verify(delegate)/* w w w.ja v a 2 s . c om*/ .readHeaderBlock(continuationFrame.slice(HTTP_FRAME_HEADER_SIZE, headerBlockLength)); inOrder.verify(delegate).readHeaderBlockEnd(); verifyNoMoreInteractions(delegate); }
From source file:com.twitter.http2.HttpFrameDecoderTest.java
License:Apache License
@Test public void testHttpHeadersFrameEmptyContinuation() throws Exception { int length = 16; byte flags = 0; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf headersFrame = headersFrame(length, flags, streamId); writeRandomData(headersFrame, length); ByteBuf continuationFrame = continuationFrame(0, (byte) 0x04, streamId); // END_HEADERS decoder.decode(releaseLater(Unpooled.wrappedBuffer(headersFrame, continuationFrame))); InOrder inOrder = inOrder(delegate); inOrder.verify(delegate).readHeadersFrame(streamId, false, false, false, HTTP_DEFAULT_DEPENDENCY, HTTP_DEFAULT_WEIGHT);/*from www . ja va 2s . co m*/ inOrder.verify(delegate).readHeaderBlock(headersFrame.slice(HTTP_FRAME_HEADER_SIZE, length)); inOrder.verify(delegate).readHeaderBlockEnd(); verifyNoMoreInteractions(delegate); }