List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:org.apache.giraph.comm.netty.handler.ResponseEncoder.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("write(" + ctx + "," + msg); }//from ww w .ja va2 s . c o m if (!(msg instanceof WritableRequest)) { throw new IllegalArgumentException("encode: cannot encode message of type " + msg.getClass() + " since it is not an instance of an implementation of " + " WritableRequest."); } @SuppressWarnings("unchecked") WritableRequest writableRequest = (WritableRequest) msg; ByteBuf buf = ctx.alloc().buffer(10); ByteBufOutputStream output = new ByteBufOutputStream(buf); if (LOG.isDebugEnabled()) { LOG.debug("encode: Encoding a message of type " + msg.getClass()); } // Space is reserved now to be filled later by the serialize request size output.writeInt(0); // write type of object. output.writeByte(writableRequest.getType().ordinal()); // write the object itself. writableRequest.write(output); output.flush(); output.close(); // Set the correct size at the end. buf.setInt(0, buf.writerIndex() - SIZE_OF_INT); if (LOG.isDebugEnabled()) { LOG.debug("encode: Encoding a message of type " + msg.getClass()); } ctx.write(buf, promise); /*if[HADOOP_NON_SECURE] else[HADOOP_NON_SECURE]*/ if (writableRequest.getType() == RequestType.SASL_COMPLETE_REQUEST) { // We are sending to the client a SASL_COMPLETE response (created by // the SaslServer handler). The SaslServer handler has removed itself // from the pipeline after creating this response, and now it's time for // the ResponseEncoder to remove itself also. if (LOG.isDebugEnabled()) { LOG.debug("encode: Removing RequestEncoder handler: no longer needed," + " since client: " + ctx.channel().remoteAddress() + " has " + "completed authenticating."); } ctx.pipeline().remove(this); } /*end[HADOOP_NON_SECURE]*/ ctx.write(buf, promise); }
From source file:org.apache.hadoop.hbase.ipc.NettyRpcDuplexHandler.java
License:Apache License
private void writeRequest(ChannelHandlerContext ctx, Call call, ChannelPromise promise) throws IOException { id2Call.put(call.id, call);//w w w . j ava 2s . co m ByteBuf cellBlock = cellBlockBuilder.buildCellBlock(codec, compressor, call.cells, ctx.alloc()); CellBlockMeta cellBlockMeta; if (cellBlock != null) { CellBlockMeta.Builder cellBlockMetaBuilder = CellBlockMeta.newBuilder(); cellBlockMetaBuilder.setLength(cellBlock.writerIndex()); cellBlockMeta = cellBlockMetaBuilder.build(); } else { cellBlockMeta = null; } RequestHeader requestHeader = IPCUtil.buildRequestHeader(call, cellBlockMeta); int sizeWithoutCellBlock = IPCUtil.getTotalSizeWhenWrittenDelimited(requestHeader, call.param); int totalSize = cellBlock != null ? sizeWithoutCellBlock + cellBlock.writerIndex() : sizeWithoutCellBlock; ByteBuf buf = ctx.alloc().buffer(sizeWithoutCellBlock + 4); buf.writeInt(totalSize); ByteBufOutputStream bbos = new ByteBufOutputStream(buf); requestHeader.writeDelimitedTo(bbos); if (call.param != null) { call.param.writeDelimitedTo(bbos); } if (cellBlock != null) { ChannelPromise withoutCellBlockPromise = ctx.newPromise(); ctx.write(buf, withoutCellBlockPromise); ChannelPromise cellBlockPromise = ctx.newPromise(); ctx.write(cellBlock, cellBlockPromise); PromiseCombiner combiner = new PromiseCombiner(); combiner.addAll(withoutCellBlockPromise, cellBlockPromise); combiner.finish(promise); } else { ctx.write(buf, promise); } }
From source file:org.apache.jackrabbit.oak.plugins.segment.NetworkErrorProxy.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof ByteBuf) { ByteBuf bb = (ByteBuf) msg; this.transferredBytes += (bb.writerIndex() - bb.readerIndex()); }/*from ww w .j ava 2s . c om*/ remote.channel().write(msg); }
From source file:org.apache.jackrabbit.oak.plugins.segment.NetworkErrorProxy.java
License:Apache License
public int messageSize(Object msg) { if (msg instanceof ByteBuf) { ByteBuf bb = (ByteBuf) msg; return (bb.writerIndex() - bb.readerIndex()); }/* w w w .j a v a 2s. c o m*/ // unknown return 0; }
From source file:org.apache.pulsar.broker.service.PersistentDispatcherFailoverConsumerTest.java
License:Apache License
@BeforeMethod public void setup() throws Exception { ServiceConfiguration svcConfig = spy(new ServiceConfiguration()); PulsarService pulsar = spy(new PulsarService(svcConfig)); doReturn(svcConfig).when(pulsar).getConfiguration(); mlFactoryMock = mock(ManagedLedgerFactory.class); doReturn(mlFactoryMock).when(pulsar).getManagedLedgerFactory(); ZooKeeper mockZk = createMockZooKeeper(); doReturn(mockZk).when(pulsar).getZkClient(); doReturn(createMockBookKeeper(mockZk, pulsar.getOrderedExecutor().chooseThread(0))).when(pulsar) .getBookKeeperClient();//from w ww.j a va 2s . c o m configCacheService = mock(ConfigurationCacheService.class); @SuppressWarnings("unchecked") ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class); LocalZooKeeperCacheService zkCache = mock(LocalZooKeeperCacheService.class); doReturn(CompletableFuture.completedFuture(Optional.empty())).when(zkDataCache).getAsync(any()); doReturn(zkDataCache).when(zkCache).policiesCache(); doReturn(zkDataCache).when(configCacheService).policiesCache(); doReturn(configCacheService).when(pulsar).getConfigurationCache(); doReturn(zkCache).when(pulsar).getLocalZkCacheService(); brokerService = spy(new BrokerService(pulsar)); doReturn(brokerService).when(pulsar).getBrokerService(); consumerChanges = new LinkedBlockingQueue<>(); this.channelCtx = mock(ChannelHandlerContext.class); doAnswer(invocationOnMock -> { ByteBuf buf = invocationOnMock.getArgumentAt(0, ByteBuf.class); ByteBuf cmdBuf = buf.retainedSlice(4, buf.writerIndex() - 4); try { int cmdSize = (int) cmdBuf.readUnsignedInt(); int writerIndex = cmdBuf.writerIndex(); cmdBuf.writerIndex(cmdBuf.readerIndex() + cmdSize); ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(cmdBuf); BaseCommand.Builder cmdBuilder = BaseCommand.newBuilder(); BaseCommand cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build(); cmdBuilder.recycle(); cmdBuf.writerIndex(writerIndex); cmdInputStream.recycle(); if (cmd.hasActiveConsumerChange()) { consumerChanges.put(cmd.getActiveConsumerChange()); } cmd.recycle(); } finally { cmdBuf.release(); } return null; }).when(channelCtx).writeAndFlush(any(), any()); serverCnx = spy(new ServerCnx(pulsar)); doReturn(true).when(serverCnx).isActive(); doReturn(true).when(serverCnx).isWritable(); doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnx).clientAddress(); when(serverCnx.getRemoteEndpointProtocolVersion()).thenReturn(ProtocolVersion.v12.getNumber()); when(serverCnx.ctx()).thenReturn(channelCtx); serverCnxWithOldVersion = spy(new ServerCnx(pulsar)); doReturn(true).when(serverCnxWithOldVersion).isActive(); doReturn(true).when(serverCnxWithOldVersion).isWritable(); doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnxWithOldVersion).clientAddress(); when(serverCnxWithOldVersion.getRemoteEndpointProtocolVersion()) .thenReturn(ProtocolVersion.v11.getNumber()); when(serverCnxWithOldVersion.ctx()).thenReturn(channelCtx); NamespaceService nsSvc = mock(NamespaceService.class); doReturn(nsSvc).when(pulsar).getNamespaceService(); doReturn(true).when(nsSvc).isServiceUnitOwned(any(NamespaceBundle.class)); doReturn(true).when(nsSvc).isServiceUnitActive(any(TopicName.class)); setupMLAsyncCallbackMocks(); }
From source file:org.apache.pulsar.client.impl.RawMessageImpl.java
License:Apache License
static public RawMessage deserializeFrom(ByteBuf buffer) { try {/*from w w w . j a v a2 s .c o m*/ int idSize = buffer.readInt(); int writerIndex = buffer.writerIndex(); buffer.writerIndex(buffer.readerIndex() + idSize); ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer); MessageIdData.Builder builder = MessageIdData.newBuilder(); MessageIdData id = builder.mergeFrom(stream, null).build(); buffer.writerIndex(writerIndex); builder.recycle(); int payloadAndMetadataSize = buffer.readInt(); ByteBuf metadataAndPayload = buffer.slice(buffer.readerIndex(), payloadAndMetadataSize); return new RawMessageImpl(id, metadataAndPayload); } catch (IOException e) { // This is in-memory deserialization, should not fail log.error("IO exception deserializing ByteBuf (this shouldn't happen as operation is in-memory)", e); throw new RuntimeException(e); } }
From source file:org.apache.pulsar.common.api.PulsarDecoder.java
License:Apache License
@Override final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // Get a buffer that contains the full frame ByteBuf buffer = (ByteBuf) msg; BaseCommand cmd = null;/*w w w . j a v a 2 s. c om*/ BaseCommand.Builder cmdBuilder = null; try { // De-serialize the command int cmdSize = (int) buffer.readUnsignedInt(); int writerIndex = buffer.writerIndex(); buffer.writerIndex(buffer.readerIndex() + cmdSize); ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer); cmdBuilder = BaseCommand.newBuilder(); cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build(); buffer.writerIndex(writerIndex); cmdInputStream.recycle(); if (log.isDebugEnabled()) { log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType()); } messageReceived(); switch (cmd.getType()) { case PARTITIONED_METADATA: checkArgument(cmd.hasPartitionMetadata()); handlePartitionMetadataRequest(cmd.getPartitionMetadata()); cmd.getPartitionMetadata().recycle(); break; case PARTITIONED_METADATA_RESPONSE: checkArgument(cmd.hasPartitionMetadataResponse()); handlePartitionResponse(cmd.getPartitionMetadataResponse()); cmd.getPartitionMetadataResponse().recycle(); break; case LOOKUP: checkArgument(cmd.hasLookupTopic()); handleLookup(cmd.getLookupTopic()); cmd.getLookupTopic().recycle(); break; case LOOKUP_RESPONSE: checkArgument(cmd.hasLookupTopicResponse()); handleLookupResponse(cmd.getLookupTopicResponse()); cmd.getLookupTopicResponse().recycle(); break; case ACK: checkArgument(cmd.hasAck()); handleAck(cmd.getAck()); cmd.getAck().getMessageId().recycle(); cmd.getAck().recycle(); break; case CLOSE_CONSUMER: checkArgument(cmd.hasCloseConsumer()); handleCloseConsumer(cmd.getCloseConsumer()); cmd.getCloseConsumer().recycle(); break; case CLOSE_PRODUCER: checkArgument(cmd.hasCloseProducer()); handleCloseProducer(cmd.getCloseProducer()); cmd.getCloseProducer().recycle(); break; case CONNECT: checkArgument(cmd.hasConnect()); handleConnect(cmd.getConnect()); cmd.getConnect().recycle(); break; case CONNECTED: checkArgument(cmd.hasConnected()); handleConnected(cmd.getConnected()); cmd.getConnected().recycle(); break; case ERROR: checkArgument(cmd.hasError()); handleError(cmd.getError()); cmd.getError().recycle(); break; case FLOW: checkArgument(cmd.hasFlow()); handleFlow(cmd.getFlow()); cmd.getFlow().recycle(); break; case MESSAGE: { checkArgument(cmd.hasMessage()); handleMessage(cmd.getMessage(), buffer); cmd.getMessage().recycle(); break; } case PRODUCER: checkArgument(cmd.hasProducer()); handleProducer(cmd.getProducer()); cmd.getProducer().recycle(); break; case SEND: { checkArgument(cmd.hasSend()); // Store a buffer marking the content + headers ByteBuf headersAndPayload = buffer.markReaderIndex(); handleSend(cmd.getSend(), headersAndPayload); cmd.getSend().recycle(); break; } case SEND_ERROR: checkArgument(cmd.hasSendError()); handleSendError(cmd.getSendError()); cmd.getSendError().recycle(); break; case SEND_RECEIPT: checkArgument(cmd.hasSendReceipt()); handleSendReceipt(cmd.getSendReceipt()); cmd.getSendReceipt().recycle(); break; case SUBSCRIBE: checkArgument(cmd.hasSubscribe()); handleSubscribe(cmd.getSubscribe()); cmd.getSubscribe().recycle(); break; case SUCCESS: checkArgument(cmd.hasSuccess()); handleSuccess(cmd.getSuccess()); cmd.getSuccess().recycle(); break; case PRODUCER_SUCCESS: checkArgument(cmd.hasProducerSuccess()); handleProducerSuccess(cmd.getProducerSuccess()); cmd.getProducerSuccess().recycle(); break; case UNSUBSCRIBE: checkArgument(cmd.hasUnsubscribe()); handleUnsubscribe(cmd.getUnsubscribe()); cmd.getUnsubscribe().recycle(); break; case PING: checkArgument(cmd.hasPing()); handlePing(cmd.getPing()); cmd.getPing().recycle(); break; case PONG: checkArgument(cmd.hasPong()); handlePong(cmd.getPong()); cmd.getPong().recycle(); break; case REDELIVER_UNACKNOWLEDGED_MESSAGES: checkArgument(cmd.hasRedeliverUnacknowledgedMessages()); handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages()); cmd.getRedeliverUnacknowledgedMessages().recycle(); break; case CONSUMER_STATS: checkArgument(cmd.hasConsumerStats()); handleConsumerStats(cmd.getConsumerStats()); cmd.getConsumerStats().recycle(); break; case CONSUMER_STATS_RESPONSE: checkArgument(cmd.hasConsumerStatsResponse()); handleConsumerStatsResponse(cmd.getConsumerStatsResponse()); cmd.getConsumerStatsResponse().recycle(); break; case REACHED_END_OF_TOPIC: checkArgument(cmd.hasReachedEndOfTopic()); handleReachedEndOfTopic(cmd.getReachedEndOfTopic()); cmd.getReachedEndOfTopic().recycle(); break; } } finally { if (cmdBuilder != null) { cmdBuilder.recycle(); } if (cmd != null) { cmd.recycle(); } buffer.release(); } }
From source file:org.apache.pulsar.common.api.PulsarDecoderTest.java
License:Apache License
@Test public void testChannelRead() throws Exception { long consumerId = 1234L; ByteBuf changeBuf = Commands.newActiveConsumerChange(consumerId, true); ByteBuf cmdBuf = changeBuf.slice(4, changeBuf.writerIndex() - 4); doNothing().when(decoder).handleActiveConsumerChange(any(CommandActiveConsumerChange.class)); decoder.channelRead(mock(ChannelHandlerContext.class), cmdBuf); verify(decoder, times(1)).handleActiveConsumerChange(any(CommandActiveConsumerChange.class)); }
From source file:org.apache.tajo.util.BytesUtils.java
License:Apache License
/** * this is an implementation copied from ByteBufUtil in netty4 */// w w w. j a va 2 s . c om public static int writeUtf8(ByteBuf buffer, char[] chars, boolean ignoreSurrogate) { int oldWriterIndex = buffer.writerIndex(); int writerIndex = oldWriterIndex; // We can use the _set methods as these not need to do any index checks and reference checks. // This is possible as we called ensureWritable(...) before. for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (c < 0x80) { buffer.setByte(writerIndex++, (byte) c); } else if (c < 0x800) { buffer.setByte(writerIndex++, (byte) (0xc0 | (c >> 6))); buffer.setByte(writerIndex++, (byte) (0x80 | (c & 0x3f))); } else if (!ignoreSurrogate && isSurrogate(c)) { if (!Character.isHighSurrogate(c)) { throw new IllegalArgumentException("Invalid encoding. " + "Expected high (leading) surrogate at index " + i + " but got " + c); } final char c2; try { // Surrogate Pair consumes 2 characters. Optimistically try to get the next character to avoid // duplicate bounds checking with charAt. If an IndexOutOfBoundsException is thrown we will // re-throw a more informative exception describing the problem. c2 = chars[++i]; } catch (IndexOutOfBoundsException e) { throw new IllegalArgumentException("Underflow. " + "Expected low (trailing) surrogate at index " + i + " but no more characters found.", e); } if (!Character.isLowSurrogate(c2)) { throw new IllegalArgumentException("Invalid encoding. " + "Expected low (trailing) surrogate at index " + i + " but got " + c2); } int codePoint = Character.toCodePoint(c, c2); // See http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G2630. buffer.setByte(writerIndex++, (byte) (0xf0 | (codePoint >> 18))); buffer.setByte(writerIndex++, (byte) (0x80 | ((codePoint >> 12) & 0x3f))); buffer.setByte(writerIndex++, (byte) (0x80 | ((codePoint >> 6) & 0x3f))); buffer.setByte(writerIndex++, (byte) (0x80 | (codePoint & 0x3f))); } else { buffer.setByte(writerIndex++, (byte) (0xe0 | (c >> 12))); buffer.setByte(writerIndex++, (byte) (0x80 | ((c >> 6) & 0x3f))); buffer.setByte(writerIndex++, (byte) (0x80 | (c & 0x3f))); } } // update the writerIndex without any extra checks for performance reasons buffer.writerIndex(writerIndex); return writerIndex - oldWriterIndex; }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte array argument as if it was a double value and returns the * result. Throws NumberFormatException if the byte buffer does not represent a * double value.//from w ww . j av a 2 s. c o m * * @return double, the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as a double */ public static double parseDouble(ByteBuf bytes, int start, int length) { if (!PlatformDependent.hasUnsafe()) { return parseDouble(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); /* * Strip off leading blanks */ int offset = start; int end = start + length; while (offset < end && PlatformDependent.getByte(memoryAddress + offset) == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } /* * check for a sign. */ boolean sign = false; if (PlatformDependent.getByte(memoryAddress + offset) == '-') { sign = true; offset++; } else if (PlatformDependent.getByte(memoryAddress + offset) == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } /* * Count the number of digits in the mantissa (including the decimal * point), and also locate the decimal point. */ int mantSize = 0; /* Number of digits in mantissa. */ int decicalOffset = -1; /* Number of mantissa digits BEFORE decimal point. */ for (; offset < end; offset++) { if (!isDigit(PlatformDependent.getByte(memoryAddress + offset))) { if ((PlatformDependent.getByte(memoryAddress + offset) != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; /* Temporarily holds location of exponent in bytes. */ /* * Now suck up the digits in the mantissa. Use two integers to * collect 9 digits each (this is faster than using floating-point). * If the mantissa has more than 18 digits, ignore the extras, since * they can't affect the value anyway. */ offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; /* One of the digits was the decimal point. */ } int fracExponent; /* Exponent that derives from the fractional * part. Under normal circumstatnces, it is * the negative of the number of digits in F. * However, if I is very long, the last digits * of I get dropped (otherwise a long I with a * large negative exponent could cause an * unnecessary overflow on I alone). In this * case, fracExp is incremented one for each * dropped digit. */ if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = PlatformDependent.getByte(memoryAddress + offset); offset++; if (b == '.') { b = PlatformDependent.getByte(memoryAddress + offset); offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = PlatformDependent.getByte(memoryAddress + offset); offset++; if (b == '.') { b = PlatformDependent.getByte(memoryAddress + offset); offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; /* * Skim off the exponent. */ int exponent = 0; /* Exponent read from "EX" field. */ offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((PlatformDependent.getByte(memoryAddress + offset) != 'E') && (PlatformDependent.getByte(memoryAddress + offset) != 'e')) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } // (bytes[offset] == 'E') || (bytes[offset] == 'e') offset++; if (PlatformDependent.getByte(memoryAddress + offset) == '-') { expSign = true; offset++; } else if (PlatformDependent.getByte(memoryAddress + offset) == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(PlatformDependent.getByte(memoryAddress + offset))) { exponent = exponent * 10 + (PlatformDependent.getByte(memoryAddress + offset) - '0'); } else { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); /* * Generate a floating-point number that represents the exponent. * Do this by processing the exponent one bit at a time to combine * many powers of 2 of 10. Then combine the exponent with the * fraction. */ if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; }