Re: how to extend a byte[] array with a null byte?

Discussion in 'Java' started by Tom McGlynn, Apr 17, 2008.

  1. Tom McGlynn

    Tom McGlynn Guest

    On Apr 17, 2:09 pm, "Peter Duniho" <>
    > I was surprised to not be able to find a helper method in the Array or
    > Arrays class that would do the copy. It's possible one exists elsewhere
    > and I just haven't run across it yet. That would be nicer (and possibly
    > perform better) than the for() loop above.

    You're probably looking for the System.arraycopy method.

    In this case...
    System.arraycopy(rgbString, 0, rbgDatabase, 0,

    Tom McGlynn
    Tom McGlynn, Apr 17, 2008
    1. Advertisements

  2. In article <>,
    "Peter Duniho" <> wrote:

    > On Thu, 17 Apr 2008 20:57:33 -0700, Lew <> wrote:
    > > [...]
    > > You use System.arraycopy(), as others have suggested, when you're using
    > > a target array that you need to allocate outside the call.

    > I understand that. But part of a well-designed API is discoverability. A
    > natural place to look for that functionality is in the classes involved,
    > as opposed to a general purpose utility class (especially considering that
    > that utility class consists mostly of things related to the run-time
    > environment, rather than management of data within a Java program).
    > The arraycopy() method, to me, seems like an anomaly in the System class,
    > whereas it would seem quite at home as a static method in either the Array
    > or Arrays class.
    > YMMV.
    > Pete

    The System and Runtime classes are an odd collection of things that
    needed a home in the Java 1.0 days. That was back when Java was supposed
    to be a compact, super efficient language for browsers and embedded
    devices. (Yes, the flaws in that logic were visible back then too.)

    Block Google's spam and enjoy Usenet again.
    Reply with Google and I won't hear from you.
    Kevin McMurtrie, Apr 18, 2008
    1. Advertisements

  3. Lew <> wrote:
    > Besides, Arrays.copyOf() is far more idiomatic and something I'd almost
    > certainly use 99.9% of the time in preference to System.arraycopy() (which
    > doesn't even follow Sun's own naming conventions).

    The only usecase (that I can think of right now), where copyOf doesn't
    do better than arraycopy, is when you want all the elements of two or more
    separate arrays concatenated in one new big array. For the first array,
    copyOf (to the final length) is fine, but then?
    I do not claim that this was a particularly common usecase, but it seems
    plausible to me that it may come up casually.
    Andreas Leitgeb, Apr 18, 2008
    1. Advertisements

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
    Oct 8, 2004
  2. Replies:
    Mike Schilling
    Mar 29, 2006
  3. Tom McGlynn
    Mark Space
    Apr 19, 2008
  4. Patricia Shanahan
    Patricia Shanahan
    Apr 17, 2008
  5. aneuryzma
    Jim Langston
    Jun 16, 2008

Share This Page