2
ArrayList<Byte> bytes = new ArrayList<Byte>();
try {
    int data = putObjectRequest.getInputStream().read();
    bytes.add((byte) data);
    while (data != -1) {
        data = putObjectRequest.getInputStream().read();
        bytes.add((byte)data);
    }
} catch (IOException e) {
    e.printStackTrace();
}

I want to convert this to byte[]. is this this the only way?

byte[] byteArray = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); i++) {
   byteArray[i] = bytes.get(i);
}

5 Answers 5

4

I'd suggest using a ByteArrayOutputStream instead of an ArrayList<Byte> to collect your input:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    int data = putObjectRequest.getInputStream().read();
    while (data != -1) {
        bos.write(data);
        data = putObjectRequest.getInputStream().read();
    }
} catch (IOException e) {
    e.printStackTrace();
}
byte[] byteArray = bos.toByteArray();

This avoids the horrible overhead of boxing and unboxing every byte. (I also fixed a small bug in your original code where you would write -1 if putObjectRequest was empty.)

Sign up to request clarification or add additional context in comments.

Comments

2
byte[] byteArray = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); i++) {
   byteArray[i] = bytes.get(i);
}

Yes, this is the only way.

byte[] byteArray = bytes.toArray(new byte[bytes.size()]);

Using toArray() as proposed in another answer does not work because the method can't automatically convert the wrapper type Byte to the primitive byte.

2 Comments

With an ArrayList implementation this is fine but it will run in polynomial time on any linked list implementation. Writing to object array and then iterating through with static casts is safer because guarantees linear time.
@SamGrondahl - OP is using ArrayList, which is not a linked list implementation.
2

Using ArrayUtils in Apache Commons:

byte[] byteArray = ArrayUtils.toPrimitive(bytes.toArray(new Byte[bytes.size()]));

1 Comment

Which uses the code in main-- answer, I'll bet. You'll have to decide whether you prefer three lines of code or one line of code and a JAR dependency.
1

Nope. Easier:

Byte[] byteArray = bytes.toArray(new Byte[bytes.size()]);

And if you really want primitives:

byte[] primitives = new byte[byteArray.length]
for (int i = 0; i < byteArray.length; i++) {
  primitives [i] = (byte)byteArray[i];
}

This guarantees you linear time complexity for both linked list and resizing array implementations.

It's been supported since 5.0:

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#toArray(T[])

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html

5 Comments

true, this doesn't even compiles
The OP wants a byte[], not a Byte[]... I guess?
Yes, I wanted byte[] not Byte[] as mentioned in OP
it is not necessary to pass array size as an argument.
So instead of simply iterating over the original ArrayList<Byte> as OP is doing, you propose converting the ArrayList<Byte> to a Byte[] and then iterating. How is this an improvement? (OP is using ArrayList, not some random linked list implementation.)
1

You could always use something like TByteList from trove4j, instead of your ArrayList<Byte>. Your algorithm would then become:

TByteList bytes = new TByteArrayList();
try {
    int data = putObjectRequest.getInputStream().read();
    bytes.add((byte) data);
    while (data != -1) {
        data = putObjectRequest.getInputStream().read();
        bytes.add((byte)data);
    }
} catch (IOException e) {
    e.printStackTrace();
}

byte[] byteArray = bytes.toArray();

2 Comments

This won't quite work, since TByteList is an interface. It would need to be TByteList bytes = new TByteArrayList();. Then it wouldn't be much different from using the core ByteArrayOutputStream class (which would avoid a jar dependency).
@TedHopp: Whoops thanks for the interface hint. You're right, using BAOS is probably optimal in this case. I was kind of throwing this in as an option to the OP's question "is this this the only way?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.