wood burning stoves*
The moose likes Servlets and the fly likes skip() method of InputStream??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "skip() method of InputStream???" Watch "skip() method of InputStream???" New topic
Author

skip() method of InputStream???

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
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
Prashanth menon
Ranch Hand

Joined: Feb 20, 2001
Posts: 65
Hi
The FileOutputStream constructor used above will overwrite the existing contents.
I think you must use
os = new BufferedOutputStream(new FileOutputStream(outfile,true));
This will append to the existing file
Prashanth
roopa sudhi
Greenhorn

Joined: Oct 31, 2001
Posts: 16
hi,
I think you can use RandomAccessFile and its methods.
get the length of the file and seek the position.
[seek(position)]now your file pointer will be at the point where you stopped.
HTH.
Roopa
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Prashanth,
Thanks for ur guide.But for that I have to put condition in my servlet coz If I want to write a new file from scratch then this FileOutput constructor is ok
[os = new BufferedOutputStream(new FileOutputStream(outfile));]
If I want to append in that file which I stopped in between then ur suggested constructor will work.But I couldn't decide how I put condition for this two cases in my servlet.Can any one plz guide me for that.
Regards
Bikash
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: skip() method of InputStream???