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 6071563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:03:41+00:00 2026-05-23T10:03:41+00:00

I am writing a very simple networking library that I can use for my

  • 0

I am writing a very simple networking library that I can use for my other projects in the future. Right now, I am having trouble handling reading operations. I have a class that is responsible for handling events from SocketChannels, and it also contains the input and output buffers. To initialize a buffer, you need to define the size of it. So by default, all the buffers are 1024 bytes in size.

The problem that I have is, if I read a packet that is larger than 1024 bytes, I will get an exception. This could be fixed by allocating an even larger buffer by default (2048 bytes, instead of 1024, for example) but that seems like the easy way out, and I particularly don’t like that solution.

The solution that I came up with was creating a very large, direct, static buffer (it is Short.MAX_VALUE large). All data from SocketChannels would be read into the large buffer, and then copied over into the smaller buffers (and the buffers are expanded, if they cannot fit the data).

I am just worried that there might be a large cost of constantly clearing and putting data into the large ‘carry-over’ buffer. I would love it if there was something like SocketChannel.available(), but the only closest alternative is Socket.getInputStream().available(), but that method blocks. That way I would expand the input buffer (not the large one) if there is more available data than the buffer can fit. Unfortunately, I cannot – so the only solution that I can think of is the one I mentioned above.

I have come here to ask … do any of you wise people have a possibly better solution to the problem? Also, I don’t want to use an external library – it’s just personal preference.

Thanks a lot in advance!


My apologies. After talking to a close friend, I have realized that there was no problem to begin with. It’s quite a long explanation so I shall not explain it. My apologies.


Note to poster: please never delete the question text. It serves as guidance for others.

  • 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-05-23T10:03:42+00:00Added an answer on May 23, 2026 at 10:03 am

    I have implemented an NIO socket layer such that I have a dynamic list of buffers that are all sized the same (whatever size you would like). For writing data, once a buffer is filled up, a new buffer is added to the list and data continues to be written to the new buffer. When it is decided (by your code, supposedly) that data is ready to be written to the socket (based on the Selector’s callback), I write each of the buffers out until all the buffers have been written to the socket.

    I think you can do a similar thing for reading from a SocketChannel (though I didn’t implement it in my application because of the different requirements for reading). Whether or not this is a better solution for your application is up to you.

    I’m not entirely sure I understand why you will get an exception…why do you try to read more than the size of the buffer? Whatever logic reads from the socket should ensure that you don’t read more than the size of the buffer.

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

Sidebar

Related Questions

I'm writing a very simple and small wrapper around sqlite3, and use sqlite3_get_table() to
I'm writing a very simple CRUD app that takes user stories and stores them
I'm writing a very small and simple server that I want to listen on
I'm writing a page that does very simple search queries, resulting in something like:
I am writing a very simple Bash script that tars a given directory, encrypts
I am writing a distributed Java application that will make heavy use of networking,
I am writing a very simple script that calls another script, and I need
I'm a bit confused. I'm writing a very simple file system that basically: -reads
I am writing an auto update client. It's a very simple app that: 1)
I'm writing a very simple blog engine for own use (since every blog engine

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.