Appending byte[] to another byte[] array

Discussion in 'Java' started by Bharat Bhushan, Aug 5, 2003.

  1. 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.
    Bharat Bhushan, Aug 5, 2003
    #1
    1. Advertising

  2. 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" <> schrieb im Newsbeitrag
    news:xlMXa.3629$...
    > 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.
    >
    >
    Bernhard Messerer, Aug 5, 2003
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:bgo46t$nl9$...
    > 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" <> schrieb im Newsbeitrag
    > news:xlMXa.3629$...
    > > 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.
    > >
    > >

    >
    >
    Bharat Bhushan, Aug 5, 2003
    #3
  4. 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" <> wrote in message
    news:eek:...
    > 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:
    > PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
    >
    > Always remember: The answer is forty-two, there can only be wrong
    > questions!
    Bharat Bhushan, Aug 5, 2003
    #4
  5. 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:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

    [correcting quoting]

    > "Lothar Kimmeringer" <> wrote in message
    > news:1unhjiar1kxze$...
    >> 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:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Aug 5, 2003
    #8
  9. 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" <> schrieb im Newsbeitrag
    news:zHMXa.3719$...
    > 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" <> wrote in message
    > news:bgo46t$nl9$...
    > > 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" <> schrieb im Newsbeitrag
    > > news:xlMXa.3629$...
    > > > 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.
    > > >
    > > >

    > >
    > >

    >
    >
    Bernhard Messerer, Aug 5, 2003
    #9
  10. "Bharat Bhushan" <> wrote in message
    news:xlMXa.3629$...
    > 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.
    Mr. J M Court, Aug 5, 2003
    #10
  11. Bharat Bhushan

    Rory Graves Guest

    Mr. J M Court wrote:

    > "Bharat Bhushan" <> wrote in message
    > news:xlMXa.3629$...
    >
    >>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.
    >
    >
    >
    Rory Graves, Aug 5, 2003
    #11
  12. "Bharat Bhushan" <> writes:
    > 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
    Thomas Weidenfeller, Aug 5, 2003
    #12
  13. Bharat Bhushan

    Jon A. Cruz Guest

    Bharat Bhushan wrote:
    > 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/groups?selm=

    http://groups.google.com/groups?selm=

    http://groups.google.com/groups?selm=
    Jon A. Cruz, Aug 5, 2003
    #13
  14. Bharat Bhushan

    Chris Smith Guest

    Bharat Bhushan wrote:
    > 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:

    > http://groups.google.com/groups?selm=list-20030715150002$


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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Aug 5, 2003
    #14
  15. Bharat Bhushan

    Chris Guest

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

    Bharat Bhushan wrote:

    > 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-----
    Chris, Aug 5, 2003
    #15
  16. Bharat Bhushan

    Roedy Green Guest

    On Tue, 5 Aug 2003 12:15:53 +0100, "Bharat Bhushan"
    <> wrote or quoted :

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


    see http://mindprod.com/jgloss/random.html

    you can generate them a byte or int or long at a time and fill in your
    array.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Aug 5, 2003
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kirby
    Replies:
    3
    Views:
    630
    Kirby
    Oct 8, 2004
  2. DaBeef
    Replies:
    1
    Views:
    608
    Matt Humphrey
    Jul 21, 2006
  3. toton
    Replies:
    5
    Views:
    6,198
    Jens Theisen
    Oct 5, 2006
  4. Debajit Adhikary
    Replies:
    17
    Views:
    674
    Debajit Adhikary
    Oct 18, 2007
  5. James French

    appending an array to a frozen array

    James French, Mar 25, 2009, in forum: Ruby
    Replies:
    4
    Views:
    94
    Robert Klemme
    Mar 25, 2009
Loading...

Share This Page