【问题标题】:How to Clear byte buffer in android如何在android中清除字节缓冲区
【发布时间】:2015-06-16 05:44:08
【问题描述】:

我正在做一个应用程序,其中包括从 android 向微控制器发送接收数据,在接收端,从微控制器到 android 我使用字节缓冲区来存储数据。但在这里我遇到问题我想在不同阶段接收多个字节的数据为此我想清除字节缓冲区并再次重新使用相同的字节缓冲区谁能告诉我如何清除字节缓冲区

这是我的接收代码:

ByteBuffer buffer = ByteBuffer.allocate(64);
         byte[] length = buffer.array();
         byte[] Recevied_Data = new byte[64];
         UsbRequest request = new UsbRequest();

         while (true) 
         {
            request.initialize(UsbDriver.USB_Device_Connection, UsbDriver.Data_In_End_Point); 
            request.queue(buffer, 64);
             if (UsbDriver.USB_Device_Connection.requestWait() == request) 
             {
                for(int i=0;i<length.length;i++)
                {   
                    Recevied_Data[i]=buffer.get(i);
                    if(Recevied_Data[0]==0x02 && Recevied_Data[1]==0x3B && Recevied_Data[2]==0x3B && Recevied_Data[3]==0x00 && Recevied_Data[4]==0x03){
                    Communication_Ok=true;
                //  **I want to clear the Buffer Here**           


                }
             }

             }
         } 

【问题讨论】:

  • 请尝试阅读stackoverflow.com/questions/8462200/… 可能对您有所帮助。
  • 你试过了吗:buffer.clear()?
  • 但是在这里我将字节缓冲区分配给 Received_data[i] 我没有使用那个家伙@TheTanic
  • 你能在你的代码中标记你想清除缓冲区的地方吗?
  • 我想在communication_ok=true标志我编辑后清除它请检查它

标签: android bytebuffer


【解决方案1】:

为什么要擦除数据?是局部变量吗?如果它是局部变量,它将被 GC 清除,否则(在成员变量的情况下)只需将 null 分配给缓冲区。您可以通过将 0(使用 arraycopy)复制到 bytebuffer 来执行 memset,但这将是毫无意义的代码。

【讨论】:

    【解决方案2】:

    This might be enough. 但是如果你想完全擦除数据,你可以Look here,虽然它很脏但简单易读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 2020-03-16
      相关资源
      最近更新 更多