简体   繁体   中英

What's the difference between buffer(int) and buffer()?

I'm trying to send a packet to the client and i noticed that there's no error even when i don't set an initial capacity.

// data is another ByteBuf object
// With capacity
ByteBuf d = Unpooled.buffer(2 + data.array().length);
d.writeByte(identifiers[0]).writeByte(identifiers[1]).writeBytes(data.array());
// Without capacity
ByteBuf d = Unpooled.buffer();
d.writeByte(identifiers[0]).writeByte(identifiers[1]).writeBytes(data.array());

Is there any difference between these two? They both seem to be working fine.

I also wonder if i'm doing it right, i'm creating a new ByteBuf with Unpooled.buffer , write my data in it and send it to the channel using channel.write(ByteBuf) when i need to send something to the client. Am I doing it right?

From the docs:

  • buffer()

    creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

  • buffer(int initialCapacity)

    creates a new big-endian Java heap buffer with the specified capacity, which expands its capacity boundlessly on demand. The new buffer's readerIndex and writerIndex are 0.

So, buffer(int) does the same thing as buffer() but with a specified starting capacity. It will perform better if you know you need a larger capacity than the default, as it won't have to keep expanding if there's not enough capacity. edit : also, as @yelliver stated, you could save memory by specifying, which might add up depending on how many buffer objects you create (or depending on how large the buffer is).

It looks like buffer(int) also sets some indices that buffer() might not.

The difference is they have difference inititial capacity.

See the buffer() doc:

Creates a new big-endian Java heap buffer with reasonably small initial capacity , which expands its capacity boundlessly on demand.

and the buffer(int initialCapacity) doc:

Creates a new big-endian Java heap buffer with the specified capacity , which expands its capacity boundlessly on demand.

In general Java, many cases you can pass capacity or not, both are OK. If you do not pass the capacity, it will have default capacity, When the capacity is full, it's auto expanded. For example:

String[] arr = list.toArray(new String[list.size()]);
//OR
String[] arr = list.toArray(new String[0]);

List<String> list = new ArrayList<>(10);
//OR
List<String> list = new ArrayList<>();

If you let the default capacity:

  1. Pros : no need to care about capacity (obviously)
  2. Cons : if you intent to use small size which less than default capacity, it will waste of memory.

If you pass your intent capacity:

  1. Pros : save memory, you allocate exactly memory which you want.
  2. Cons : Sometimes you cannot know the exactly size from beginning, or the calculation of size is complicated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM