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

The Archive Base Latest Questions

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

I am trying to read in data from an input stream, but if the

  • 0

I am trying to read in data from an input stream, but if the program does not receive data for X amount of time, I would like to terminate the attempt and return a -1. I was previously using Thread.sleep( X ) but then realized that thats a completely incorrect approach. If anyone has any ideas please let me know. Here is my code for reading from the input stream…

            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer, 0, length);

                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                // Start the service over to restart listening mode
                BluetoothService.this.start();
                //break;
            }
  • 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-09T15:09:03+00:00Added an answer on June 9, 2026 at 3:09 pm

    You can use Future to do this.

    First, you need a class which will be returned as “future” value:

    public class ReadResult {
        public final int size;
        public final byte[] buffer;
    
        public ReadResult(int size, byte[] buffer) {
             this.size = size;
             this.buffer = buffer;
        }
    }
    

    Then you need to use executor service and use get(long timeout, TimeUnit unit) like this:

            ExecutorService service = Executors.newSingleThreadExecutor();
            Future<ReadResult> future = service.submit(new Callable<ReadResult>() {
    
                @Override
                public ReadResult call() throws Exception {
                    bytes = mInStream.read(buffer, 0, length);
                    return new ReadResult(bytes, buffer);
                }
            });
    
            ReadResult result = null;
            try {
                result = future.get(10, TimeUnit.SECONDS);
            } catch (InterruptedException e1) {
                // Thread was interrupted
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                // Something bad happened during reading
                e1.printStackTrace();
            } catch (TimeoutException e1) {
                // read timeout
                e1.printStackTrace();
            }
    
            if (result != null) {
                // here you can use it
            }
    

    In that way you will be able to achieve your goal. Plz note that its better to subclass Callable class which will accept inputstream as constructor argument then using class variables.

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

Sidebar

Related Questions

I am trying to read data from an RS-232 port. Does anyone have an
I am trying to read data from a file stream as shown below: fileStream.Read(byteArray,
I'm trying to figure out how to read from the standard input stream of
I am trying to write the data using the pipe input streams. But from
I'm trying to read data from an xml file and display it in a
I'm trying to read data from excel using POI. How can I check if
I am trying to read data from a sqlite database in an ios app.
I am trying to read data from a sqlite database and display it in
I'm trying to read data from SQL Server database using Perl and the DBI
I am trying to read data from a bluetooth barcode scanner (KDC300) using C.

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.