Java Code Examples for io.netty.buffer.ByteBuf.array()

The following are Jave code examples for showing how to use array() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: spark_deep   File: SaslEncryption.java   Source Code and License Vote up 6 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
  throws Exception {

  byte[] data;
  int offset;
  int length = msg.readableBytes();
  if (msg.hasArray()) {
    data = msg.array();
    offset = msg.arrayOffset();
    msg.skipBytes(length);
  } else {
    data = new byte[length];
    msg.readBytes(data);
    offset = 0;
  }

  out.add(Unpooled.wrappedBuffer(backend.unwrap(data, offset, length)));
}
 
Example 2
Project: wecard-server   File: ProtobufDecoder.java   Source Code and License Vote up 5 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }

    if (extensionRegistry == null) {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length).build());
        }
    } else {
        if (HAS_PARSER) {
            out.add(prototype.getParserForType().parseFrom(array, offset, length, extensionRegistry));
        } else {
            out.add(prototype.newBuilderForType().mergeFrom(array, offset, length, extensionRegistry).build());
        }
    }
}
 
Example 3
Project: wecard-server   File: ProtobufDecoder.java   Source Code and License Vote up 5 votes
public Object decode(ByteBuf msg) throws Exception {
    final byte[] array;
    final int offset;
    final int length = msg.readableBytes();
    if (msg.hasArray()) {
        array = msg.array();
        offset = msg.arrayOffset() + msg.readerIndex();
    } else {
        array = new byte[length];
        msg.getBytes(msg.readerIndex(), array, 0, length);
        offset = 0;
    }

    return prototype.getParserForType().parseFrom(array, offset, length);
}
 
Example 4
Project: incubator-servicecomb-java-chassis   File: VertxUtils.java   Source Code and License Vote up 5 votes
public static byte[] getBytesFast(ByteBuf byteBuf) {
  if (byteBuf.hasArray()) {
    return byteBuf.array();
  }

  byte[] arr = new byte[byteBuf.writerIndex()];
  byteBuf.getBytes(0, arr);
  return arr;
}
 
Example 5
Project: LibraSock   File: LibraMessage.java   Source Code and License Vote up 5 votes
/**
 * 解码
 * 
 * @param in
 * @return
 * @throws Exception
 */
public static LibraMessage decode(ByteBuf in) throws Exception {
	LibraMessage message = new LibraMessage();
	message.setHead(LibraHead.decode(in));
	short bodyLength = in.readShort();// 消息体的长度
	if (bodyLength != in.readableBytes()) {
		LibraLog.error("LibraMessage.decode is error! params:bodyLength:" + bodyLength + "\treadableLength:" + in.readableBytes());
		return null;
	}
	ByteBuf bodyByteBuf = in.readBytes(in.readableBytes());
	byte[] array;
	// 反序列化数据的起始点
	int offset;
	// 可读的数据字节长度
	int readableLen = bodyByteBuf.readableBytes();
	// 分为包含数组数据和不包含数组数据两种形式
	if (bodyByteBuf.hasArray()) {
		array = bodyByteBuf.array();
		offset = bodyByteBuf.arrayOffset() + bodyByteBuf.readerIndex();
	} else {
		array = new byte[readableLen];
		bodyByteBuf.getBytes(bodyByteBuf.readerIndex(), array, 0, readableLen);
		offset = 0;
	}
	// 反序列化
	message.setBody(decodeBody(message.getHead().getProtocolID(), array, offset, readableLen));
	return message;
}
 
Example 6
Project: NettyStudy   File: ByteBufExamples.java   Source Code and License Vote up 5 votes
/**
 * Listing 5.1
 */
public static void heapBuffer(ByteBuf heapBuf) {
    if (heapBuf.hasArray()) {
    	System.out.println("has array");
        byte[] array = heapBuf.array();
        int offset = heapBuf.arrayOffset() + heapBuf.readerIndex();
        int length = heapBuf.readableBytes();
        System.out.println(offset+" " + length);
        handleArray(array, offset, length);
    }
}
 
Example 7
Project: sctalk   File: PacketDecoder.java   Source Code and License Vote up 4 votes
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {

    try {

        logger.debug("Protobuf decode started.");
        in.markReaderIndex();
        if (in.readableBytes() < 4) {
            logger.info("Readable Bytes length less than 4 bytes, ignored");
            in.resetReaderIndex();
            return;
        }

        DataBuffer dataBuf = new DataBuffer(in);

        IMHeader header = new IMHeader();
        header.decode(dataBuf);

        if (header.getLength() < 0) {
            ctx.close();
            logger.error("message length less than 0, channel closed");
            return;
        }

        ByteBuf byteBuf = ctx.alloc().buffer(header.getLength() - SysConstant.PROTOCOL_HEADER_LENGTH);

        in.readBytes(byteBuf);
        byte[] body;
        if (byteBuf.hasArray()) {
            body = byteBuf.array();
        } else {
            body = new byte[byteBuf.capacity()];
            byteBuf.readBytes(body);
        }

        MessageLite msg = ProtobufParseMap.getMessage(header.getServiceId(), header.getCommandId(), body);

        IMProtoMessage<MessageLite> protoMessage = new IMProtoMessage<>(header, msg);
        out.add(protoMessage);
        
        logger.debug("Received protobuf : length={}, commandId={}", header.getLength(), header.getCommandId());
    } catch (Exception e) {
        logger.error(ctx.channel().remoteAddress() + ",decode failed.", e);
    } finally {
        logger.debug("Protobuf decode finished.");
    }
}
 
Example 8
Project: MineDonate   File: ModShopManager.java   Source Code and License Vote up 3 votes
public static void addEntityToShop ( Account acc, Shop s, int catId, int limit, int cost, String name ) {

		EntityInfo info = new EntityInfo(s.sid, catId, s.cats[catId].getNextMerchId(), 0, Integer.valueOf(cost), acc.manageSession.currentMob, name, limit);
        
        s.cats[catId].addMerch(info);
       
        ModNetworkRegistry . sendToAllAddMerchPacket ( info ) ;

        PreparedStatement pstat = null ;

        try {
          
            ByteBuf buf = Unpooled.buffer();
            
            ByteBufUtils.writeTag(buf, info.entity_data);
            
            InputStream stream = new ByteArrayInputStream(buf.array());
        
            pstat = ModDataBase . getPreparedStatement ( "main", "INSERT INTO " + s.cats[catId].getDatabaseTable() + " (name, data, cost, lim) VALUES(?,?,?,?)");
            
            pstat.setString(1, name);
            pstat.setBlob(2, stream);
            pstat.setInt(3, cost);
            pstat.setInt(4, limit);

            pstat.execute();
            
        } catch ( Exception ex ) {
            
        	ex . printStackTrace ( ) ;
            
        }
        
        ModDataBase . closePreparedStatementAndConnection ( pstat ) ;

	}
 
Example 9
Project: MineDonate   File: ModShopManager.java   Source Code and License Vote up 3 votes
public static void addItemToShop ( Account acc, Shop s, int catId, int limit, int cost, String name ) {
	
   	ItemInfo info = new ItemInfo(s.sid, catId, s.cats[catId].getNextMerchId(), 0, Integer.valueOf(cost), name, Integer.valueOf(limit), acc.manageSession.currentItemStack);

       PreparedStatement pstat = null ;

       try {
         
       	ByteBuf buf = Unpooled . buffer ( ) ;
        
           NBTTagCompound nbt = new NBTTagCompound ( ) ;
           
           acc . manageSession . currentItemStack . writeToNBT ( nbt ) ;
		acc . manageSession . setItemStack ( null ) ;
           
           s . cats [ catId ] . addMerch ( info ) ;
           
           ModNetworkRegistry . sendToAllAddMerchPacket ( info ) ;
           
           ByteBufUtils.writeTag(buf, nbt);

           InputStream stream = new ByteArrayInputStream(buf.array());
          
           
           if ( s . sid == 0 ) {
           
           	pstat = ModDataBase . getPreparedStatement ( "main", "INSERT INTO " + s.cats[catId].getDatabaseTable() + " (name, cost, lim, stack_data) VALUES(?,?,?,?)");
           
           } else {
        
           	pstat = ModDataBase . getPreparedStatement ( "main", "INSERT INTO " + s.cats[catId].getDatabaseTable() + " (name, cost, lim, stack_data, shopId) VALUES(?,?,?,?,?)");
           	pstat.setInt(5, s.sid);

           }
           
           pstat.setString(1, name);
           pstat.setInt(2, Integer.valueOf(cost));
           pstat.setInt(3, Integer.valueOf(limit));
           pstat.setBlob(4, stream);
           pstat.execute();
           
       } catch ( Exception ex ) {
           
       	ex . printStackTrace ( ) ;
           
       }
       
       ModDataBase . closePreparedStatementAndConnection ( pstat ) ;

}