Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Appending byte[] to another byte[] array

Reply
Thread Tools

Appending byte[] to another byte[] array

 
 
Bharat Bhushan
Guest
Posts: n/a
 
      08-05-2003
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.


 
Reply With Quote
 
 
 
 
Bernhard Messerer
Guest
Posts: n/a
 
      08-05-2003
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" <(E-Mail Removed)> schrieb im Newsbeitrag
newslMXa.3629$(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
 
 
 
Bharat Bhushan
Guest
Posts: n/a
 
      08-05-2003
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" <(E-Mail Removed)> wrote in message
news:bgo46t$nl9$(E-Mail Removed)...
> 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" <(E-Mail Removed)> schrieb im Newsbeitrag
> newslMXa.3629$(E-Mail Removed)...
> > 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.
> >
> >

>
>



 
Reply With Quote
 
Bharat Bhushan
Guest
Posts: n/a
 
      08-05-2003
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Tue, 5 Aug 2003 12:15:53 +0100, Bharat Bhushan wrote:
>
> > 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
> PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
>
> Always remember: The answer is forty-two, there can only be wrong
> questions!



 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      08-05-2003
On Tue, 5 Aug 2003 12:39:23 +0100, Bharat Bhushan wrote:

> 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 Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Bharat Bhushan
Guest
Posts: n/a
 
      08-05-2003
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.


"Lothar Kimmeringer" <(E-Mail Removed)> wrote in message
news:1unhjiar1kxze$(E-Mail Removed)...
> On Tue, 5 Aug 2003 12:39:23 +0100, Bharat Bhushan wrote:
>
> > 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 Removed)
> PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
>
> Always remember: The answer is forty-two, there can only be wrong
> questions!



 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      08-05-2003
On Tue, 5 Aug 2003 12:15:53 +0100, Bharat Bhushan wrote:
> 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

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      08-05-2003
On Tue, 5 Aug 2003 12:47:59 +0100, Bharat Bhushan wrote:

[correcting quoting]

> "Lothar Kimmeringer" <(E-Mail Removed)> wrote in message
> news:1unhjiar1kxze$(E-Mail Removed)...
>> On Tue, 5 Aug 2003 12:39:23 +0100, Bharat Bhushan wrote:
>>
>>> 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.

> 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 think there should be something like interval.length * i


> 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 Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Bernhard Messerer
Guest
Posts: n/a
 
      08-05-2003
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" <(E-Mail Removed)> schrieb im Newsbeitrag
news:zHMXa.3719$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:bgo46t$nl9$(E-Mail Removed)...
> > 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" <(E-Mail Removed)> schrieb im Newsbeitrag
> > newslMXa.3629$(E-Mail Removed)...
> > > 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.
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Mr. J M Court
Guest
Posts: n/a
 
      08-05-2003

"Bharat Bhushan" <(E-Mail Removed)> wrote in message
newslMXa.3629$(E-Mail Removed)...
> 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[i] = intervals[i];
for(int i=0; i< biggerByteArray.length; i++) temp[i + intervals.length] =
biggerByteArray[i];

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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
appending an array to a frozen array James French Ruby 4 03-25-2009 04:50 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
appending one vector after another. toton C++ 5 10-05-2006 10:04 PM
appending to the end of array azzamqazi@gmail.com Java 2 10-02-2005 10:49 PM
appending an array to a list in BOOST / Python GujuBoy Python 0 06-09-2005 05:06 AM



Advertisments