Hi,
I am facing a problem with reading in data from a file using FileConnection.
Cannot seem to read in any data from a file exceeding a certain file size.
Below is a sample extract from my code which gives the problem:
InputStream in=null;
ByteArrayOutputStream bStrm=null;
FileConnection fc = null;
byte[] byteArray=null;
byte[] raw = null;
try{
fc = (FileConnection) Connector.open("file://"+sfile.getPath()+
sfile.getFilename());
in = fc.openInputStream();
int noOfBytesRead=1;
for(int i=1; i<=numChunks; i++){
bStrm = new ByteArrayOutputStream();
noOfBytesRead = 1;
while(noOfBytesRead != -1) {
byteArray = new byte[1];
//if(in==null)
// System.out.println("in is null");
//if(byteArray==null)
// System.out.println("byteArray is null");
noOfBytesRead = in.read(byteArray);
if (noOfBytesRead != -1) {
bStrm.write(byteArray,0,noOfBytesRead);
//System.out.println("byte stream write");
}
}
raw = bStrm.toByteArray();
}
return;
}
catch(Exception e){
System.out.println(e);
}
finally{
if(in!=null)in.close();
if(fc!=null)fc.close();
if(bStrm!=null)bStrm.close();
}
The code above gives a NullPointerException at the line: in.read(byteArray) when I open the InputStream of a file of size 16MB. But for a file that is 14MB in size below, there is no problem. Uncommenting the println statements reveal that InputStream in is not null.
I have varied the size of byteArray from 1 to 512 to 32K but it still fails.
Can anyone help? Thanks a lot.

Reply With Quote

