InputStream partial reading problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: InputStream partial reading problem

  1. #1
    Join Date
    Sep 2003
    Posts
    24

    InputStream partial reading problem

    Hi, I have to read a big file partially, in my function as a parameter i will have
    InputStream - 'is'

    byte[] buffer = new byte[1024];
    long sentBytes = 0;
    int bytesRead = is.read(buffer, 0, 1024);
    while (bytesRead > 0) {
    sentBytes += bytesRead;
    bytesRead = is.read(buffer, 0, 1024);
    counter++;
    }
    i'm reading more than i should- how to cope with that? its a problem with last part of stream i suppose..
    bytesRead = is.read(buffer, 0, chunkSize);
    but when i call buffer.length at the end, it prints lower value than 1024, so where is the problem?

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    If the overall size of the file isn't divisible by 1024, then the last chunk you read will be less than 1024.

    Say the file has 2448 bytes:

    chunk 1: 1024
    chunk 2: 1024
    chunk 3: 400

    Because there's only 400 bytes left to read at the end. Is this what you were asking?

Similar Threads

  1. Replies: 2
    Last Post: 09-13-2006, 10:15 PM
  2. Replies: 0
    Last Post: 03-11-2005, 08:44 AM
  3. Java Applet Compiler problem?
    By mdl in forum Java
    Replies: 3
    Last Post: 03-07-2005, 03:34 AM
  4. Reliability Problem
    By elise in forum Java
    Replies: 0
    Last Post: 10-30-2002, 05:40 AM
  5. Replies: 0
    Last Post: 09-08-2000, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center