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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:46:00+00:00 2026-06-09T16:46:00+00:00

I have this code on the client side : DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream());

  • 0

I have this code on the client side :

DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream());
while(dis.available()){
     SomeOtherClass.method(dis);
}

But available() keeps returning 0, although there is readable data in the stream. So after the actual data to be read is finished, empty data is passed to the other class to be read and this causes corruption.

After a little search; I found that available() is not reliable when using with sockets, and that I should be reading first few bytes from stream to actually see if data is available to parse.

But in my case; I have to pass the DataInputStream reference I get from the socket to some other class that I cannot change.

Is it possible to read a few bytes from DataInputStream without corrupting it, or any other suggestions ?

  • 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-09T16:46:01+00:00Added an answer on June 9, 2026 at 4:46 pm

    Putting a PushbackInputStream in between allows you to read some bytes without corrupting the data.

    EDIT: Untested code example below. This is from memory.

    static class MyWrapper extends PushbackInputStream {
        MyWrapper(InputStream in) {
            super(in);
        }
    
        @Override
        public int available() throws IOException {
            int b = super.read();
            // do something specific?
            super.unread(b);
            return super.available();
        }
    }
    
    public static void main(String... args) {
        InputStream originalSocketStream = null;
        DataInputStream dis = new DataInputStream(new MyWrapper(originalSocketStream));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in the client side: $.post('<%=Url.Action(Action_Name,Controller_Name)%>', { serverParam: clientParam}, null, null);
If i have written this code on Client Side then does my communication is
I have this server side SLIM code: require 'Slim/Slim.php'; $app = new Slim(); //$app->config('debug',
I have this php code $jsonArray = array(); $sql = SELECT ID,CLIENT FROM PLD_SERVERS;
I have downloaded the sample code from this site for FTP Client http://www.lysesoft.com/products/andftp/index.html But
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I have some code(client side) that is trying to cache responses from a web
I have this controller method: public JsonResult List(int number) { var list = new
I have this code right now that sets the nick and room: io.sockets.on('connection', function(client){
i have this code on the server side: <?php header('Content-Type: text/html; charset=utf-8'); require ../general.variables.php;

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.