Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill byte array with junk?

Tags:

I am using this:

byte[] buffer = new byte[10240]; 

As I understand this initialize the buffer array of 10kb filled with 0s.

Whats the fastest way to fill this array (or initialize it) with junk data every time?

I need to use that array like >5000 times and fill it every time with different junk data, that's why I am looking for a fast method to do it. The array size will also have to change every time.

like image 251
flyout Avatar asked Jun 06 '10 17:06

flyout


People also ask

How do you create a dummy byte array in Java?

The java. util. Random. nextBytes() method generates random bytes and provides it to the user defined byte array.

How do I combine byte arrays?

The recommended solution to concatenate two or more byte arrays is using ByteArrayOutputStream . The idea is to write bytes from each of the byte arrays to the output stream, and then call toByteArray() to get the current contents of the output stream as a byte array.

What is ByteArray?

ByteArray is an extremely powerful Class that can be used for many things related to data manipulation, including (but not limited to) saving game data online, encrypting data, compressing data, and converting a BitmapData object to a PNG or JPG file.


2 Answers

Answering 'the fastest way' is impossible without describing what the properties of your junk data have to be. Why isn't all zeroes valid junk data?

That said, this is a fast way to fill your array with meaningless numbers.

Random r = new Random(); r.NextBytes(buffer); 

You might also look at implementing your own Linear congruential generator if Random isn't fast enough for you. They're simple to implement and fast, but won't give high quality random numbers. (It's unclear to me if you need those or not.)

like image 111
Joren Avatar answered Sep 18 '22 20:09

Joren


If you are happy with the data being random, but being created form a random seed buffer, then you could do the following:

public class RandomBufferGenerator {     private readonly Random _random = new Random();     private readonly byte[] _seedBuffer;      public RandomBufferGenerator(int maxBufferSize)     {         _seedBuffer = new byte[maxBufferSize];          _random.NextBytes(_seedBuffer);     }      public byte[] GenerateBufferFromSeed(int size)     {         int randomWindow = _random.Next(0, size);          byte[] buffer = new byte[size];          Buffer.BlockCopy(_seedBuffer, randomWindow, buffer, 0, size - randomWindow);         Buffer.BlockCopy(_seedBuffer, 0, buffer, size - randomWindow, randomWindow);          return buffer;     } } 

I found it to be approx 60-70 times faster then generating a random buffer from scratch each time.

              START: From seed buffer. 00:00:00.009  END  : From seed buffer. (Items = 5,000; Per Second = 500,776.20)               START: From scratch. 00:00:00.604  END  : From scratch. (Items = 5,000; Per Second = 8,276.95)  

Update

The general idea is to create a RandomBufferGenerator once, and then use this instance to generate random buffers, e.g.:

RandomBufferGenerator generator = new RandomBufferGenerator(MaxBufferSize);  byte[] randomBuffer1 = generator.GenerateBufferFromSeed(10 * 1024); byte[] randomBuffer2 = generator.GenerateBufferFromSeed(5 * 1024); ... 
like image 39
Tim Lloyd Avatar answered Sep 18 '22 20:09

Tim Lloyd