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

  • SEARCH
  • Home
  • 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 7990653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:04:49+00:00 2026-06-04T13:04:49+00:00

I am using Java 7 and AsynchronousSocketChannel . I would like to read a

  • 0

I am using Java 7 and AsynchronousSocketChannel. I would like to read a request (e.g. HTTP POST) but I’m struggling to come up with a nice solution to read the full request, if it’s bigger than the size of the ByteBuffer I’m using. E.g. if the ByteBuffer is 4048 bytes and the HTTP POST contains an image that is larger than 4kB.

Is there any nice recursive solution or loop for this?

Here is my code for reading requests:

public void readRequest(final AsynchronousSocketChannel ch) {
    final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
    final StringBuilder strBuilder = new StringBuilder();
    final CharsetDecoder decoder = Charset.forName("US-ASCII").newDecoder();
                    
    ch.read(buffer, null, new CompletionHandler<Integer, Void>() {
                        
        public void completed(Integer bytes, Void att) {
                        
            buffer.flip();                          
            try {
                decoder.reset();
                strBuilder.append(decoder.decode(buffer).toString());
            } catch (CharacterCodingException e) {
                e.printStackTrace();
            }           
            buffer.clear();         

            // More data to read or send response
            if(bytes != -1) {

                // More data to read
                ch.read(...);

            } else {
            
                // Create and send a response
    
            }
        }

        public void failed(Throwable exc, Void att) {
            exc.printStackTrace();
        }

    });
}

And where I have written:

// More data to read
ch.read(...);

it look like a good place for code reuse, but I can’t come up with a nice solution. Is there any way I can reuse the CompletionHandler here? Any suggestion for reading a full request with a limited ByteBuffer?

I would like to solve this in an non-blocking and asynchronous way.

  • 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-04T13:04:50+00:00Added an answer on June 4, 2026 at 1:04 pm

    The completed method is called asynchronously from a thread managed by java when a block of data is read. To reuse the CompletionHandler:

    // More data to read
    ch.read(buffer, null, this); //here you pass the same CompletionHandler you are using
    

    The java guys recommend that when you finish the read operation (the else block) you should use another thread context.

    This is the document that says to avoid blocking and long lived operations inside a CompletionHandler, look in page 33 http://openjdk.java.net/projects/nio/presentations/TS-4222.pdf

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write an asynchronous server using Java 7 and NIO 2.
Im using java and jsf and I would like to create a folder within
While using Java's switch case, it excepts only char and int, but I want
I am using Java Robot class to send keyevents. I tried robot.keyPress() function. But
Using Java 1.6 and the AtomicLongArray, I'd like to copy the original AtomicLongArray into
Using java.util.logging.Logger to output some log to the console just like this: public static
Using Java: I am reading a directory containing files with Greek Names. But when
Using java.util.regex.Pattern.compile() you can specify a pattern as parameter. But how can you specify
Using Java regex pattern I want to match the & symbol. But it should
I'm using Java but I guess this question applies to whatever language. I just

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.