troubleshooting Question

Truncate a byte array?

Avatar of adg
adg asked on
Java
6 Comments2 Solutions5869 ViewsLast Modified:
I need to truncate a byte array. For example the array has 100 elements but only the first 3 are used.  So I wrote this method to truncate the array.  The copy is working correctly (b2 looks good).  But when the calling method gets the array it only has one element in it.

    // return new array by truncating passed array.
    private static byte[] Truncate(byte[] b1, int nextAvailable)
    {
        byte[] b2 = new byte[nextAvailable];
        System.arraycopy(b1, 0, b2, 0, nextAvailable);
        return b2; // b2 looks ok here
    }

I am calling like this:

byte[] b = Truncate(result, nextAvailable); // b only contains element zero here.

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros