• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is wrong with my nio code?  RSS feed

 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try understand how work with NIO.

My java code below:



How I understand my code:

Create Channel and ByteBuffer;

Line 1: Display current Buffer's status:

[pos=0 lim=1024 cap=1024]
- It is OK.

Add data to Buffer; We added String "Request". String length - 7. So, Buffer's position must contains value -7.

Line 2: Display again current Buffer's status:

[pos=7 lim=1024 cap=1024]
- It is OK.

On next step, I want write data to Channel (so, when I write to Channel, I read from Buffer);

Line 3: Before write to Channel (read from Buffer) run method flip();

Line 4: Check again Buffer's current status:

[pos=0 lim=7 cap=1024]
- It is OK. (method flip() - set position value to 0, limit value set to position value before execute flip method - 7);

Line 5: Write data to Channel. Method write() wrote first 7 Buffer's elements, because pos = 0 and limit = 7.

Line 6: Check current Buffer's status:
[pos=7 lim=7 cap=1024]
- It is OK, after execute method write(), changed position.

Line 7: Before read data to Buffer - we must clear Buffer. Lets do it!

Line 8: Check Current Buffer's status:
[pos=0 lim=1024 cap=1024]
- It is OK, I have clear Buffer.

Line 9: Lets go read some data from Channel to Buffer:

.... but I don't see any results... my program doesn't show me any results... why? What happen? I cant understand this point

Please, help me understand, where my mistake?

Thanks.





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!