List of usage examples for io.netty.buffer ByteBuf getInt
public abstract int getInt(int index);
From source file:org.apache.drill.common.util.DecimalUtility.java
License:Apache License
public static void getSparseFromBigDecimal(BigDecimal input, ByteBuf data, int startIndex, int scale, int precision, int nDecimalDigits) { // Initialize the buffer for (int i = 0; i < nDecimalDigits; i++) { data.setInt(startIndex + (i * integerSize), 0); }/* w w w.j a va 2 s.co m*/ boolean sign = false; if (input.signum() == -1) { // negative input sign = true; input = input.abs(); } // Truncate the input as per the scale provided input = input.setScale(scale, BigDecimal.ROUND_HALF_UP); // Separate out the integer part BigDecimal integerPart = input.setScale(0, BigDecimal.ROUND_DOWN); int destIndex = nDecimalDigits - roundUp(scale) - 1; // we use base 1 billion integer digits for out integernal representation BigDecimal base = new BigDecimal(DIGITS_BASE); while (integerPart.compareTo(BigDecimal.ZERO) == 1) { // store the modulo as the integer value data.setInt(startIndex + (destIndex * integerSize), (integerPart.remainder(base)).intValue()); destIndex--; // Divide by base 1 billion integerPart = (integerPart.divide(base)).setScale(0, BigDecimal.ROUND_DOWN); } /* Sparse representation contains padding of additional zeroes * so each digit contains MAX_DIGITS for ease of arithmetic */ int actualDigits; if ((actualDigits = (scale % MAX_DIGITS)) != 0) { // Pad additional zeroes scale = scale + (MAX_DIGITS - actualDigits); input = input.setScale(scale, BigDecimal.ROUND_DOWN); } //separate out the fractional part BigDecimal fractionalPart = input.remainder(BigDecimal.ONE).movePointRight(scale); destIndex = nDecimalDigits - 1; while (scale > 0) { // Get next set of MAX_DIGITS (9) store it in the ByteBuf fractionalPart = fractionalPart.movePointLeft(MAX_DIGITS); BigDecimal temp = fractionalPart.remainder(BigDecimal.ONE); data.setInt(startIndex + (destIndex * integerSize), (temp.unscaledValue().intValue())); destIndex--; fractionalPart = fractionalPart.setScale(0, BigDecimal.ROUND_DOWN); scale -= MAX_DIGITS; } // Set the negative sign if (sign == true) { data.setInt(startIndex, data.getInt(startIndex) | 0x80000000); } }
From source file:org.apache.drill.common.util.DecimalUtility.java
License:Apache License
public static int getIntegerFromSparseBuffer(ByteBuf buffer, int start, int index) { int value = buffer.getInt(start + (index * 4)); if (index == 0) { /* the first byte contains sign bit, return value without it */ value = (value & 0x7FFFFFFF); }//from w ww . j a v a 2 s. c om return value; }
From source file:org.apache.drill.common.util.DecimalUtility.java
License:Apache License
public static int getFirstFractionalDigit(ByteBuf data, int scale, int start, int nDecimalDigits) { if (scale == 0) { return 0; }/* ww w . jav a 2s. c o m*/ int index = nDecimalDigits - roundUp(scale); return (int) (adjustScaleDivide(data.getInt(start + (index * integerSize)), MAX_DIGITS - 1)); }
From source file:org.apache.drill.exec.util.DecimalUtility.java
License:Apache License
public static BigDecimal getBigDecimalFromDrillBuf(ByteBuf data, int startIndex, int nDecimalDigits, int scale, boolean truncateScale) { // For sparse decimal type we have padded zeroes at the end, strip them while converting to BigDecimal. int actualDigits; // Initialize the BigDecimal, first digit in the DrillBuf has the sign so mask it out BigInteger decimalDigits = BigInteger.valueOf((data.getInt(startIndex)) & 0x7FFFFFFF); BigInteger base = BigInteger.valueOf(DIGITS_BASE); for (int i = 1; i < nDecimalDigits; i++) { BigInteger temp = BigInteger.valueOf(data.getInt(startIndex + (i * INTEGER_SIZE))); decimalDigits = decimalDigits.multiply(base); decimalDigits = decimalDigits.add(temp); }//from w ww. j a v a2 s . c o m // Truncate any additional padding we might have added if (truncateScale == true && scale > 0 && (actualDigits = scale % MAX_DIGITS) != 0) { BigInteger truncate = BigInteger.valueOf((int) Math.pow(10, (MAX_DIGITS - actualDigits))); decimalDigits = decimalDigits.divide(truncate); } // set the sign if ((data.getInt(startIndex) & 0x80000000) != 0) { decimalDigits = decimalDigits.negate(); } BigDecimal decimal = new BigDecimal(decimalDigits, scale); return decimal; }
From source file:org.apache.drill.exec.util.DecimalUtility.java
License:Apache License
public static void getSparseFromBigDecimal(BigDecimal input, ByteBuf data, int startIndex, int scale, int precision, int nDecimalDigits) { // Initialize the buffer for (int i = 0; i < nDecimalDigits; i++) { data.setInt(startIndex + (i * INTEGER_SIZE), 0); }/*from w w w . j a v a2 s. c om*/ boolean sign = false; if (input.signum() == -1) { // negative input sign = true; input = input.abs(); } // Truncate the input as per the scale provided input = input.setScale(scale, BigDecimal.ROUND_HALF_UP); // Separate out the integer part BigDecimal integerPart = input.setScale(0, BigDecimal.ROUND_DOWN); int destIndex = nDecimalDigits - roundUp(scale) - 1; // we use base 1 billion integer digits for out integernal representation BigDecimal base = new BigDecimal(DIGITS_BASE); while (integerPart.compareTo(BigDecimal.ZERO) == 1) { // store the modulo as the integer value data.setInt(startIndex + (destIndex * INTEGER_SIZE), (integerPart.remainder(base)).intValue()); destIndex--; // Divide by base 1 billion integerPart = (integerPart.divide(base)).setScale(0, BigDecimal.ROUND_DOWN); } /* Sparse representation contains padding of additional zeroes * so each digit contains MAX_DIGITS for ease of arithmetic */ int actualDigits; if ((actualDigits = (scale % MAX_DIGITS)) != 0) { // Pad additional zeroes scale = scale + (MAX_DIGITS - actualDigits); input = input.setScale(scale, BigDecimal.ROUND_DOWN); } //separate out the fractional part BigDecimal fractionalPart = input.remainder(BigDecimal.ONE).movePointRight(scale); destIndex = nDecimalDigits - 1; while (scale > 0) { // Get next set of MAX_DIGITS (9) store it in the DrillBuf fractionalPart = fractionalPart.movePointLeft(MAX_DIGITS); BigDecimal temp = fractionalPart.remainder(BigDecimal.ONE); data.setInt(startIndex + (destIndex * INTEGER_SIZE), (temp.unscaledValue().intValue())); destIndex--; fractionalPart = fractionalPart.setScale(0, BigDecimal.ROUND_DOWN); scale -= MAX_DIGITS; } // Set the negative sign if (sign == true) { data.setInt(startIndex, data.getInt(startIndex) | 0x80000000); } }
From source file:org.apache.flink.runtime.io.network.netty.OutboundEnvelopeEncoder.java
License:Apache License
private void encode(Envelope env, ByteBuf out) { // -------------------------------------------------------------------- // (1) header (48 bytes) // -------------------------------------------------------------------- out.writeInt(MAGIC_NUMBER); // 4 bytes if (out.getInt(out.writerIndex() - 4) != MAGIC_NUMBER) { throw new RuntimeException(); }/*w w w . j a v a2 s . c o m*/ out.writeInt(env.getSequenceNumber()); // 4 bytes env.getJobID().writeTo(out); // 16 bytes env.getSource().writeTo(out); // 16 bytes out.writeInt(env.getEventsSerialized() != null ? env.getEventsSerialized().remaining() : 0); // 4 bytes out.writeInt(env.getBuffer() != null ? env.getBuffer().size() : 0); // 4 bytes // -------------------------------------------------------------------- // (2) events (var length) // -------------------------------------------------------------------- if (env.getEventsSerialized() != null) { out.writeBytes(env.getEventsSerialized()); } // -------------------------------------------------------------------- // (3) buffer (var length) // -------------------------------------------------------------------- if (env.getBuffer() != null) { Buffer envBuffer = env.getBuffer(); out.writeBytes(envBuffer.getMemorySegment().wrap(0, envBuffer.size())); // Recycle the buffer from OUR buffer pool after everything has been // copied to Nettys buffer space. envBuffer.recycleBuffer(); } }
From source file:org.apache.hadoop.hbase.security.SaslChallengeDecoder.java
License:Apache License
private ByteBuf tryDecodeChallenge(ByteBuf in, int offset, int readableBytes) throws IOException { if (readableBytes < 4) { return null; }/*from www. j a va 2s . c o m*/ int len = in.getInt(offset); if (len <= 0) { // fall back to simple in.readerIndex(offset + 4); return in.retainedSlice(offset, 4); } if (len > MAX_CHALLENGE_SIZE) { throw new IOException("Sasl challenge too large(" + len + "), max allowed is " + MAX_CHALLENGE_SIZE); } int totalLen = 4 + len; if (readableBytes < totalLen) { return null; } in.readerIndex(offset + totalLen); return in.retainedSlice(offset, totalLen); }
From source file:org.apache.hadoop.hbase.security.SaslChallengeDecoder.java
License:Apache License
private void tryDecodeError(ByteBuf in, int offset, int readableBytes) throws IOException { if (readableBytes < 4) { return;//from w w w .j av a 2s.co m } int classLen = in.getInt(offset); if (classLen <= 0) { throw new IOException("Invalid exception class name length " + classLen); } if (classLen > MAX_CHALLENGE_SIZE) { throw new IOException("Exception class name length too large(" + classLen + "), max allowed is " + MAX_CHALLENGE_SIZE); } if (readableBytes < 4 + classLen + 4) { return; } int msgLen = in.getInt(offset + 4 + classLen); if (msgLen <= 0) { throw new IOException("Invalid exception message length " + msgLen); } if (msgLen > MAX_CHALLENGE_SIZE) { throw new IOException( "Exception message length too large(" + msgLen + "), max allowed is " + MAX_CHALLENGE_SIZE); } int totalLen = classLen + msgLen + 8; if (readableBytes < totalLen) { return; } String className = in.toString(offset + 4, classLen, HConstants.UTF8_CHARSET); String msg = in.toString(offset + classLen + 8, msgLen, HConstants.UTF8_CHARSET); in.readerIndex(offset + totalLen); throw new RemoteException(className, msg); }
From source file:org.apache.hadoop.hbase.security.SaslChallengeDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int readableBytes = in.readableBytes(); if (readableBytes < 4) { return;// w w w. j a v a 2 s. co m } int offset = in.readerIndex(); int status = in.getInt(offset); if (status == SaslStatus.SUCCESS.state) { ByteBuf challenge = tryDecodeChallenge(in, offset + 4, readableBytes - 4); if (challenge != null) { out.add(challenge); } } else { tryDecodeError(in, offset + 4, readableBytes - 4); } }
From source file:org.dcache.xrootd.core.AbstractXrootdDecoder.java
License:Open Source License
protected int verifyMessageLength(ByteBuf in) { int readable = in.readableBytes(); /* All other requests have a common framing format with a * fixed length header.// w ww.ja v a2 s .c om */ if (readable < CLIENT_REQUEST_LEN) { return 0; } int pos = in.readerIndex(); int headerFrameLength = in.getInt(pos + 20); if (headerFrameLength < 0) { LOGGER.error("Received illegal frame length in xrootd header: {}." + " Closing channel.", headerFrameLength); return -1; } int length = CLIENT_REQUEST_LEN + headerFrameLength; if (readable < length) { return 0; } return length; }