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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:42:57+00:00 2026-05-22T00:42:57+00:00

I am writing an app for android which uses InputStream from Socket . I

  • 0

I am writing an app for android which uses InputStream from Socket. I am trying to send a file from pc to android this way. The size of the file is almost 40kb and on android I am finding that it is able to read only 2kb of data at a time so I am reading it in chunks.

I have two methods for reading bytes

1)

while((d=inputStream.read())>=0)
{
    imgData[i]=(byte)d;
    i++;
    if(i>=40054)
    {
        // do the further processing here like saving it on disk.
        i=0;
    }
}

2)

while(inputStream.read(byte,0,2048)>=0)
{
    //merge this byte to buffer here... 
    i=i+2048;
    if(i>=40054)
    {
        // do the further processing here like saving it on disk.
        i=0;
    }
}

Form these 2 methods which will be faster in terms of performance?

  • 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-22T00:42:58+00:00Added an answer on May 22, 2026 at 12:42 am

    The second, potentially by a long way. Reading a block at a time is almost always preferable to reading a byte at a time, unless you really only want to read a single byte anyway.

    However, your code is currently broken, in that you don’t take account of the return value of read other than to check that something has been read. It may not have read 2048 bytes. You should use something like:

    int bytesRead;
    
    while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) > 0)
    {
        // Use bytesRead here
    }
    

    With this code in place, you may well be able to read more than 2K at a time safely in some situations… it depends on the network, but at least the code will be reliable.

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

Sidebar

Related Questions

I'm writing an app to help facilitate some research, and part of this involves
I'm writing an app which for various reasons involves Internet Explorer (IE7, for the
I'm writing an app that will need to make use of Timer s, but
I'm writing an app using asp.net-mvc deploying to iis6. I'm using forms authentication. Usually
I'm writing an app that contains the following tables: (1) employee_type, (2) employee and
I'm writing an app where 3rd party vendors can write plugin DLLs and drop
I'm writing an app in C# (.net 3.5) and I have a question about
I'm currently writing an app for a Windows Mobile 5.0 app and it seems
I'm writing an app for Blackberry that was originally implemented in standard J2ME. The
I'm writing an app for WM that handles incoming SMS events. I tried making

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.