Buffer Memory in a Client-Server Model - java

I am writing a basic client-server program where I send a file from the client to the server. I using a BufferedReader to upload the file from the client to the server, but I had a question about where it would exactly be stored on the server.
From my understanding, buffer memory is only temporary and doesn't necessarily save information. So how would I ensure that a file would be saved on the server? Thanks in advance.

BufferedReader doesn't store the whole stream in memory, just parts of the stream so you are aren't reading a byte at a time from I/O.
You'll need to read in the entire stream with BufferedReader, and store it in memory, assuming you have enough space, using something like StringBuilder, or store it on the server file system by using a FileWriter.

Related

Reading end of huge and dynamic file via SFTP from server

I am trying to find a way to read just end of huge and dynamic log file (like 20-30 lines from end) via SFTP from server and to save the point until where I read, and if I need more lines, to read more from this point upper.
Everything I've tried takes too long time, I've tried to copy this file on machine and after this to read from end using ReversedLinesFileReader because this method need the File object, when via SFTP you will get only InputStream, takes a lot to download file.
Also tried to count lines and to read from n line but also takes too long and throws exception because sometime in this time file is modified. Another way I tried to connect via SSH and used tail -100 and get the desired result, but just for one time, because next time I will get also new logs, but I need to go upper. Is there a fast way to get the end of file and to save the point and to read more upper of this point later? Any idea?
You don't say what SFTP library you're using, but the most widely used Java SSH/SFTP library is JSch, so I'll assume you're using that.
The SFTP protocol has operations to perform random-access I/O on remote files. Unfortunately, the JSch SFTP client doesn't expose the full range of operations. However, it does have versions of the get operation (for getting a file from the remote server) which permit skipping over the first part of the remote file. You can use one of these operations to read for example the last 10 KB of a file.
Several of the JSch get operations return an InputStream. You can read the contents of the remote file from the input stream. If you want to access the remote file line-by-line, you can convert it to Reader using InputStreamReader.
So, a process might do the following:
Call stat() on the remote file to get its size.
Figure out where in the file you want to start reading from. You could keep track of where you stopped reading last time, or you could guess based on the amount of data you're willing to download and the expected size in bytes of these last 20-30 lines.
Call get() to start reading it.
Process data read from the InputStream returned by the get() call.
Best would be to have a kind of rotating log files, possibly with compression.
Hower rsync is a unidirectional synchronisation, that can transmit only the changed parts of a file: for a log the new end.
I am not sure whether it works sufficiently performant in your case, and ssh is a prerequisite.

Efficient Approach For File Large File Download

I am working on a Java based web application.This application consumes REST API for its data via a custom made rest client which is a sub component of the application.Now I am stuck in a situation where I have to write code for downloading a large file.The rest client calls the api , obtains data stream from the Response and writes the file at a particular location at the server.
Should I read the file and write it to the output stream? Or is there any more efficient way to achieve this functionality?
Thanks in advance
Finally I am using the same approach of reading the large file and writing it to the output stream.The rest client sub component reads the file data from the rest api and writes it at a temporary location on the server.
After that I read that file and write it to the output stream.

Store a file inside an object

I have a Java client/server desktop application, where the communication between client and server is based on Sockets, and the messages exchanged between client and server are serialized objects (message objects, that incapsulate requests and responses).
Now I need to make the client able to upload a file from the local computer to the server, but I can't send the File through the buffer, since the Buffer is already used for exchanging the message objects.
Should i open another stream to send the file, or is there any better way to upload a file for my situation?
I need to make the client able to upload a file from the local computer to the server
- Open a Solely Dedicated Connection to the Server for File uploading.
- Use File Transfer Protocol to ease your work, and moreover its quite easy and reliable to use the Apache's common lib for File uploading and downloading....
See this link:
http://commons.apache.org/net/
You really only have two options:
Open another connection dedicated to the file upload and send it through that.
Make a message object representing bits of a file being uploaded, and send the file in chunks via these message objects.
The former seems simpler & cleaner to me, requiring less overhead and less complicated code.
You can keep your solution and pass the file content as an object, for example as a String - use Base64 encoding (or similar) of the content if it contains troublesome characters

Java RTP/HTTP Streaming

i'm decoding and encoding a videofile via Xuggle to FLV-video format and send it via Sockets to my java server (not the entire file, only parts of it every X seconds). On the server-side I get the encoded file as ByteArrayInputStream. Is it possible to stream this ByteArrayInputStream via rtp or http-streaming? Or do i need a finished encoded file for that? I'm creating a video streaming server, where the client encodes the video file and sends it to the server in parts. This is already done. I'm now stuck on the server side, to stream the ByteArrayInputStream via RTP or HTTP, so I can watch it via VLC. Are there any good examples for it?
I've already tested to save the ByteArrayInputStream to file. This works but I don't wanna save the file on the server. I wanna stream it ;)
Thank you
1) See Server implementation
http://cs.anu.edu.au/student/comp3310/2004/Labs/lab6/lab5.html
2) http://code.google.com/p/vlcj/ and see http://code.google.com/p/vlcj/wiki/Streaming

Can and How do you use RandomAccessFile with a file contained on an FTP server?

This problem pertains to Java
By using RandomAccessFile I intend to be able to also modify the file without blanking it.
The FTP protocol only barely supports random access reads and writes.
That is to say, an FTP client can use the REST command to start reading or writing from a particular offset, but it will always truncate the file from that point.

Categories