List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:com.allanbank.mongodb.netty.NettyOutputBufferTest.java
License:Apache License
/** * Test method for {@link NettyOutputBuffer#close()}. *///from w ww . j a v a 2 s .c o m @Test public void testClose() { final ByteBuf mockBuffer = createMock(ByteBuf.class); expect(mockBuffer.writerIndex()).andReturn(0); expect(mockBuffer.release()).andReturn(true); replay(mockBuffer); final NettyOutputBuffer outBuffer = new NettyOutputBuffer(mockBuffer, new StringEncoderCache()); assertThat(outBuffer.getBuffer(), sameInstance(mockBuffer)); outBuffer.close(); verify(mockBuffer); }
From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java
License:Open Source License
/** * Add header to finish the full packet// w w w . ja v a 2 s. c om * @param out * @param packetSeq * @param writeBodyFunc write packet body function */ public static void writePacket(ByteBuf out, byte packetSeq, Callback writeBodyFunc) { int start = out.writerIndex(); out.writeZero(4); writeBodyFunc.callback(); int end = out.writerIndex(); out.writeByte(0); out.writerIndex(start); int length = end - start - MySQLPacket.packetHeaderSize; BufferUtils.writeLongInt(out, length); out.writeByte(packetSeq); out.writerIndex(end); if (_log.isTraceEnabled()) { int readerIndex = out.readerIndex(); out.readerIndex(start); byte[] bytes = new byte[end - start]; out.readBytes(bytes); out.readerIndex(readerIndex); String dump = '\n' + UberUtil.hexDump(bytes); _log.trace(dump); } }
From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java
License:Open Source License
/** * //from w w w.java 2s. co m * From server to client. One packet for each row in the result set. * * <pre> * Bytes Name * ----- ---- * n (Length Coded String) (column value) * ... * * (column value): The data in the column, as a character string. * If a column is defined as non-character, the * server converts the value into a character * before sending it. Since the value is a Length * Coded String, a NULL can be represented with a * single byte containing 251(see the description * of Length Coded Strings in section "Elements" above). * * @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Row_Data_Packet * </pre> * * @param buffer * @param fieldValues */ public void writeRowBinaryBody(ByteBuf buffer, long pRecord, CursorMeta meta, int nColumns) { if ((pRecord != 0) && (nColumns > 0)) { // start of package buffer.writeByte(0); int nullByteCnt = (nColumns + 7 + 2) / 8; byte[] nullBitmap = new byte[nullByteCnt]; int nullPos = buffer.writerIndex(); buffer.writeBytes(nullBitmap); for (int i = 0; i < nColumns; i++) { long pValue = Record.getValueAddress(pRecord, i); if (pValue != 0) { writeValue(buffer, meta.getColumn(i), pValue); } else { nullBitmap[(i + 2) / 8] |= 1 << (i + 2) % 8; } } int endPos = buffer.writerIndex(); buffer.writerIndex(nullPos); buffer.writeBytes(nullBitmap); buffer.writerIndex(endPos); } }
From source file:com.cloudera.livy.client.local.rpc.TestKryoMessageCodec.java
License:Apache License
@Test public void testFragmentation() throws Exception { ByteBuf buf = newBuffer(); Object[] messages = { "msg1", "msg2" }; int[] indices = new int[messages.length]; KryoMessageCodec codec = new KryoMessageCodec(0); for (int i = 0; i < messages.length; i++) { codec.encode(null, messages[i], buf); indices[i] = buf.writerIndex(); }//from w ww . jav a 2s. c o m List<Object> objects = Lists.newArrayList(); // Don't read enough data for the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] - 1), objects); assertEquals(0, objects.size()); // Read enough data for just the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] + 1), objects); assertEquals(1, objects.size()); }
From source file:com.cloudera.livy.rsc.rpc.TestKryoMessageCodec.java
License:Apache License
@Test public void testFragmentation() throws Exception { ByteBuf buf = newBuffer(); Object[] messages = { "msg1", "msg2" }; int[] indices = new int[messages.length]; KryoMessageCodec codec = new KryoMessageCodec(0); for (int i = 0; i < messages.length; i++) { codec.encode(null, messages[i], buf); indices[i] = buf.writerIndex(); }//from w w w. jav a 2s. c o m List<Object> objects = new ArrayList<>(); // Don't read enough data for the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] - 1), objects); assertEquals(0, objects.size()); // Read enough data for just the first message to be decoded. codec.decode(null, buf.slice(0, indices[0] + 1), objects); assertEquals(1, objects.size()); }
From source file:com.corundumstudio.socketio.parser.Encoder.java
License:Apache License
public int encodePacket(Packet packet, ByteBuf buffer) throws IOException { ByteBufOutputStream out = new ByteBufOutputStream(buffer); int start = buffer.writerIndex(); int type = packet.getType().getValue(); buffer.writeByte(toChar(type));/* ww w . ja v a 2 s. co m*/ buffer.writeByte(Packet.SEPARATOR); Long id = packet.getId(); String endpoint = packet.getEndpoint(); Object ack = packet.getAck(); if (Packet.ACK_DATA.equals(ack)) { buffer.writeBytes(toChars(id)); buffer.writeByte('+'); } else { if (id != null) { buffer.writeBytes(toChars(id)); } } buffer.writeByte(Packet.SEPARATOR); if (endpoint != null) { buffer.writeBytes(endpoint.getBytes()); } switch (packet.getType()) { case MESSAGE: if (packet.getData() != null) { buffer.writeByte(Packet.SEPARATOR); byte[] data = packet.getData().toString().getBytes(); buffer.writeBytes(data); } break; case EVENT: List<Object> args = packet.getArgs(); if (args.isEmpty()) { args = null; } buffer.writeByte(Packet.SEPARATOR); Event event = new Event(packet.getName(), args); jsonSupport.writeValue(out, event); break; case JSON: buffer.writeByte(Packet.SEPARATOR); jsonSupport.writeValue(out, packet.getData()); break; case CONNECT: if (packet.getQs() != null) { buffer.writeByte(Packet.SEPARATOR); byte[] qsData = packet.getQs().toString().getBytes(); buffer.writeBytes(qsData); } break; case ACK: if (packet.getAckId() != null || !packet.getArgs().isEmpty()) { buffer.writeByte(Packet.SEPARATOR); } if (packet.getAckId() != null) { byte[] ackIdData = toChars(packet.getAckId()); buffer.writeBytes(ackIdData); } if (!packet.getArgs().isEmpty()) { buffer.writeByte('+'); jsonSupport.writeValue(out, packet.getArgs()); } break; case ERROR: if (packet.getReason() != null || packet.getAdvice() != null) { buffer.writeByte(Packet.SEPARATOR); } if (packet.getReason() != null) { int reasonCode = packet.getReason().getValue(); buffer.writeByte(toChar(reasonCode)); } if (packet.getAdvice() != null) { int adviceCode = packet.getAdvice().getValue(); buffer.writeByte('+'); buffer.writeByte(toChar(adviceCode)); } break; } return charsScanner.getLength(buffer, start); }
From source file:com.corundumstudio.socketio.parser.UTF8CharsScanner.java
License:Apache License
public int getLength(ByteBuf inputBuffer, int start) { int len = 0;//ww w . j av a2s . co m for (int i = start; i < inputBuffer.writerIndex();) { i = getCharTailIndex(inputBuffer, i); len++; } return len; }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public void encodeJsonP(Integer jsonpIndex, Queue<Packet> packets, ByteBuf out, ByteBufAllocator allocator, int limit) throws IOException { boolean jsonpMode = jsonpIndex != null; ByteBuf buf = allocateBuffer(allocator); int i = 0;/* w ww . j a v a 2 s .c o m*/ while (true) { Packet packet = packets.poll(); if (packet == null || i == limit) { break; } ByteBuf packetBuf = allocateBuffer(allocator); encodePacket(packet, packetBuf, allocator, true); int packetSize = packetBuf.writerIndex(); buf.writeBytes(toChars(packetSize)); buf.writeBytes(B64_DELIMITER); buf.writeBytes(packetBuf); packetBuf.release(); i++; for (ByteBuf attachment : packet.getAttachments()) { ByteBuf encodedBuf = Base64.encode(attachment, Base64Dialect.URL_SAFE); buf.writeBytes(toChars(encodedBuf.readableBytes() + 2)); buf.writeBytes(B64_DELIMITER); buf.writeBytes(BINARY_HEADER); buf.writeBytes(encodedBuf); } } if (jsonpMode) { out.writeBytes(JSONP_HEAD); out.writeBytes(toChars(jsonpIndex)); out.writeBytes(JSONP_START); } processUtf8(buf, out, jsonpMode); buf.release(); if (jsonpMode) { out.writeBytes(JSONP_END); } }
From source file:com.corundumstudio.socketio.protocol.PacketEncoder.java
License:Apache License
public void encodePacket(Packet packet, ByteBuf buffer, ByteBufAllocator allocator, boolean binary) throws IOException { ByteBuf buf = buffer; if (!binary) { buf = allocateBuffer(allocator); }/* w ww . j a v a2 s . c o m*/ byte type = toChar(packet.getType().getValue()); buf.writeByte(type); try { switch (packet.getType()) { case PONG: { buf.writeBytes(packet.getData().toString().getBytes(CharsetUtil.UTF_8)); break; } case OPEN: { ByteBufOutputStream out = new ByteBufOutputStream(buf); jsonSupport.writeValue(out, packet.getData()); break; } case MESSAGE: { ByteBuf encBuf = null; if (packet.getSubType() == PacketType.ERROR) { encBuf = allocateBuffer(allocator); ByteBufOutputStream out = new ByteBufOutputStream(encBuf); jsonSupport.writeValue(out, packet.getData()); } if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.ACK) { List<Object> values = new ArrayList<Object>(); if (packet.getSubType() == PacketType.EVENT) { values.add(packet.getName()); } encBuf = allocateBuffer(allocator); List<Object> args = packet.getData(); values.addAll(args); ByteBufOutputStream out = new ByteBufOutputStream(encBuf); jsonSupport.writeValue(out, values); if (!jsonSupport.getArrays().isEmpty()) { packet.initAttachments(jsonSupport.getArrays().size()); for (byte[] array : jsonSupport.getArrays()) { packet.addAttachment(Unpooled.wrappedBuffer(array)); } packet.setSubType(PacketType.BINARY_EVENT); } } byte subType = toChar(packet.getSubType().getValue()); buf.writeByte(subType); if (packet.hasAttachments()) { byte[] ackId = toChars(packet.getAttachments().size()); buf.writeBytes(ackId); buf.writeByte('-'); } if (packet.getSubType() == PacketType.CONNECT) { if (!packet.getNsp().isEmpty()) { buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8)); } } else { if (!packet.getNsp().isEmpty()) { buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8)); buf.writeByte(','); } } if (packet.getAckId() != null) { byte[] ackId = toChars(packet.getAckId()); buf.writeBytes(ackId); } if (encBuf != null) { buf.writeBytes(encBuf); encBuf.release(); } break; } } } finally { // we need to write a buffer in any case if (!binary) { buffer.writeByte(0); int length = buf.writerIndex(); buffer.writeBytes(longToBytes(length)); buffer.writeByte(0xff); buffer.writeBytes(buf); buf.release(); } } }
From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelper.java
License:Apache License
/** * Finds the position of the correct closing character, taking into account the fact that before the correct one, * other sub section with same opening and closing characters can be encountered. * * This implementation starts for the current {@link ByteBuf#readerIndex() readerIndex} + startOffset. * * @param buf the {@link ByteBuf} where to search for the end of a section enclosed in openingChar and closingChar. * @param startOffset the offset at which to start reading (from buffer's readerIndex). * @param openingChar the section opening char, used to detect a sub-section. * @param closingChar the section closing char, used to detect the end of a sub-section / this section. * @return the section closing position or -1 if not found. *///from ww w . j a v a 2s. c o m public static int findSectionClosingPosition(ByteBuf buf, int startOffset, char openingChar, char closingChar) { int from = buf.readerIndex() + startOffset; int length = buf.writerIndex() - from; if (length < 0) { throw new IllegalArgumentException( "startOffset must not go beyond the readable byte length of the buffer"); } return buf.forEachByte(from, length, new ClosingPositionBufProcessor(openingChar, closingChar, true)); }