List of usage examples for io.netty.buffer ByteBuf getBytes
public abstract ByteBuf getBytes(int index, ByteBuffer dst);
From source file:org.apache.rocketmq.remoting.netty.FileRegionEncoderTest.java
License:Apache License
/** * This unit test case ensures that {@link FileRegionEncoder} indeed wraps {@link FileRegion} to * {@link ByteBuf}./*from w w w.jav a2s . c o m*/ * @throws IOException if there is an error. */ @Test public void testEncode() throws IOException { FileRegionEncoder fileRegionEncoder = new FileRegionEncoder(); EmbeddedChannel channel = new EmbeddedChannel(fileRegionEncoder); File file = File.createTempFile(UUID.randomUUID().toString(), ".data"); file.deleteOnExit(); Random random = new Random(System.currentTimeMillis()); int dataLength = 1 << 10; byte[] data = new byte[dataLength]; random.nextBytes(data); write(file, data); FileRegion fileRegion = new DefaultFileRegion(file, 0, dataLength); Assert.assertEquals(0, fileRegion.transfered()); Assert.assertEquals(dataLength, fileRegion.count()); Assert.assertTrue(channel.writeOutbound(fileRegion)); ByteBuf out = (ByteBuf) channel.readOutbound(); byte[] arr = new byte[out.readableBytes()]; out.getBytes(0, arr); Assert.assertArrayEquals("Data should be identical", data, arr); }
From source file:org.apache.tajo.engine.planner.UniformRangePartition.java
License:Apache License
/** * * @param last//from w ww. j a v a2s . c o m * @param interval * @return */ public Tuple increment(final Tuple last, BigInteger interval, final int baseDigit) { BigInteger[] incs = new BigInteger[last.size()]; boolean[] overflowFlag = new boolean[last.size()]; BigInteger[] result; BigInteger value = interval; BigInteger[] reverseCardsForDigit = new BigInteger[baseDigit + 1]; for (int i = baseDigit; i >= 0; i--) { if (i == baseDigit) { reverseCardsForDigit[i] = colCards[i]; } else { reverseCardsForDigit[i] = reverseCardsForDigit[i + 1].multiply(colCards[i]); } } for (int i = 0; i < baseDigit; i++) { result = value.divideAndRemainder(reverseCardsForDigit[i + 1]); incs[i] = result[0]; value = result[1]; } int finalId = baseDigit; incs[finalId] = value; for (int i = finalId; i >= 0; i--) { if (isOverflow(i, last, i, incs[i], sortSpecs)) { if (i == 0) { throw new RangeOverflowException(mergedRange, last, incs[i].longValue(), sortSpecs[i].isAscending()); } // increment some volume of the serialized one-dimension key space long rem = incrementAndGetReminder(i, last, value.longValue()); incs[i] = BigInteger.valueOf(rem); incs[i - 1] = incs[i - 1].add(BigInteger.ONE); overflowFlag[i] = true; } else { if (i > 0) { incs[i] = value; break; } } } for (int i = 0; i < incs.length; i++) { if (incs[i] == null) { incs[i] = BigInteger.ZERO; } } VTuple end = new VTuple(sortSpecs.length); Column column; for (int i = 0; i < last.size(); i++) { column = sortSpecs[i].getSortKey(); switch (column.getDataType().getType()) { case CHAR: if (overflowFlag[i]) { end.put(i, DatumFactory .createChar((char) (mergedRange.getStart().getChar(i) + incs[i].longValue()))); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createChar((char) (last.getChar(i) + incs[i].longValue()))); } else { end.put(i, DatumFactory.createChar((char) (last.getChar(i) - incs[i].longValue()))); } } break; case BIT: if (overflowFlag[i]) { end.put(i, DatumFactory .createBit((byte) (mergedRange.getStart().getByte(i) + incs[i].longValue()))); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createBit((byte) (last.getByte(i) + incs[i].longValue()))); } else { end.put(i, DatumFactory.createBit((byte) (last.getByte(i) - incs[i].longValue()))); } } break; case INT2: if (overflowFlag[i]) { end.put(i, DatumFactory .createInt2((short) (mergedRange.getStart().getInt2(i) + incs[i].longValue()))); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createInt2((short) (last.getInt2(i) + incs[i].longValue()))); } else { end.put(i, DatumFactory.createInt2((short) (last.getInt2(i) - incs[i].longValue()))); } } break; case INT4: if (overflowFlag[i]) { end.put(i, DatumFactory .createInt4((int) (mergedRange.getStart().getInt4(i) + incs[i].longValue()))); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createInt4((int) (last.getInt4(i) + incs[i].longValue()))); } else { end.put(i, DatumFactory.createInt4((int) (last.getInt4(i) - incs[i].longValue()))); } } break; case INT8: if (overflowFlag[i]) { end.put(i, DatumFactory.createInt8(mergedRange.getStart().getInt8(i) + incs[i].longValue())); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createInt8(last.getInt8(i) + incs[i].longValue())); } else { end.put(i, DatumFactory.createInt8(last.getInt8(i) - incs[i].longValue())); } } break; case FLOAT4: if (overflowFlag[i]) { end.put(i, DatumFactory.createFloat4(mergedRange.getStart().getFloat4(i) + incs[i].longValue())); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createFloat4(last.getFloat4(i) + incs[i].longValue())); } else { end.put(i, DatumFactory.createFloat4(last.getFloat4(i) - incs[i].longValue())); } } break; case FLOAT8: if (overflowFlag[i]) { end.put(i, DatumFactory.createFloat8(mergedRange.getStart().getFloat8(i) + incs[i].longValue())); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createFloat8(last.getFloat8(i) + incs[i].longValue())); } else { end.put(i, DatumFactory.createFloat8(last.getFloat8(i) - incs[i].longValue())); } } break; case TEXT: if (overflowFlag[i]) { end.put(i, DatumFactory.createText( ((char) (mergedRange.getStart().getText(i).charAt(0) + incs[i].longValue())) + "")); } else { BigInteger lastBigInt; if (last.isBlankOrNull(i)) { lastBigInt = BigInteger.valueOf(0); end.put(i, DatumFactory.createText(lastBigInt.add(incs[i]).toByteArray())); } else { if (isPureAscii[i]) { lastBigInt = new BigInteger(last.getBytes(i)); if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createText(lastBigInt.add(incs[i]).toByteArray())); } else { end.put(i, DatumFactory.createText(lastBigInt.subtract(incs[i]).toByteArray())); } } else { // We consider an array of chars as a 2^16 base number system because each char is 2^16 bits. // See Character.MAX_NUMBER. Then, we increase some number to the last array of chars. char[] lastChars = last.getUnicodeChars(i); int[] charIncs = new int[lastChars.length]; BigInteger remain = incs[i]; for (int k = lastChars.length - 1; k > 0 && remain.compareTo(BigInteger.ZERO) > 0; k--) { BigInteger digitBase = BigInteger.valueOf(TextDatum.UNICODE_CHAR_BITS_NUM).pow(k); if (remain.compareTo(digitBase) > 0) { charIncs[k] = remain.divide(digitBase).intValue(); BigInteger sub = digitBase.multiply(BigInteger.valueOf(charIncs[k])); remain = remain.subtract(sub); } } charIncs[charIncs.length - 1] = remain.intValue(); for (int k = 0; k < lastChars.length; k++) { if (charIncs[k] == 0) { continue; } if (sortSpecs[i].isAscending()) { int sum = (int) lastChars[k] + charIncs[k]; if (sum > TextDatum.UNICODE_CHAR_BITS_NUM) { // if carry occurs in the current digit charIncs[k] = sum - TextDatum.UNICODE_CHAR_BITS_NUM; charIncs[k - 1] += 1; lastChars[k - 1] += 1; lastChars[k] += charIncs[k]; } else { lastChars[k] += charIncs[k]; } } else { int sum = (int) lastChars[k] - charIncs[k]; if (sum < 0) { // if carry occurs in the current digit charIncs[k] = TextDatum.UNICODE_CHAR_BITS_NUM - sum; charIncs[k - 1] -= 1; lastChars[k - 1] -= 1; lastChars[k] += charIncs[k]; } else { lastChars[k] -= charIncs[k]; } } } ByteBuf byteBuf = Unpooled.buffer(lastChars.length * 3); BytesUtils.writeUtf8(byteBuf, lastChars, true); byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.getBytes(0, bytes); end.put(i, DatumFactory.createText(bytes)); } } } break; case DATE: if (overflowFlag[i]) { end.put(i, DatumFactory .createDate((int) (mergedRange.getStart().getInt4(i) + incs[i].longValue()))); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createDate((int) (last.getInt4(i) + incs[i].longValue()))); } else { end.put(i, DatumFactory.createDate((int) (last.getInt4(i) - incs[i].longValue()))); } } break; case TIME: if (overflowFlag[i]) { end.put(i, DatumFactory.createTime(mergedRange.getStart().getInt8(i) + incs[i].longValue())); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory.createTime(last.getInt8(i) + incs[i].longValue())); } else { end.put(i, DatumFactory.createTime(last.getInt8(i) - incs[i].longValue())); } } break; case TIMESTAMP: if (overflowFlag[i]) { end.put(i, DatumFactory.createTimestampDatumWithJavaMillis( mergedRange.getStart().getInt8(i) + incs[i].longValue())); } else { if (sortSpecs[i].isAscending()) { end.put(i, DatumFactory .createTimestampDatumWithJavaMillis(last.getInt8(i) + incs[i].longValue())); } else { end.put(i, DatumFactory .createTimestampDatumWithJavaMillis(last.getInt8(i) - incs[i].longValue())); } } break; default: throw new UnsupportedOperationException(column.getDataType() + " is not supported yet"); } // replace i'th end value by NULL if begin and end are all NULL if (beginNulls[i] && endNulls[i]) { end.put(i, NullDatum.get()); continue; } } return end; }
From source file:org.asynchttpclient.providers.netty4.util.ByteBufUtil.java
License:Apache License
public static byte[] byteBuf2bytes(ByteBuf b) { int readable = b.readableBytes(); int readerIndex = b.readerIndex(); if (b.hasArray()) { byte[] array = b.array(); if (b.arrayOffset() == 0 && readerIndex == 0 && array.length == readable) { return array; }//from ww w . ja va 2s.com } byte[] array = new byte[readable]; b.getBytes(readerIndex, array); return array; }
From source file:org.code_house.ebus.netty.codec.MasterTelegramDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() == 0) { return;// www .ja va 2 s.c om } // buffer we receive might be a correct frame or just data we have between frames int index = in.readerIndex(); if (in.readableBytes() >= 6) { // seven bytes is shortest valid ebus telegram, which no data byte source = in.readByte(); byte destination = in.readByte(); boolean fromMaster = Predicates.IS_MASTER.apply(source); boolean broadcast = Constants.BROADCAST_ADDRESS == destination; boolean toSlave = Predicates.IS_SLAVE.apply(destination); boolean toMaster = Predicates.IS_MASTER.apply(destination); // if below condition is correct then we have regular frame, at least it looks like, as long as we are not sure // if everything is fine we do not read but just scan buffer if (fromMaster && (toMaster || (toSlave || broadcast))) { // short is encoded on two bytes byte commandGroup = in.readByte(); byte command = in.readByte(); byte length = in.readByte(); if (in.readableBytes() >= length + 1 /* crc */) { ByteBuffer exchange = ByteBuffer.allocate(length); in.getBytes(in.readerIndex(), exchange); in.skipBytes(length); byte crc = in.readByte(); MasterData data = new MasterData(exchange, calculateCrc((byte) source, (byte) destination, commandGroup, command, length), crc); // below condition checks crc and verify if we fully consumed frame data and we will leave no remaining // bytes master buffer. If there is any data left or crc doesn't match we might just parse middle of long // telegram which accidentally got spliced or we missed for some reason SYN and just got corrupted // data. if (data.isValid()) { out.add(new MasterHeader(source, destination, commandGroup, command, length)); out.add(data); if (in.readableBytes() > 0) { out.add(in.readBytes(super.actualReadableBytes())); } return; } } } // invalid data found in.readerIndex(index); out.add(in.readBytes(super.actualReadableBytes())); } }
From source file:org.code_house.ebus.netty.codec.SlaveTelegramDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() == 0) { return;//from w w w . j av a 2 s . c om } int index = in.readerIndex(); // no master, no slave, no broadcast -> slave reply or ACK if (in.readableBytes() >= 3) { // we have at least three bytes which should be slave ACK, slave reply len, slave crc. byte ack = in.readByte(); if (ack == Constants.ACK) { // slave confirmed received data byte length = in.readByte(); if (in.readableBytes() >= length + 1 /* crc */) { ByteBuffer exchange = ByteBuffer.allocate(length); in.getBytes(in.readerIndex(), exchange); in.skipBytes(length); byte crc = in.readByte(); SlaveData data = new SlaveData(exchange, calculateCrc(length), crc); if (data.isValid()) { // proper slave reply out.add(new Confirmation()); out.add(new SlaveHeader(length)); out.add(data); } else { System.out.println("Slave CRC fail!!!"); in.discardReadBytes(); } } } else if (ack == Constants.NACK) { out.add(new Rejection()); } } if (out.isEmpty()) { System.out.println("slave fail -> \n" + ByteBufUtil.prettyHexDump(in)); in.readerIndex(index); } if (in.readableBytes() > 0) { out.add(in.readBytes(super.actualReadableBytes())); } }
From source file:org.dcache.xrootd.core.XrootdSigverDecoder.java
License:Open Source License
private byte[] extractContents(int requestId, int flags, ByteBuf frame) throws XrootdException { int len;//w w w . j a v a 2 s .co m /* * If this is a write request, kXR_nodata should be set; * extract only the header. */ if (requestId == kXR_write) { if (flags != kXR_nodata) { throw new XrootdException(kXR_error, "signed hash verification:" + " kXR_nodata not set, " + "cannot verify write request."); } len = 24; } else { len = frame.readableBytes(); } byte[] contents = new byte[len]; frame.getBytes(0, contents); return contents; }
From source file:org.dcache.xrootd.core.XrootdSigverDecoder.java
License:Open Source License
/** * A signature consists of a SHA-256 hash of * 1. an unsigned 64-bit sequence number, * 2. the request header, and//w ww.j av a 2 s . c o m * 3. the request payload, * in that exact order. * * In this case, 2 + 3 are given in order by the frame buffer, which * contains the raw bytes of the request. */ private byte[] generateHash(long seqno, byte[] payload, ChannelHandlerContext ctx) throws XrootdException { ByteBuf buffer = ctx.alloc().buffer(8 + payload.length); try { buffer.writeLong(seqno); buffer.writeBytes(payload); byte[] contents = new byte[buffer.readableBytes()]; buffer.getBytes(0, contents); MessageDigest digest = MessageDigest.getInstance("SHA-256"); return digest.digest(contents); } catch (NoSuchAlgorithmException e) { throw new XrootdException(kXR_error, e.toString()); } finally { buffer.release(); } }
From source file:org.dcache.xrootd.protocol.messages.EndSessionRequest.java
License:Open Source License
public EndSessionRequest(ByteBuf buffer) { super(buffer, kXR_endsess); buffer.getBytes(4, session); }
From source file:org.dcache.xrootd.protocol.messages.SigverRequest.java
License:Open Source License
public SigverRequest(ByteBuf buffer) { super(buffer, kXR_sigver); expectrid = buffer.getShort(4);/*from ww w .j ava 2 s . co m*/ version = buffer.getByte(6); flags = buffer.getByte(7); // should == kXR_nodata if this is a write seqno = buffer.getLong(8); crypto = buffer.getByte(16); /* * skip reserved [bytes 17-19] */ int dlen = buffer.getInt(20); signature = new byte[dlen]; buffer.getBytes(24, signature); }
From source file:org.dcache.xrootd.protocol.messages.WriteRequest.java
License:Open Source License
public WriteRequest(ByteBuf buffer) { super(buffer, kXR_write); fhandle = buffer.getInt(4);//from w w w . jav a 2s . c om offset = buffer.getLong(8); dlen = buffer.getInt(20); data = buffer.alloc().ioBuffer(dlen); // Most likely this will be written to disk buffer.getBytes(24, data); }