- ScatteringByteChannel在java.nio.channels包中,繼承了AutoCloseable, Channel,
Closeable, ReadableByteChannel介面 - 所有的實作類為:DatagramChannel, FileChannel, Pipe.SourceChannel, SocketChannel
- ScatteringByteChannel是一個通道,可以將Channel中的資料寫入Buffer陣列中,在網路協議或者檔案讀寫中非常有用,比如把資料按照segment的header指定的長度,寫入到對應的body中,
- ScatteringByteChannel有兩個方法:
- long read?(ByteBuffer[] dsts)
- long read?(ByteBuffer[] dsts, int offset, int length)
long read?(ByteBuffer[] dsts, int offset, int length) throws IOException
- dsts是被寫入的目標buffer陣列
- offset是目標陣列的index,比如dsts的長度是5,offset為3,則從第4個陣列開始寫入
- length是最大可訪問的陣列長度,
回傳讀取的byte數量,-1代表通道關閉,
long read?(ByteBuffer[] dsts) throws IOException
上述方法是呼叫了c.read(dsts, 0, dsts.length);
從第0個陣列開始寫,可以寫的陣列長度為dsts.length,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/166945.html
標籤:其他
上一篇:Java入門及詞解
