Your SlideShare is downloading. ×
Java NIO Buffer basics
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java NIO Buffer basics

173
views

Published on

Hello and welcome to NIO, in this video we will cover the basics of buffer before that let’s focus why we need NIO. NIO was created to allow Java programmers to implement high-speed I/O without having …

Hello and welcome to NIO, in this video we will cover the basics of buffer before that let’s focus why we need NIO. NIO was created to allow Java programmers to implement high-speed I/O without having to write custom native code. NIO moves the most time-consuming I/O activities (namely, filling and draining buffers) back into the operating system, thus allowing for a great increase in speed.

www.youtube.com/watch?v=rTa68v4Gfrk

Published in: Education, Technology, Design

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
173
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Hello and welcome to NIO, in this video we will cover the basics of buffer before that let’s focus why we need NIO. NIO was created to allow Java programmers to implement high-speed I/O without having to write custom native code. NIO moves the most time-consuming I/O activities (namely, filling and draining buffers) back into the operating system, thus allowing for a great increase in speed.
  • Before moving to Buffer Basics recap the following concept like Stream IO vs Block IO. Java NIO and IO is that IO is stream oriented, where NIO is buffer oriented. So, what does that mean? Java IO's various streams are blocking. That means, that when a thread invokes a read() or write(), that thread is blocked until there is some data to read, or the data is fully written. The thread can do nothing else in the meantime. Java NIO's non-blocking mode enables a thread to request reading data from a channel, and only get what is currently available, or nothing at all, if no data is currently available. Rather than remain blocked until data becomes available for reading, the thread can go on with something else
  • What is a Buffer ?. A buffer is an array of primitive data elements wrapped inside an object. There are four attributes all buffer.CapacityThe capacity of a buffer specifies the maximum amount of data that can be stored therein. Limit The first element of the buffer that should not be read or written.PositionLikewise, when you are writing to a channel, you get the data from a buffer. The position value keeps track of how much you have gotten from the buffer. More precisely, it specifies from which array element the next byte will come Mark Calling mark( ) sets mark = position.
  • Kinds of buffers The most commonly used kind of buffer is the ByteBuffer. Each of the Buffer classes is an instance of the Buffer interface. Types of Buffers are ByteBuffer also known as Direct Buffer, other fall in category of nondirect buffer like CharBuffer,ShortBuffer,IntBuffer,LongBuffer,FloatBuffer,DoubleBuffer
  • Byte Buffer Visual Representation. Figure shows a logical view of a newly created ByteBuffer with a capacity of 10. The position is set to 0, the capacity and Limit are set to 10.Mark is undefined, capacity is fixed, position will change as buffer moves.
  • In this example we Just fill the byte buffer with byte values representing the ASCII character sequence Hello into a ByteBuffer object named buffer. As we can see that MARK remains undefined the Limit and capacity remains unchanged the POSITION only moves, every PUT of byte value increment the position by one, Now the question is what trigger the LIMIT and MARK attribute to change, we will get to know when we see the buffer flipping.
  • What is buffer flipping,when are ready to write our data to an output channel. Before we can do this, we must call the flip() method. This method does two crucial things: It sets the limit to the current position. It sets the position to 0. Why we need to perform flip() before sending to the channel, the reason behind this is the limit indicates the end of the active buffer content, and position is set back to 0 means that from position to limit the buffer data need to accessJAVA API buffer.flip( );The rewind( ) method is similar to flip( ) but does not affect the limit.
  • Access data from buffer,there are two methods hasRemaining() and remaining() that will tell you if you've reached the buffer's limit when draining. The boolean method hasRemaining() Tells whether there are any elements between the current position and the limit, and true if, and only if, there is at least one element remaining in this buffer.Theint method remaining() returns the number of elements between the current position and the limit.
  • Buffer fourth important attribute is called mark() and it’s remain undefined until it set. In this example we have mark the position 2. if we reset now then position will set to the mark. Mark can be used as abstraction on buffer as it’s gives the control how many bytes of data need to be sent over the channel.
  • In this example we will see how the buffers are compared. Buffer usually implements the comparable interfaces which gives equals( ) method for testing the equality of two buffers and a compareTo( ) method for comparing buffersThe example shows buffer1 and buffer2 are equals as they have same number of remaining elements and also satisfied the criteria that says two buffers are equals.
  • We talked lot about the Buffers, now let us understand how we can create it there are two methods two perform this the allocate and wrap methods. Before you can read or write, you must have a buffer. To create a buffer, you must allocate it. We allocate a buffer using the static method of allocate().You can also turn an existing array into a buffer, as shown here: byte array[] = new byte[1024]; ByteBuffer buffer = ByteBuffer.wrap( array ); In this case, you've used the wrap() method to wrap a buffer around an array.
  • Duplicating a buffer means Creating a new byte buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent. The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
  • Buffer slicing means creating a kind of sub-buffer from an existing buffer. That is, it.The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent
  • ByteBuffer is biased over network and also knows as Direct buffer, this is special buffer and having few characteristics.A direct buffer is one whose memory is allocated in a special way to increase I/O speed.Thanks for watching the video, do watch the next video on ByteBuffer explained and subscribe to the channel.
  • Transcript

    • 1. NIO Why NIO ?
    • 2. Streams versus blocks A stream-oriented I/O system deals with data one byte at a time. An input stream produces one byte of data, and an output stream consumes one byte of data . A block-oriented I/O system deals with data in blocks http://www.youtube.com/zarigatongy
    • 3. Buffer Basics • A buffer is an array of primitive data elements wrapped inside an object. • Attributes – Capacity – Limit – Position – Mark 0 <= mark <= position <= limit <= capacity http://www.youtube.com/zarigatongy
    • 4. Buffer Types • ByteBuffer  Direct Buffer • CharBuffer • ShortBuffer • IntBuffer • LongBuffer • FloatBuffer • DoubleBuffer http://www.youtube.com/zarigatongy
    • 5. ByteBuffer Visual Representation 0 1 X 2 3 4 5 6 0 7 8 10 MARK POSITION Limit let's assume that our buffer has a total capacity of Ten bytes http://www.youtube.com/zarigatongy 9 10 CAPACITY
    • 6. Data in Buffer buffer.put((byte)'H').put((byte)'e').put((byte)'l'). put((byte)'l').put((byte) ’o’) 0 1 2 h 48 e 65 l 6c X MARK 3 l 6c 4 5 6 7 8 9 0 6f 5 10 POSITION Limit http://www.youtube.com/zarigatongy 10 CAPACITY
    • 7. Buffer Flipping buffer.flip() 0 1 2 h 48 e 65 l 6c X MARK 3 l 6c 4 5 6 7 8 9 o 6f 0 6 POSITION Limit http://www.youtube.com/zarigatongy 10 CAPACITY
    • 8. Buffer Draining • hasRemaining( ) for (int i = 0; buffer.hasRemaining( ), i++) { buffer.get( ); } • remaining( ) int count = buffer.remaining( ); for (int i = 0; i < count, i++) { buffer.get( ); } http://www.youtube.com/zarigatongy
    • 9. Buffer Marking buffer.position(2).mark( ).position(4); 0 1 2 3 4 h 48 r 65 l 6c l 6c o 6f 2 MARK 5 6 4 7 6 POSITION Limit rewind( ), clear( ), and flip( ) always discard the mark clear( ) method makes a buffer empty reset( ) returns the position to a previously set mark http://www.youtube.com/zarigatongy 8 9 10 CAPACITY
    • 10. Buffer Comparison • Two buffers are considered to be equal – Both objects are the same type – Both buffers have the same number of remaining elements POSITION LIMIT h 68 e 65 l 6c l 6c t 74 i 69 l 6c Buffer 1 l 6c POSITION o 6f Buffer 2 LIMIT EQUAL Buffer Demo http://www.youtube.com/zarigatongy
    • 11. Creating Buffer • Allocation – ByteBuffer buffer = ByteBuffer.allocate( 1024 ); – CharBuffer charBuffer = CharBuffer.allocate (100); • Wrap – byte array[] = new byte[1024]; ByteBuffer buffer = ByteBuffer.wrap( array ); – char [] myArray = new char [100]; CharBuffer charbuffer = CharBuffer.wrap (myArray); – CharBuffer charbuffer = CharBuffer.wrap (myArray, 12, 90); http://www.youtube.com/zarigatongy
    • 12. Duplicating Buffer • returns a new byte buffer that shares the old buffer's content. Changes to the old buffer's content will be visible in the new buffer, and vice versa. • The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only • CharBuffer buffer = CharBuffer.allocate (8); buffer.position (3).limit (6).mark( ).position (5); CharBuffer dupeBuffer = buffer.duplicate( ); http://www.youtube.com/zarigatongy
    • 13. Buffer Slicing • The slice() method creates a kind of sub-buffer from an existing buffer • The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent. • The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer, and its mark will be undefined. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only. http://www.youtube.com/zarigatongy
    • 14. Direct Buffers • Direct Buffer– The ByteBuffer – A direct buffer is one whose memory is allocated in a special way to increase I/O speed – Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations. http://www.youtube.com/zarigatongy

    ×