Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:10:27+00:00 2026-06-08T10:10:27+00:00

Java code below: import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; public class Test

  • 0

Java code below:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Test {
    public static void main(String args[]) throws IOException {

        SocketChannel c = SocketChannel.open();
        c.connect(new InetSocketAddress("google.com", 80));

        ByteBuffer b = ByteBuffer.allocate(1024);
        b.put("Request".getBytes());

        System.out.println("Write: " + c.write(b));

        int i;
        while ((i = c.read(b)) != -1) {

            System.out.println("Read: " + i);
            b.clear();

        }
    }
}

Actual Result:

Write: 1017 Read: 0 Read: 1024 Read: 44

First time, method read() read 0 bytes. It is not cool.

I modified my code:

    b.put("Request".getBytes());

    System.out.println("Write: " + c.write(b));

    b.flip(); //I added this line
    int i;
    while ((i = c.read(b)) != -1) {

        System.out.println("Read: " + i);
        b.clear();

    }

Actual results:

Write: 1017 Read: 1024 Read: 44

It already looks better. Thanks flip() for this!

Next, I put to buffer String “Request”, this String has length 7, but method write()returned 1017.

What information method wrote to channel?

I am not sure, that method wrote string “Request”.

Ok, I modified my code again:

    b.put("Request".getBytes());

    b.flip(); // I added this line
    System.out.println("Write: " + c.write(b));

    b.flip();
    int i;
    while ((i = c.read(b)) != -1) {

        System.out.println("Read: " + i);
        b.clear();

    }

Actual results:

Write: 7

and code crashed…

By why? Where is my mistake?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:10:28+00:00Added an answer on June 8, 2026 at 10:10 am

    The flip method needs to be called before reading data from the buffer. The flip() method, reset the buffer’s limit to the current position and reset the buffer’s position to 0.

    So, if you have 7 byte of data in ByteBuffer, your position (starting from 0), would be 7. flip()‘ing it, would make limit = 7, position = 0. Now, reading can occur.

    Here’s an example on how to best use flip():

    public static final void nioCopy(ReadableByteChannel input, WritableByteChannel output) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
        while (input.read(buffer) != -1) {
            //Flip buffer
            buffer.flip();
            //Write to destination
            output.write(buffer);
            //Compact
            buffer.compact();
        }
    
        //In case we have remainder
        buffer.flip();
        while (buffer.hasRemaining()) {
            //Write to output
            output.write(buffer);
        }
    }
    
    • Additional resource.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java code below: import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; public class Test
I have wrote below code. import java.io.*; class Test { public static void main(String
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have written a MapReduce program, code is below: import java.io.IOException; import java.util.Iterator; import
I try use a integer array in java with the code below: public static
I've got the following java code, which is giving the error below: import java.io.File;
Given the Java code below, what's the closest you could represent these two static
I'm trying to work with the below code: import javax.servlet.*; import javax.servlet.http.*; import java.io.*;
Consider the Java code below, what would happen if there were no paintComponent method
For example, I have the java code below: URL u = new URL(http://google.com); URLConnection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.