Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting byte[] buffer to zeros?

In the following code for sockets, I declare a byte buffer and listen for client messages in a loop:

// Create some buffer
byte[] buffer = new byte[512];

while (true)
{
    // Listen for messages from Client

    int length = socket.getInputStream().read(buffer);

    if (length != -1)
        System.out.println(new String(buffer));
    else
        break;

    // Reset buffer here to zeros?
}

Each time I'd require to reset the contents of buffer to receive a new message. What's the proper way to do this?

like image 274
user963241 Avatar asked Jun 09 '17 10:06

user963241


1 Answers

Agree with dasblinkenlight answers, but to answer the question itself (how to reset an array), you can use Arrays.fill to set a value to every cell

byte[] array = new byte[10];
Arrays.fill(array, (byte)0);
like image 64
AxelH Avatar answered Oct 17 '22 09:10

AxelH