List of usage examples for io.netty.buffer ByteBuf array
public abstract byte[] array();
From source file:org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsGenerator.java
License:Apache License
public static void generate(PulsarService pulsar, OutputStream out) throws IOException { ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer(); try {//from www .j a v a2 s . c o m SimpleTextOutputStream stream = new SimpleTextOutputStream(buf); generateSystemMetrics(stream, pulsar.getConfiguration().getClusterName()); NamespaceStatsAggregator.generate(pulsar, stream); out.write(buf.array(), buf.arrayOffset(), buf.readableBytes()); } finally { buf.release(); } }
From source file:org.apache.pulsar.functions.worker.rest.api.FunctionsMetricsResource.java
License:Apache License
@Path("metrics") @GET/*from w w w . jav a 2 s. co m*/ @Produces(MediaType.TEXT_PLAIN) public Response getMetrics() { WorkerService workerService = get(); ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer(); try { SimpleTextOutputStream stream = new SimpleTextOutputStream(buf); FunctionsStatsGenerator.generate(workerService, "default", stream); byte[] payload = buf.array(); int arrayOffset = buf.arrayOffset(); int readableBytes = buf.readableBytes(); StreamingOutput streamOut = out -> { out.write(payload, arrayOffset, readableBytes); out.flush(); }; return Response.ok(streamOut).type(MediaType.TEXT_PLAIN_TYPE).build(); } finally { buf.release(); } }
From source file:org.apache.qpid.jms.provider.amqp.AmqpFixedProducer.java
License:Apache License
private void doSend(JmsOutboundMessageDispatch envelope, AsyncResult request) throws IOException, JMSException { // If the transaction has failed due to remote termination etc then we just indicate // the send has succeeded until the a new transaction is started. if (session.isTransacted() && session.isTransactionFailed()) { request.onSuccess();// w ww .ja v a 2 s. c o m return; } LOG.trace("Producer sending message: {}", envelope); boolean presettle = envelope.isPresettle() || isPresettle(); Delivery delivery = null; if (presettle) { delivery = getEndpoint().delivery(EMPTY_BYTE_ARRAY, 0, 0); } else { byte[] tag = tagGenerator.getNextTag(); delivery = getEndpoint().delivery(tag, 0, tag.length); } if (session.isTransacted()) { AmqpTransactionContext context = session.getTransactionContext(); delivery.disposition(context.getTxnEnrolledState()); context.registerTxProducer(this); } // Write the already encoded AMQP message into the Sender ByteBuf encoded = (ByteBuf) envelope.getPayload(); getEndpoint().send(encoded.array(), encoded.arrayOffset() + encoded.readerIndex(), encoded.readableBytes()); AmqpProvider provider = getParent().getProvider(); InFlightSend send = null; if (request instanceof InFlightSend) { send = (InFlightSend) request; } else { send = new InFlightSend(envelope, request); if (!presettle && getSendTimeout() != JmsConnectionInfo.INFINITE) { send.requestTimeout = getParent().getProvider().scheduleRequestTimeout(send, getSendTimeout(), send); } } if (presettle) { delivery.settle(); } else { sent.put(envelope.getMessageId(), send); getEndpoint().advance(); } send.setDelivery(delivery); delivery.setContext(send); // Put it on the wire and let it fail if the connection is broken, if it does // get written then continue on to determine when we should complete it. if (provider.pumpToProtonTransport(request)) { // For presettled messages we can just mark as successful and we are done, but // for any other message we still track it until the remote settles. If the send // was tagged as asynchronous we must mark the original request as complete but // we still need to wait for the disposition before we can consider the send as // having been successful. if (presettle) { send.onSuccess(); } else if (envelope.isSendAsync()) { send.getOriginalRequest().onSuccess(); } } }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpJmsBytesMessageFacade.java
License:Apache License
@Override public void reset() { if (bytesOut != null) { ByteBuf writeBuf = bytesOut.buffer(); Binary body = new Binary(writeBuf.array(), writeBuf.arrayOffset(), writeBuf.readableBytes()); setBody(new Data(body)); try {//from w w w .j av a 2 s . co m bytesOut.close(); } catch (IOException e) { } bytesOut = null; } else if (bytesIn != null) { try { bytesIn.close(); } catch (IOException e) { } bytesIn = null; } }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpMessageSupport.java
License:Apache License
/** * Given a byte buffer that represents an encoded AMQP Message instance, * decode and return the Message./*www . ja v a2 s.c om*/ * * @param encodedBytes * the bytes that represent an encoded AMQP Message. * * @return a new Message instance with the decoded data. */ public static Message decodeMessage(ByteBuf encodedBytes) { // For now we must fully decode the message to get at the annotations. Message protonMessage = Message.Factory.create(); protonMessage.decode(encodedBytes.array(), 0, encodedBytes.readableBytes()); return protonMessage; }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpReadableBufferTest.java
License:Apache License
@Test public void testArrayAccess() { ByteBuf byteBuffer = Unpooled.buffer(100, 100); AmqpReadableBuffer buffer = new AmqpReadableBuffer(byteBuffer); assertTrue(buffer.hasArray());/* w w w . j av a2s.c o m*/ assertSame(buffer.array(), byteBuffer.array()); assertEquals(buffer.arrayOffset(), byteBuffer.arrayOffset()); }
From source file:org.apache.qpid.jms.provider.amqp.message.AmqpReadableBufferTest.java
License:Apache License
@Test public void testGetBytesToWritableBuffer() { byte[] data = new byte[] { 0, 1, 2, 3, 4 }; ByteBuf byteBuffer = Unpooled.wrappedBuffer(data); AmqpReadableBuffer buffer = new AmqpReadableBuffer(byteBuffer); ByteBuf targetBuffer = Unpooled.buffer(data.length, data.length); AmqpWritableBuffer target = new AmqpWritableBuffer(targetBuffer); buffer.get(target);//w w w. ja v a2 s . c o m assertFalse(buffer.hasRemaining()); assertArrayEquals(targetBuffer.array(), data); }
From source file:org.apache.reef.wake.remote.transport.netty.ChunkedReadWriteHandler.java
License:Apache License
/** * Thread-safe since there is no shared instance state. * Just prepend size to the message and stream it through * a chunked stream and let the base method handle the actual * chunking.//w w w. j a va 2s . c om * <p/> * We do not need to tag the writes since the base class ChunkedWriteHandler * serializes access to the channel and first write will complete before * the second begins. */ @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof ByteBuf) { final ByteBuf bf = (ByteBuf) msg; if (bf.hasArray()) { final byte[] data = bf.array(); final byte[] size = sizeAsByteArr(data.length); final ByteBuf writeBuffer = Unpooled.wrappedBuffer(size, data); final ByteBufCloseableStream stream = new ByteBufCloseableStream(writeBuffer); final ChunkedStream chunkedStream = new ChunkedStream(stream, NettyChannelInitializer.MAXFRAMELENGTH - 1024); super.write(ctx, chunkedStream, promise); } else { super.write(ctx, msg, promise); } } else { super.write(ctx, msg, promise); } }
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 w w . jav a2 s. com * * @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; }
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 ww w. j av a2s .c o m*/ * * @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); }