0

Can anybody please tell me what are those possible purposes of allocating zero-length buffer?

ByteBuffer.allocate(0); // no IllegalArgumentException

Why the one who designed the API did this?

Thanks for comments and answers.

I hope there will be an update like this. :)

public abstract class ByteBuffer
    extends Buffer
    implements Comparable<ByteBuffer> {

    public static final ByteBuffer VOID = allocate(0);
}
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

2 Answers2

6

If you have a method that must return a ByteBuffer and returning null is inappropriate for whatever reason, but you have no data to return, then returning a zero-length ByteBuffer would satisfy those conditions.

Jason C
  • 38,729
  • 14
  • 126
  • 182
2

This can be used to implement Null Object design pattern http://en.wikipedia.org/wiki/Null_Object_pattern, similar to Collections.emptyList and others, creates an immutable object that can be reused.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275