Appending byte[] to another byte[] array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bharat Bhushan

    Appending byte[] to another byte[] array

    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.NoOfa ttributes(); lctr=lctr+1){

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

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

    Any help, pointers on web are appreciated.


    Many Thanks,

    - Bharat.


  • Mr. J M Court

    #2
    Re: Appending byte[] to another byte[] array


    "Bharat Bhushan" <bharatb@talk21 .com> wrote in message
    news:xlMXa.3629 $dL4.183@news-binary.blueyond er.co.uk...[color=blue]
    > Hi,
    >
    > I need to generate random bytes for x number of times and keep appending[/color]
    it[color=blue]
    > to a bigger byte[] array. How can I do this ?
    >
    > for (int lctr=0; lctr < main.Main.NoOfa ttributes(); lctr=lctr+1){
    >
    > // This line below generates the random bits for a given length
    > byte[] intervals = RandomFunctions .makeRandomGene Bits(length);
    >
    > biggerByteArray[] = biggerByteArray + intervals ; // this is the
    > idea...obviousl y this statement doesn't work for byte[]
    > }
    >
    > Any help, pointers on web are appreciated.
    >
    >
    > Many Thanks,
    >
    > - Bharat.
    >
    >[/color]

    byte[] intervals = ...

    byte[] biggerByteArray = ...

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

    for(int i=0; i< intervals.lengt h; i++) temp[i] = intervals[i];
    for(int i=0; i< biggerByteArray .length; i++) temp[i + intervals.lengt h] =
    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.



    Comment

    • Rory Graves

      #3
      Re: Appending byte[] to another byte[] array

      Mr. J M Court wrote:
      [color=blue]
      > "Bharat Bhushan" <bharatb@talk21 .com> wrote in message
      > news:xlMXa.3629 $dL4.183@news-binary.blueyond er.co.uk...
      >[color=green]
      >>Hi,
      >>
      >>I need to generate random bytes for x number of times and keep appending[/color]
      >
      > it
      >[color=green]
      >>to a bigger byte[] array. How can I do this ?
      >>
      >>for (int lctr=0; lctr < main.Main.NoOfa ttributes(); lctr=lctr+1){
      >>
      >> // This line below generates the random bits for a given length
      >> byte[] intervals = RandomFunctions .makeRandomGene Bits(length);
      >>
      >> biggerByteArray[] = biggerByteArray + intervals ; // this is the
      >>idea...obviou sly this statement doesn't work for byte[]
      >>}
      >>
      >>Any help, pointers on web are appreciated.
      >>
      >>
      >>Many Thanks,
      >>
      >>- Bharat.
      >>
      >>[/color]
      >
      >
      > byte[] intervals = ...
      >
      > byte[] biggerByteArray = ...
      >
      > byte[] temp = new byte[intervals.lengt h + biggerByteArray .length];
      >[/color]

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

      System.arraycop y(intervals,0,t emp,0, intervals.lengt h);
      System.arraycop y(biggerByteArr ay,0,temp,inter vals.length,
      biggerByteArray .length);

      biggerByteArray = temp;

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

      Cheers

      Rory

      [color=blue]
      > for(int i=0; i< intervals.lengt h; i++) temp[i] = intervals[i];
      > for(int i=0; i< biggerByteArray .length; i++) temp[i + intervals.lengt h] =
      > 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.
      >
      >
      >[/color]



      Comment

      • Chris

        #4
        Re: Appending byte[] to another byte[] array

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

        Bharat Bhushan wrote:
        [color=blue]
        > 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 ?[/color]
        [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 ByteArrayOutput Stream, 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 ByteArrayOutput Stream.
        - --
        Chris
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.2 (GNU/Linux)

        iD8DBQE/L4H4wxczzJRavJY RAlACAJ4wppAjjt mnyRkCRlxx2Is14 uFWiACgoBui
        JguxSEhknuaCjaz r4puzxEo=
        =y8WP
        -----END PGP SIGNATURE-----

        Comment

        • Roedy Green

          #5
          Re: Appending byte[] to another byte[] array

          On Tue, 5 Aug 2003 12:15:53 +0100, "Bharat Bhushan"
          <bharatb@talk21 .com> wrote or quoted :
          [color=blue]
          >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 ?[/color]

          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.

          Comment

          Working...