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 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

So I'm writing an Android app which uses a large c++ library. I have
I am writing an app which uses android's speech recognition. However my app doesnt
I'm writing an Android game which uses an AsyncTask when the app starts to
I am writing an Android app, part of which will be a survey involving
I'm writing an Android app. I have a main method, which creates and runs
I am writing an Android App in which I am using Spinner Control. I
I am writing a simple android app which have a edittext and a button.
I've come from iPhone background and am writing an android app. I've got a
I am writing a commercial Android app to take survey, which should also give
so i am writing an app for android which will push some data over

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.