Manipulate file while being downloaded?

Hi,

I was wondering if there is a way that the application server can intercept a file being downloaded and change it's byte code in a certain way.

For example, Imagine I upload a letter on the internet. If the letter is downloaded by John then the first part will be address to John else if the letter is downloaded by ben it will be addressed to ben.

However I do not want to actually build two letters in memory since there could be tousands of users downloading the same letter which would crash the server (imagine this is a very long a big letter).

So is it possible to capture the user download from the application server and convert some of the bytes in that letter accordingly.

I am not intrested to know how to convert the bytes in the letter, but how to make this possible when a file is being downloaded!

regards,

Sim085

[883 byte] By [sim085a] at [2007-11-26 15:47:55]
# 1

> I am not intrested to know how to convert the bytes

> in the letter, but how to make this possible when a

> file is being downloaded!

Sounds like you want to generate the file when a person tries to view it. One way to do that is to link to a servlet with the file name as argument, and let the servlet generate the letter.

Kaj

kajbja at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 2
Either use a Filter or divert any download to a servlet that not only transmits but also performs the content change.
quittea at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 3
You can do that if you use an Action for the download instead of a direct link to a file.In your action you can stream your file after editing it using the session data of the user.
Rodney_McKaya at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 4
Always 1 minute late ...
quittea at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 5
Thanks people :)I tought of the servlet thing, have used it to generate some pdf's once. However wouldn't that be creating a file on the server (at least for a temporary ammount of time?). I could have the wrong impression as always :)Regards,Sim085
sim085a at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 6

> I tought of the servlet thing, have used it to

> generate some pdf's once. However wouldn't that be

> creating a file on the server (at least for a

> temporary ammount of time?). I could have the wrong

> impression as always :)

What do you mean? You can create it in memory, on the fly, you don't have to write anything to disk.

Kaj

kajbja at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 7

> What do you mean? You can create it in memory, on the

> fly, you don't have to write anything to disk.

Ishould have been more clear. What I am afraid of is to use out all the memory of the computer!!

The question should better be asked as follows I guess:

If I use a servlet, would the same file be loaded in memory each time it is going to be downloaded.

Example:

Ben comes to download the file, the servlet loads the file in memory, makes the changes to the byte code and let the user download it.

Mark comes to download the file, the servlet loads the file in memory, makes the changes to the byte code and let the user download it.

Now having 10 users downloading the same file would not be a problem to load that file in memory! However having 10,000 users downloading the same file with the file being 500Mb or more then I think that would be a critical problem!

Regards,

Sim085

sim085a at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 8
Restrict the number of server sockets.
ChuckBinga at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 9
Since all the instances of the servlet are running on the same JVM you can synchronize this part of the code, so that only one instance of the file will be loaded all the time.
Rodney_McKaya at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 10

> Now having 10 users downloading the same file would

> not be a problem to load that file in memory! However

> having 10,000 users downloading the same file with

> the file being 500Mb or more then I think that would

> be a critical problem!

You don't have to load the whole file into memory. You can use a FileReader, read a chuck, or a line, replace tags, write data to client and then repeat.

Kaj

kajbja at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 11

> Since all the instances of the servlet are running on

> the same JVM you can synchronize this part of the

> code, so that only one instance of the file will be

> loaded all the time.

hmm .. that sounds intresting. Have to read more on how servlets can create downloadable content.

However from what I unbderstand, before the user starts downloading the file, the servlet needs to return the file in question.

Usually it is still represented as a link to file on the drive! I have always tought that the servlet needs to always save the file on the server, and then inform the Application Server that the download is available from the particular location.

What I am searching for is to create a download that is customizable (manipulated a download time) while taking the least ammount of space in memory and harddisk! At the moment searching for available content on google, would be more then happy to read any articles you people think are relevant on this :)

Regards,

Sim085

sim085a at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 12

> You don't have to load the whole file into memory.

> You can use a FileReader, read a chuck, or a line,

> replace tags, write data to client and then repeat.

Yes I know about that, however I am not searching to replace tags in a file, but actuall bytes. The word document was just an example, the file could be a zip file, a rar file, a jar file or anything else. That is why in the first post I said:

> I am not intrested to know how to convert the bytes in the letter, but

> how to make this possible when a file is being downloaded!

regards,

Sim085

sim085a at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 13
For example, imagine that before the download of any file I would like to encrypt the file in question (byte code manipulation).regards,Sim085
sim085a at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 14
> having 10,000 users downloading the same file with> the file being 500Mb or more then I think that would> be a critical problem!Is this a realistic scenario for your project?
quittea at 2007-7-8 22:07:22 > top of Java-index,Java Essentials,Java Programming...
# 15

> For example, imagine that before the download of any

> file I would like to encrypt the file in question

> (byte code manipulation).

You should still encrypt on the fly, you don't have to encrypt the whole file before you send the data to the client.

There's no difference if you want to replace bytes or tags. You should still do the same thing (but of course use an InputStream instead of a Reader)

Kaj

kajbja at 2007-7-21 16:34:18 > top of Java-index,Java Essentials,Java Programming...
# 16

> Is this a realistic scenario for your project?

could be one day ;) at the moment I am mostly trying to learn more on the subject, however would like to use what I learn in some project one day.

> You should still encrypt on the fly, you don't have

> to encrypt the whole file before you send the data to

> the client.

That is what I WANT :) ... 'on the fly' ... without storing anything on the server harddisk or memory!! :) So you tell me I can do this with normal servlets!! As I said I am no expert on servlets, what is this process called so that I can make a quick search on google and read more about this!?

> There's no difference if you want to replace bytes or

> tags. You should still do the same thing (but of

> course use an InputStream instead of a Reader)

I agree with that.

thanks and regards,

Sim085

sim085a at 2007-7-21 16:34:18 > top of Java-index,Java Essentials,Java Programming...
# 17
See this example how to stream a file using a servlet: http://www.devx.com/getHelpOn/Article/11698/0/page/1
Rodney_McKaya at 2007-7-21 16:34:18 > top of Java-index,Java Essentials,Java Programming...
# 18

> That is what I WANT :) ... 'on the fly' ... without

> storing anything on the server harddisk or memory!!

> :) So you tell me I can do this with normal

> servlets!!

I don't see why you wouldn't be able to do that with a servlet (but I'm not a web developer). The servlet has an output stream which you should write to. The servlet does not care where the data that you write comes from, so you read a chunk, replaces data, writes, and then repeats.

> As I said I am no expert on servlets, what

> is this process called so that I can make a quick

> search on google and read more about this!?

I don't know what it's called. It could be a special kind of streaming?

kajbja at 2007-7-21 16:34:18 > top of Java-index,Java Essentials,Java Programming...
# 19

> That is what I WANT :) ... 'on the fly' ... without

> storing anything on the server harddisk or memory!!

> :) So you tell me I can do this with normal

> servlets!! As I said I am no expert on servlets, what

> is this process called so that I can make a quick

> search on google and read more about this!?

I doubt there's a special name for this, as it's just using a servlet's service methods for reading a file and manipulating data while pushing it in the pipe to the client.

Start simple and care for more load when it happens, e.g. by using a cache if necessary.

quittea at 2007-7-21 16:34:18 > top of Java-index,Java Essentials,Java Programming...
# 20
Thanks to all for the help and support :) I will try to read some more on this topic before continue to asking more questions. However really a big thanks for all the help :) you really gave me a head-start on this.Thank You,Sim085
sim085a at 2007-7-21 16:34:18 > top of Java-index,Java Essentials,Java Programming...