stephansun对《Java Nio》的笔记(6)

stephansun
stephansun (认真你就输了...)

读过 Java Nio

Java Nio
  • 书名: Java Nio
  • 作者: Ron Hitchens
  • 页数: 312
  • 出版社: O'Reilly Media
  • 出版年: 2002-8-15
  • 第32页
    for (int i = 0; buffer.hasRemaining(), i++) {
           myByteArray [i] = buffer.get();
    }
    int count = buffer.remaining();
    for (int i = 0; i < count, i++) {
    }
    myByteArray [i] = buffer.get();
    Buffers are not thread-safe. If you want to access a given buffer concurrently from multiple threads, you'll need to do your own synchronization (e.g., acquiring a lock on the buffer object) prior to accessing the buffer.
    引自第32页
    2013-08-14 11:25:29 回应
  • 第35页
    The effect of calling compact() is to drop the data elements already drained, preserve what hasn't been drained, and make the buffer ready to resume filling to capacity. You can use a buffer in this way as a First In First Out (FIFO) queue. More efficient algorithms certainly exist (buffer shifting is not a very efficient way to do queuing), but compacting may be a convenient way to synchronize a buffer with logical blocks of data (packets) in a stream you are reading from a socket.
    引自第35页
    2013-08-14 11:44:01 回应
  • 第51页
    If a buffer was created as a view of a ByteBuffer object (see Section 2.4.3), then the value returned by the order() method is the byte-order setting of the originating ByteBuffer at the time the view was created. The byte-order setting of the view cannot be changed after it's created and will not be affected if the original byte buffer's byte order is changed later.
    引自第51页
    2013-08-15 10:32:43 回应
  • 第55页
    View buffers can potentially be much more efficient when derived from direct byte buffers. If the byte order of the view matches the native hardware byte order, the low-level code may be able to access the data values directly rather than going through the byte-packing and -unpacking process.
    引自第55页
    2013-08-15 10:49:59 回应
  • 第110页
    Socket channels are thread-safe. Multiple threads do not need to take special steps to protect against concurrent access, but only one read and one write operation will be in progress at any given time. Keep in mind that sockets are stream-oriented, not packet-oriented. They guarantee that the bytes sent will arrive in the same order but make no promises about maintaining groupings. A sender may write 20 bytes to a socket, and the receiver gets only 3 of those bytes when invoking read(). The remaining 17 bytes may still be in transit. For this reason, it's rarely a good design choice to have multiple, noncooperating threads share the same side of a stream socket.
    引自第110页
    2013-08-20 10:34:50 回应
  • 第116页
    * eight-byte buffer is used to send/recieve. But getLong() * eight-byte buffer is used to send/receive. But getLong()
    引自第116页
    2013-08-20 12:31:44 回应