分配零容量ByteBuffer [英] Allocating zero capacity ByteBuffer

查看:208
本文介绍了分配零容量ByteBuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我分配零长度缓冲区的可能目的是什么?

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

ByteBuffer.allocate(0); // no IllegalArgumentException

为什么设计API的人会这样做?

Why the one who designed the API did this?

感谢您的评论和解答。

我希望会有这样的更新。 :)

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);
}


推荐答案

如果您有方法必须返回 ByteBuffer 并返回 null 不合适的原因,但是你没有数据要返回,然后返回一个零长度 ByteBuffer 将满足这些条件。

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.

这篇关于分配零容量ByteBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆