Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileInputStream Array problem

Shaun Brierly
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[]) 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 ");
int byteCounter = 0;
byte[] tempArray;
int counter;
int bytesRead = CHUNK_SIZE;
SwingUtilities.invokeLater(new Runnable() {
public void run() {;
} // END run

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

array = new byte[CHUNK_SIZE];

bytesRead =;


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

} else



} // END try
catch(Exception e) { System.out.println("error"); e.printStackTrace(); }
} // END uploadFile
Shaun Brierly
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
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 ]
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!