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:
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 :s:
I solved the problem by inserting a pause after (or before!) reading operation:
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!
Re: Problems whit reading file from InputStream
Quote:
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.