Appending byte[] to another byte[] array

B

Bharat Bhushan

Hi,

I need to generate random bytes for x number of times and keep appending it
to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.


Many Thanks,

- Bharat.
 
B

Bernhard Messerer

I wouldn't resize the array every time but
*) If you generate less than e.g. 1000 (whatever...) bytes use a static
array of that size (i.e. if you know you won't generate more than XY bytes)
*) If you generate really large amounts (and don't know how many) then use
an ArrayList which holds "chunks" of say 1000 bytes (new byte[1024]);
Finally, concatenate them by creating a large byte array (new
byte[(list.size())*1000+sizeOfLastArray]) and copy all arrays into it (by
using System.arraycopy()... lots faster than doing it by for() loop!)

regards,

Messi
 
B

Bharat Bhushan

Hi Messi,

I am only generating 28 bits in total. These are generated in parts and need
to put together for use somewhere else.

Have just tried System.arraycopy(interval, 0, genoValues, genoValues.length,
interval.length); where interval is the source array and genoValues is the
bigger array (28 bit size) and it gave me a ArrayOutOfBounds Exception.
probably worth writing the for loop...won't take long anyway.

Regards,

- Bharat.


Bernhard Messerer said:
I wouldn't resize the array every time but
*) If you generate less than e.g. 1000 (whatever...) bytes use a static
array of that size (i.e. if you know you won't generate more than XY bytes)
*) If you generate really large amounts (and don't know how many) then use
an ArrayList which holds "chunks" of say 1000 bytes (new byte[1024]);
Finally, concatenate them by creating a large byte array (new
byte[(list.size())*1000+sizeOfLastArray]) and copy all arrays into it (by
using System.arraycopy()... lots faster than doing it by for() loop!)

regards,

Messi

Bharat Bhushan said:
Hi,

I need to generate random bytes for x number of times and keep appending it
to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.


Many Thanks,

- Bharat.
 
B

Bharat Bhushan

Thanks Lothar.

comp.lang.java does exist. Infact I am subscribed to it...and I saw my post
in that newsgroup too.


Regards,

- Bharat.



Lothar Kimmeringer said:
I need to generate random bytes for x number of times and keep appending it
to a bigger byte[] array. How can I do this ?

System.arraycopy

BTW: comp.lang.java doesn't exist


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
L

Lothar Kimmeringer

Have just tried System.arraycopy(interval, 0, genoValues, genoValues.length,
^^^^^^^^^^^^^^^
That means that the starting point in the destination-array will
be at the end of the array, leading to the Exception.

I think there should be something like interval.length * i
interval.length);


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
B

Bharat Bhushan

Hi Lothar,

Well, I need to append the interval to the end of the array thats why I have
used genoValues.length in the destination position. I didn't understand when
you said "it should be something like interval.length * i". What is this "i"
?

Regards,

- Bharat.
 
G

Gordon Beaton

I need to generate random bytes for x number of times and keep
appending it to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.

An alternative nobody else has suggested yet: write your bytes to a
ByteArrayOutputStream as you generate them. Get the array when you're
done.

/gordon
 
L

Lothar Kimmeringer

On Tue, 5 Aug 2003 12:47:59 +0100, Bharat Bhushan wrote:

[correcting quoting]
Well, I need to append the interval to the end of the array thats why I have
used genoValues.length in the destination position.

An array is not growing dynamically, so you should instantiate
the destination-array with the length you will have at the end
of the value-getting (28 bytes AFAIR).
I didn't understand when
you said "it should be something like interval.length * i". What is this "i"
?

Looked again to the original post. It's lctr in your case.

BTW: Try to work on your posting- and quoting-sytle. Half of
the time for this posting I was correcting things to
increase readability.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
B

Bernhard Messerer

WHoop, what a discussion thread for such an easy "topic" ;-)

Sorry, I no more understand the whole thing...
You have a for() loop where you call makeRandomGeneBits()... this methods
returns what... a byte array, right? Do you mean this is 28 bit (4 bytes)
long?
However, I suggest instantiating a static byte array (provided the argument
"length" is the length of the byte array returned by makeRandomGeneBits()):

byte[] myBytes=new byte[main.Main.NoOfattributes()*length];
byte[] buf;

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr++) //++ seems
better here
{

// This line below generates the random bits for a given length
buf = RandomFunctions.makeRandomGeneBits(length);
System.arraycopy(buf, 0, myBytes, lctr*length, length);
}

regards,

Messi


Bharat Bhushan said:
Hi Messi,

I am only generating 28 bits in total. These are generated in parts and need
to put together for use somewhere else.

Have just tried System.arraycopy(interval, 0, genoValues, genoValues.length,
interval.length); where interval is the source array and genoValues is the
bigger array (28 bit size) and it gave me a ArrayOutOfBounds Exception.
probably worth writing the for loop...won't take long anyway.

Regards,

- Bharat.


Bernhard Messerer said:
I wouldn't resize the array every time but
*) If you generate less than e.g. 1000 (whatever...) bytes use a static
array of that size (i.e. if you know you won't generate more than XY bytes)
*) If you generate really large amounts (and don't know how many) then use
an ArrayList which holds "chunks" of say 1000 bytes (new byte[1024]);
Finally, concatenate them by creating a large byte array (new
byte[(list.size())*1000+sizeOfLastArray]) and copy all arrays into it (by
using System.arraycopy()... lots faster than doing it by for() loop!)

regards,

Messi

Bharat Bhushan said:
Hi,

I need to generate random bytes for x number of times and keep
appending
it
to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.


Many Thanks,

- Bharat.
 
M

Mr. J M Court

Bharat Bhushan said:
Hi,

I need to generate random bytes for x number of times and keep appending it
to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.


Many Thanks,

- Bharat.

byte[] intervals = ...

byte[] biggerByteArray = ...

byte[] temp = new byte[intervals.length + biggerByteArray.length];

for(int i=0; i< intervals.length; i++) temp = intervals;
for(int i=0; i< biggerByteArray.length; i++) temp[i + intervals.length] =
biggerByteArray;

biggerByteArray = temp;

...but if you are doing this lots of times it is desperately inefficient.
Much better to copy into a byte[][] and then "flatten" to byte[] once you
know the size.

(i didnt bother compiling this so it may not work).

John.
 
R

Rory Graves

Mr. J M Court said:
Hi,

I need to generate random bytes for x number of times and keep appending
it

to a bigger byte[] array. How can I do this ?

for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){

// This line below generates the random bits for a given length
byte[] intervals = RandomFunctions.makeRandomGeneBits(length);

biggerByteArray[] = biggerByteArray + intervals ; // this is the
idea...obviously this statement doesn't work for byte[]
}

Any help, pointers on web are appreciated.


Many Thanks,

- Bharat.


byte[] intervals = ...

byte[] biggerByteArray = ...

byte[] temp = new byte[intervals.length + biggerByteArray.length];

It would be better to use a System.arrayCopy for each of the arrays here:

System.arraycopy(intervals,0,temp,0, intervals.length);
System.arraycopy(biggerByteArray,0,temp,intervals.length,
biggerByteArray.length);

biggerByteArray = temp;

The array copy does the same job as the for loops suggested, but is more
effecient.

Cheers

Rory

for(int i=0; i< intervals.length; i++) temp = intervals;
for(int i=0; i< biggerByteArray.length; i++) temp[i + intervals.length] =
biggerByteArray;

biggerByteArray = temp;

..but if you are doing this lots of times it is desperately inefficient.
Much better to copy into a byte[][] and then "flatten" to byte[] once you
know the size.

(i didnt bother compiling this so it may not work).

John.
 
T

Thomas Weidenfeller

Bharat Bhushan said:
comp.lang.java does exist. Infact I am subscribed to it...and I saw my post
in that newsgroup too.

It is a zombie. It has been splitted a long time ago into the separate
comp.lang.java.* groups. There are still servers that carry it, but it
is not an official comp.lang newsgroup any more.

The fact that it is on your news server only means your server admin
didn't clean it up.

/Thomas
 
J

Jon A. Cruz

Bharat said:
Thanks Lothar.

comp.lang.java does exist. Infact I am subscribed to it...and I saw my post
in that newsgroup too.

It doesn't exist as an official newsgroup. If the service provider you
go through is providing it, then they are just doing their own thing.
And any other service providers who follow the propery way will drop any
posts there on the floor.

So you're very likely to get the equivalent of talking to yourself
there. And a good chance that the majority of the newsgroup audience out
there never sees it.



http://groups.google.com/[email protected]

http://groups.google.com/[email protected]

http://groups.google.com/[email protected]
 
C

Chris Smith

Bharat said:
Thanks Lothar.

comp.lang.java does exist. Infact I am subscribed to it...and I saw my post
in that newsgroup too.

It's a rogue group, still carried by news servers whose admins are dull
enough not to realize that it's been obsolete since 1996. If you use a
server run by such a dull admin (as I do), you will still see it and be
able to post to it, but only other people with dull news admins will see
your post.

For a complete list of current valid newsgroups in the Big 8 hierarchies
(such as comp.*), check out the periodic posting to
news.announce.newgroups entitled "List of Big Eight Newsgroups". You
can find the most recent version at the following URL:

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bharat said:
Hi,

I need to generate random bytes for x number of times and keep
appending it to a bigger byte[] array. How can I do this ?
[snip]

Hi,
I don't know about efficiency (John suggested using a byte[][] and
"flattening" it at the end, which is probably fairly efficient), but
for convenience, you could just create a ByteArrayOutputStream, then
write each byte[] into it and convert it using toByteArray() at the
end. You have to catch IOExceptions, but, AFAIK, they're pretty much
"can't happen" with ByteArrayOutputStream.
- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/L4H4wxczzJRavJYRAlACAJ4wppAjjtmnyRkCRlxx2Is14uFWiACgoBui
JguxSEhknuaCjazr4puzxEo=
=y8WP
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top