• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileInputStream Array problem  RSS feed

 
Shaun Brierly
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very odd problem... the code below sends the same bytes over and over... the function FileInputStream.read(bytes[]) will not overwrite what's already in array, unless I call array = new byte[CHUNK_SIZE] in the while loop which is very ugly for memory, and if the file is large enough will destroy the system... any ideas?

public void uploadFile(final String path, final String dest) {

tTransferThread = new Thread(new Runnable() {
public void run() {



String fInput;
String fDest;
try {
FileInputStream f = new FileInputStream (path);
int size = f.available();
byte[] array = new byte[CHUNK_SIZE];
byte[] cleanArray = new byte[CHUNK_SIZE];
fDest = dest + "/" + path.substring(path.lastIndexOf("\\") + 1);
fDest = fDest.replace('\\', '/');

write("upload ");
uploadStream.writeObject((Object)fDest);
uploadStream.flush();
int byteCounter = 0;
byte[] tempArray;
int counter;
int bytesRead = CHUNK_SIZE;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
uwUpload.mainPanel.show();
} // END run
});

uwUpload.setProgress(0);
uwUpload.setStatus("Uploading: " + path.substring(path.lastIndexOf("\\") + 1));
while(byteCounter < size) {

array = new byte[CHUNK_SIZE];

bytesRead = f.read(array);
System.out.println(array);
byteCounter+=bytesRead;

Thread.sleep(10);

uwUpload.setProgress( (int)((double)
if(bytesRead < CHUNK_SIZE) {
tempArray = new byte[bytesRead];
for(counter=0; counter<bytesRead; counter++)
tempArray[counter] = array[counter];

uploadStream.writeObject((Object)tempArray);
} else
uploadStream.writeObject((Object)array);

uploadStream.flush();

}
uploadStream.writeObject((Object)"~~~doneWHEEE~~~");

} // END try
catch(Exception e) { System.out.println("error"); e.printStackTrace(); }
}});
tTransferThread.start();
} // END uploadFile
 
Shaun Brierly
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to take out array = new byte[CHUNK_SIZE] in the code, technically the code as is works, however is very bad for memory... however when I take out the line array never gets overwritten for some strange reason...
Shaun Brierly
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please indent your code to make it more readable, and use [code] tags to preserve the indentation when you post. Thanks.
It's hard to follow the code as written, but it seems to have several problems and to be making a lot more work out of this than is necessary. I give a simple example of how to process a stream in a loop like this here. Only one array is necessary, and no need for any ObjectOutputStream or the writeObject() method. Object serialization adds a lot of overhead here - you're not just sending the desired bytes of data in the file, but also information about the class and size of the each array. I doubt that's what you really want. Try something simpler, modifying the code I gave as necessary.
Also, note that available() does not necessarily tell you the total number of bytes in a stream - it just tells you how many can be read quickly at that instant. If your stream is large, available() may merely tell you how many bytes are in a system buffer somewhere. The best way to reliably know when a stream is complete is by checking for a -1 return value, indicating end of stream. If you want to know the file size before the end of file is reached (for purposes of your progress bar), you can use the length() method in the File class.
[ October 10, 2002: Message edited by: Jim Yingst ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!