List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
From source file:com.streamsets.pipeline.lib.parser.net.DataFormatParserDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { final int numBytes = in.readableBytes(); final byte[] bytes = new byte[numBytes]; in.readBytes(bytes); DataParser parser = parserFactory.getParser(generateRecordId(), bytes); Record record;/*w w w. j av a2 s . com*/ try { while ((record = parser.parse()) != null) { out.add(record); } } catch (RecoverableDataParserException e) { // allow to return } catch (Exception e) { ExceptionUtils.throwUndeclared(new OnRecordErrorException(Errors.DATA_PARSER_04, e.getMessage(), e)); } finally { parser.close(); } }
From source file:com.teambr.modularsystems.core.network.SyncBlockValues.java
License:Creative Commons License
@Override public void fromBytes(ByteBuf buf) { byte[] compressedBytes = null; int readableBytes = buf.readInt(); if (readableBytes > 0) compressedBytes = buf.readBytes(readableBytes).array(); if (compressedBytes != null) this.jsonBlockValues = CompressionFunctions.decompressStringFromByteArray(compressedBytes); }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
public static byte[] readBytes(ByteBuf cb, int len) { byte[] ret = new byte[len]; cb.readBytes(ret); return ret;// w w w . j a v a2 s. co m }
From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java
License:Open Source License
public static String readBytesAsString(ByteBuf cb, int len, Charset cs) { return cb.readBytes(len).toString(cs); }
From source file:com.tesora.dve.db.mysql.libmy.MyBinaryResultRow.java
License:Open Source License
@Override public void unmarshallMessage(ByteBuf cb) throws PEException { int expectedFieldCount = fieldConverters.size(); int expectedBitmapLength = MyNullBitmap.computeSize(expectedFieldCount, MyNullBitmap.BitmapType.RESULT_ROW); cb = cb.order(ByteOrder.LITTLE_ENDIAN); cb.skipBytes(1);//skip the bin row marker. byte[] nullBitmap = new byte[expectedBitmapLength]; cb.readBytes(nullBitmap); MyNullBitmap resultBitmap = new MyNullBitmap(nullBitmap, expectedFieldCount, MyNullBitmap.BitmapType.RESULT_ROW); ByteBuf values = cb;//from w w w . j a va 2 s.com for (int i = 0; i < expectedFieldCount; i++) { ByteBuf existing = fieldSlices.get(i); ByteBuf nextSlice = null; int startIndex = values.readerIndex(); if (!resultBitmap.getBit(i + 1)) { fieldConverters.get(i).readObject(values);//TODO: we throw out the unmarshalled value, we could cache it. int endingOffset = values.readerIndex(); nextSlice = values.slice(startIndex, endingOffset - startIndex); } if (existing != null) existing.release(); fieldSlices.set(i, nextSlice); } if (cb.readableBytes() > 0) { log.warn("Decoded binary row had {} leftover bytes, re-encoding may fail.", cb.readableBytes()); cb.skipBytes(cb.readableBytes());//consume rest of buffer. } }
From source file:com.tesora.dve.mysqlapi.repl.messages.MyStatusVariables.java
License:Open Source License
public void parseStatusVariables(ByteBuf cb, int svLen) throws PEException { if (svLen > 0) { ByteBuf statsVarsBuf = cb.readBytes(svLen); while (statsVarsBuf.isReadable()) { byte code = statsVarsBuf.readByte(); MyQueryEventCode mqec = MyQueryEventCode.fromByte(code); if (mqec == null) { throw new PEException("Replication could not decode query event code: '" + code + "' (0x" + Integer.toHexString(code) + ")"); }//from ww w. j ava 2 s .c o m switch (mqec) { case Q_FLAGS2_CODE: int flags = statsVarsBuf.readInt(); suppliedEventCodes.add(new QueryFlags2Event(flags)); break; case Q_SQL_MODE_CODE: long sqlMode = statsVarsBuf.readLong(); suppliedEventCodes.add(new QuerySQLModeEvent(sqlMode)); break; case Q_CATALOG_CODE: { byte len = statsVarsBuf.readByte(); String catalog = MysqlAPIUtils.readBytesAsString(statsVarsBuf, len, CharsetUtil.UTF_8); statsVarsBuf.readByte(); // null terminated byte suppliedEventCodes.add(new QueryCatalogEvent(catalog)); break; } case Q_AUTO_INCREMENT: int autoIncrementIncrement = statsVarsBuf.readUnsignedShort(); int autoIncrementOffset = statsVarsBuf.readUnsignedShort(); suppliedEventCodes .add(new QueryAutoIncrementEvent(autoIncrementIncrement, autoIncrementOffset)); break; case Q_CHARSET_CODE: int charSetClient = statsVarsBuf.readUnsignedShort(); int collationConnection = statsVarsBuf.readUnsignedShort(); int collationServer = statsVarsBuf.readUnsignedShort(); suppliedEventCodes .add(new QueryCharSetCodeEvent(charSetClient, collationConnection, collationServer)); break; case Q_TIME_ZONE_CODE: { byte len = statsVarsBuf.readByte(); String timeZone = MysqlAPIUtils.readBytesAsString(statsVarsBuf, len, CharsetUtil.UTF_8); suppliedEventCodes.add(new QueryTimeZoneCodeEvent(timeZone)); break; } case Q_CATALOG_NZ_CODE: { byte catalogLen = statsVarsBuf.readByte(); String catalog = MysqlAPIUtils.readBytesAsString(statsVarsBuf, catalogLen, CharsetUtil.UTF_8); suppliedEventCodes.add(new QueryCatalogNZEvent(catalog)); break; } case Q_LC_TIME_NAMES_CODE: short monthDayNames = statsVarsBuf.readShort(); suppliedEventCodes.add(new QueryTimeNamesEvent(monthDayNames)); break; case Q_CHARSET_DATABASE_CODE: short collationDatabase = statsVarsBuf.readShort(); suppliedEventCodes.add(new QueryCollationDatabaseEvent(collationDatabase)); break; case Q_TABLE_MAP_FOR_UPDATE_CODE: long tableMapForUpdate = statsVarsBuf.readLong(); suppliedEventCodes.add(new QueryTableMapEvent(tableMapForUpdate)); break; case Q_MASTER_DATA_WRITTEN_CODE: int originalLength = statsVarsBuf.readInt(); suppliedEventCodes.add(new QueryMasterDataWrittenEvent(originalLength)); break; case Q_INVOKER: int userLen = statsVarsBuf.readByte(); String user = MysqlAPIUtils.readBytesAsString(statsVarsBuf, userLen, CharsetUtil.UTF_8); int hostLen = statsVarsBuf.readByte(); String host = MysqlAPIUtils.readBytesAsString(statsVarsBuf, hostLen, CharsetUtil.UTF_8); suppliedEventCodes.add(new QueryInvokerEvent(user, host)); break; case Q_UPDATED_DB_NAMES: List<String> dbNames = new ArrayList<String>(); int numDbs = statsVarsBuf.readByte(); if (numDbs > 0) { for (int i = 0; i < numDbs; i++) { dbNames.add(statsVarsBuf.readSlice(statsVarsBuf.bytesBefore((byte) 0)) .toString(CharsetUtil.UTF_8)); statsVarsBuf.readByte(); //read null byte } } suppliedEventCodes.add(new QueryUpdatedDBNamesEvent(dbNames)); break; case Q_MICROSECONDS: int microseconds = statsVarsBuf.readMedium(); suppliedEventCodes.add(new QueryMicrosecondsEvent(microseconds)); break; case Q_HRNOW: //TODO: this was apparently added for MariaDB, but I can't find a lot of info on it. skip for now. suppliedEventCodes.add(new QueryMicrosecondsEvent(statsVarsBuf.readUnsignedMedium())); break; default: throw new PEException("Replication encountered an unknown query event code: '" + code + "' (0x" + Integer.toHexString(code) + ")"); } } } }
From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java
License:Open Source License
String processDecimalValue(ByteBuf cb, int valueLen) throws PEException { String value = StringUtils.EMPTY; byte precision = cb.readByte(); byte scale = cb.readByte(); int intg = (int) precision - (int) scale; int intg0 = intg / DIG_PER_DEC1; int frac0 = (int) scale / DIG_PER_DEC1; int intg0x = intg - intg0 * DIG_PER_DEC1; int frac0x = (int) scale - frac0 * DIG_PER_DEC1; int firstValue = intg0 * 4 + dig2bytes[intg0x]; int secondValue = frac0 * 4 + dig2bytes[frac0x]; int binSize = firstValue + secondValue; int readableBytes = cb.readableBytes(); if ((firstValue < 1 && secondValue < 1) || readableBytes < binSize) { throw new PEException("Cannot decode binary decimal"); }//from w w w . j a v a 2s .c om ByteBuf chunk = PooledByteBufAllocator.DEFAULT.heapBuffer(binSize); cb.readBytes(chunk); // 1st byte is special cause it determines the sign byte firstByte = chunk.getByte(0); int sign = (firstByte & 0x80) == 0x80 ? 1 : -1; // invert sign chunk.setByte(0, (firstByte ^ 0x80)); if (sign == -1) { // invert all the bytes for (int i = 0; i < binSize; i++) { chunk.setByte(i, ~chunk.getByte(i)); } } BigDecimal integerPortion = decodeBinDecimal(chunk, firstValue, true); BigDecimal fractionPortion = decodeBinDecimal(chunk, secondValue, false); value = ((sign == -1) ? "-" : StringUtils.EMPTY) + integerPortion.toPlainString() + "." + fractionPortion.toPlainString(); return value; }
From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java
License:Open Source License
BigDecimal decodeBinDecimal(ByteBuf cb, int bufferLen, boolean isIntegerPortion) throws PEException { BigDecimal decimalPortion = new BigDecimal(0); if (bufferLen > 0) { ByteBuf decimalPortionBuf = cb.readBytes(bufferLen); if (isIntegerPortion) { int initialBytes = bufferLen % 4; if (initialBytes > 0) { long intValue = readValue(decimalPortionBuf, initialBytes); decimalPortion = BigDecimal.valueOf(intValue); }//from ww w .j ava 2 s . c o m } int decimalPortionLen = decimalPortionBuf.readableBytes(); while (decimalPortionLen > 0) { int nextLen = (decimalPortionLen < 4) ? decimalPortionLen : 4; long intValue = readValue(decimalPortionBuf, nextLen); if (intValue > 0) { if (decimalPortion.longValue() == 0) { decimalPortion = decimalPortion.add(BigDecimal.valueOf(intValue)); } else { int digits = (int) (Math.log10(intValue) + 1); decimalPortion = decimalPortion.movePointRight(digits).add(BigDecimal.valueOf(intValue)); } } decimalPortionLen = decimalPortionBuf.readableBytes(); } } return decimalPortion; }
From source file:com.theoriginalbit.moarperipherals.common.network.message.MessageGeneric.java
License:Apache License
@Override public void fromBytes(ByteBuf buf) { int nStr = buf.readInt(); stringData = new String[nStr]; if (nStr > 0) { for (int i = 0; i < nStr; ++i) { stringData[i] = ByteBufUtils.readUTF8String(buf); }/* w w w .j ava 2s.c o m*/ } int nInt = buf.readInt(); intData = new int[nInt]; if (nInt > 0) { for (int i = 0; i < nInt; ++i) { intData[i] = buf.readInt(); } } int nByte = buf.readInt(); byteData = new byte[nByte]; if (nByte > 0) { buf.readBytes(byteData); } int nChar = buf.readInt(); charData = new char[nChar]; if (nChar > 0) { for (int i = 0; i < nChar; ++i) { charData[i] = buf.readChar(); } } int nFloat = buf.readInt(); floatData = new float[nFloat]; if (nFloat > 0) { for (int i = 0; i < nFloat; ++i) { floatData[i] = buf.readFloat(); } } int nDouble = buf.readInt(); doubleData = new double[nDouble]; if (nDouble > 0) { for (int i = 0; i < nDouble; ++i) { doubleData[i] = buf.readDouble(); } } boolean wasNBT = buf.readBoolean(); if (wasNBT) { nbtData = ByteBufUtils.readTag(buf); } }
From source file:com.torchmind.netty.msgpack.codec.MessageCodec.java
License:Apache License
/** * {@inheritDoc}// w ww .ja v a 2s . c o m */ @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // read identifier short identifier = in.readShort(); // search message type Class<?> type = this.getRegistry().getMessageType(identifier); // read data byte[] data = new byte[in.readableBytes()]; in.readBytes(data); // decode message out.add(this.messagePack.read(data, type)); }