[英]clear() method of ByteBuffer in java
我在其中一個教程網站上閱讀了有關DatagramChannel的內容,他們解釋了以下示例程序
String newData = "New String to write to file..."
+ System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
int bytesSent = channel.send(buf, new InetSocketAddress("example.com", 80));
他們在創建ByteBuffer類對象之后添加了buf.clear()行。 clear()用於清除緩沖區。但是最初我們不需要清除right ??。如果需要清除,那么為什么要使用??。 謝謝。(對不起,如果有語法錯誤)
也許這只是一個糟糕的教程/網站。 在您剛剛分配了新的ByteBuffer
的情況下,則不需要它。 你可以用
ByteBuffer buf = ByteBuffer.wrap(newData.getBytes());
擺脫put()
和flip()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.