0

I am doing an application that consists of a sending an receiving data from android to the micro controller here in the receiving side that was from micro-controller to the android i was using a byte buffer to store the data.But here i got a problem i want to receive data of multiple bytes at different stages for this i want to clear the byte buffer and re-use the same byte buffer again can any one tell me how to clear the byte buffer

This is my receiving code:

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**           


                }
             }

             }
         } 
dEePU
  • 195
  • 5
  • 18
  • please try to read the http://stackoverflow.com/questions/8462200/examples-of-forcing-freeing-of-native-memory-direct-bytebuffer-has-allocated-us amay be helpful to you. – Rajan Bhavsar Jun 16 '15 at 05:58
  • did you try: buffer.clear() ? – TheTanic Jun 16 '15 at 06:15
  • But here i am assigning byte buffer to the Received_data[i] i didn't use that dude @TheTanic – dEePU Jun 16 '15 at 06:21
  • Can you mark in your code where do you want to clear the buffer? – TheTanic Jun 16 '15 at 06:27
  • I want to clear after communication_ok=true flag i edited it please check it – dEePU Jun 16 '15 at 06:35
  • so when i am understanding you wright, you want to clear the complete buffer? If yes there should be no problem to use buffer.clear(). If you just want to clear a single byte of the buffer, i don´t think that this is possible – TheTanic Jun 16 '15 at 06:38
  • yes Dude i want to clear the complete buffer and reuse it dude @TheTanic – dEePU Jun 16 '15 at 06:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80636/discussion-between-thetanic-and-deepu). – TheTanic Jun 16 '15 at 06:44

2 Answers2

0

Why do you want to erase the data ? Is it local variable ? If it is local variable any way it will be cleared by GC otherwise(in case of member variable) just assign null to buffer. You can do memset by copying 0(using arraycopy) to bytebuffer but it will be pointless code.

Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
-2

This might be enough. BUT if you want to erase the data completely, you could Look here,though it's dirty but simple and readble.

Community
  • 1
  • 1
Remy
  • 1,053
  • 1
  • 19
  • 25