Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: skip() method of InputStream???


Message #1 by bikashpaul_2001@y... on Thu, 7 Mar 2002 12:03:26
Hi all friends,

I am facing with one problem.Iam writing file to remote destination. front 
end is swing interface and backend is servlet.Now I can stop writing of 
file in between and can start new file from scratch.But problem with
restart that file which I stopped in between for that I take the length of 
remote file from server and now I want to skip that portion which server 
already have.But I couldn't skip that portion it starts writing from
scratch again.Can any one plz tell me where I done a mistake.For that Iam 
pasting those portion of my codes:

In my servlet:-
===============
................
// create a DataInputStream to read from sender 
DataInputStream dis; 
dis = new DataInputStream(request.getInputStream()); 
OutputStream os; 
// create file output stream to write the output file 
System.err.println("Using output file " + outfile); 
os = new BufferedOutputStream(new FileOutputStream(outfile)); 

int cc; 
byte [] buf = new byte[512]; 
long lim, tot = 0; 

// read the number of bytes we should expect 
lim = dis.readLong(); 

// read from the input stream and write to the file 
try { 
System.err.println("Reading " + lim + " bytes..."); 
for(cc = dis.read(buf, 0, 512); 
cc > 0 && tot < lim; 
cc = dis.read(buf, 0, 512)) 
{ 
os.write(buf, 0, cc); 
tot += cc; 
System.err.print("+" + cc + "(" + tot + ")"); 
if (tot >= lim) break; 
} 
} 
catch (IOException ie) { 
try { os.close(); } catch (Exception e2) { } 
System.err.println("Exception during file transfer!"); 
} 

// done reading and writing, close the file output stream 
System.err.println("Read " + tot + " bytes."); 
os.close(); 

// Send back a response message to the application 
response.setContentType("text/plain"); 
PrintStream pos = new PrintStream(response.getOutputStream()); 
pos.println(tot); 
} 
} 

In my Front end Interface:-
=========================
........
try
  {
   BufferedReader input = new BufferedReader (new InputStreamReader
(uc.getInputStream ()));
   int str;
   while((str=input.read()) !=-1)
      {
        System.out.println(str);
      }
  }
catch(Exception serv)
           {
              System.out.println("Error from Servlet"+serv.toString());
           }
try { 
// step 7 - open the input stream 
log("step 7 - open the input stream"); 
InputStream is = uc.getInputStream(); 

//skip the bytes already have in server
is.skip(str);

// step 8 - read the input stream 
int cc; 
byte [] buf = new byte[512]; 
log("step 8 - read the input stream"); 
for(cc = is.read(buf, 0, 512); cc > 0; cc = is.read(buf, 0, 512)) { 
log(new String(buf, 0, cc)); 
} 

// step 9 - close the input stream 
log("step 9 - close the input stream"); 
is.close(); 
.............

Regards
Bikash

  Return to Index