Results 1 to 2 of 2
  1. #1
    nicox1984 is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Problems whit reading file from InputStream

    I've got a problem using java InputStream to read a file from a ftp server.

    Following the core part of my code:

    Java Code:
    int _headerread=0;
    int _byteread=0;
    int _dimMax = 1024;
    byte[] _buffer = new byte[_dimMax];
    byte[] _header = new byte[3];
    
    InputStream _br	= _client.retrieveFileStream(_fileName);
    
    while(_br.available()>0){
             //I read the header to get the lenght of next read. Transfer is in MODE BLOCK, so
             // after the first byte (0x80 or 0x40) I find 2 byte rappresenting the length.
             _headerread = _br.read(_header, 0, 3);
    
            if ((_header[0]  != (byte) 0x80) & (_header[0]  != (byte)0x40)){
    	        throw new Exception("Error while reading lotto: missing 0x80 and 0x40 in first byte"); 
            }
    
            //I compute the lenght.
            _l  	=  (_header[1] & 0x00ff)<<8 | (_header[2] & 0x00ff);
    
            //The lenght shouldn't never be bigger than _dimMax, but if it happens, I proceed with padding _buffer.
            if(_l > _buffer.length){
            _buffer = new byte[_l];
            }
            _byteread = _br.read(_buffer, 0, _l);
    
            //[...] COMPUTE OTHER 
    }

    What I do is very simple:
    1) I read a file from a FTP Server;
    2) I read the header of every line: I compute the lenght.
    3) I read the line;
    4) I compute other things.
    5) I repeat from 1 to 4 until end of file.

    The problem is that after 2/3 cicle I obtain error in reading line: I try to read XX byte but it read less byte...
    for example:
    _l = 998 byte, after reading:
    _byteread = _br.read(_buffer, 0, _l);

    I obtain _byterread is less than 998 byte!!


    Is it normal???? I'm confused


    I solved the problem by inserting a pause after (or before!) reading operation:


    Java Code:
    int _headerread=0;
    int _byteread=0;
    int _dimMax = 1024;
    byte[] _buffer = new byte[_dimMax];
    byte[] _header = new byte[3];
    
    InputStream _br	= _client.retrieveFileStream(_fileName);
    
    while(_br.available()>0){
             //I read the header to get the lenght of next read. Transfer is in MODE BLOCK, so
             // after the first byte (0x80 or 0x40) I find 2 byte rappresenting the length.
             _headerread = _br.read(_header, 0, 3);
    
            if ((_header[0]  != (byte) 0x80) & (_header[0]  != (byte)0x40)){
    	        throw new Exception("Error while reading lotto: missing 0x80 and 0x40 in first byte"); 
            }
    
            //I compute the lenght.
            _l  	=  (_header[1] & 0x00ff)<<8 | (_header[2] & 0x00ff);
    
            //The lenght shouldn't never be bigger than _dimMax, but if it happens, I proceed with padding _buffer.
            if(_l > _buffer.length){
            _buffer = new byte[_l];
            }
            _byteread = _br.read(_buffer, 0, _l);
    
            Thread.sleep(_threadPause);	//_threadPause is 1, 2 or 3 milliseconds.
            //[...] COMPUTE OTHER 
    }
    Is there a solution??? I don't like the solution of adding a sleep time, because file to read is very large: 300.000 line to read means that total sleep time is 300 seconds...more than 5 minutes!!! So much time!!


    I'm sorry for my english, but I don't speek it very well!
    Thank you!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Problems whit reading file from InputStream

    I obtain _byterread is less than 998 byte!!
    Yes you can read less than the total bytes that are being sent. When that happens you need to continue reading until you get all of the bytes that you expect.

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Reading a two's complement number from a InputStream
    By TorErik86 in forum Networking
    Replies: 1
    Last Post: 09-20-2010, 02:21 PM
  3. Reading inputstream on socket
    By javanetworknew in forum Networking
    Replies: 1
    Last Post: 04-27-2010, 11:01 AM
  4. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM
  5. Un expected behaviour when reading from inputstream
    By adoorsarath in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •