TCP Socket send data in GZIP Compression format - java

I am sending MultiPart content to my remote server to store it in filesystem. For this I am using Java TCP/IP protocol. For avoiding network bandwidth and TCP Input / Output buffer memory , I am sending the data in GZIP compressed format. But , I cannot decompress the data received from the client. I got Unexpected end of ZLIB input stream Exception. Its due to the server is receiving data in chunks.
Java Code
Client
OutputStream out = new GZIPOutputStream(sock.getOutputStream());
byte[] dataToSend = FileUtil.readFile(new File("/Users/bharathi/Downloads/programming_in_go.pdf"));
out.write(dataToSend);
Server
out = new FileOutputStream("/Users/bharathi/Documents/request_trace.log");
InputStream in = new GZIPInputStream(clntSocket.getInputStream());
int totalBytesRead = 0;
int bytesRead;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = in.read(buffer)) != -1)
{
out.write(buffer , 0 , bytesRead);
totalBytesRead += bytesRead;
}
Is there any solution to send the data in GZIP compressed format in Socket?

GZIPOutputStream generates a GZIP file format, meaning that the other end has to receive the complete stream (which is a file) before it can process it, this is the reason for your error.
If you are looking to actually do a stream based data transfer, drop gzip, and go for zlib, I believe Zlib compression Using Deflate and Inflate classes in Java answers how to do this.

Try adding:
out.flush();
sock.shutdownOutput();
to your client code.

Related

Android - HttpURLConnection count bytes

I send some data to server I use httpURLConnection and DataOutputStream.
DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
Is there a way to check if i have sent all bytes?
I mean there is something like getContentLength() but it return -1 so i guess it contains only a length which i set in header. i have to be sure if i sent e.g. 500/500 bytes(not only 480/500 bytes). so is there a way to check it? or all i can do is valid data at server side and send response?

Encoding and Decoding of Pcap Packets

I need to convert packets to byte format and decode it . How is it possible to convert packets captured using jnetpcap library to array[bytes] and vice-versa in Java?
PcapPacket class has the method
public int transferStateAndDataTo(byte[] buffer) which will copy the contents of the packet to the byte array.
define the byte[] with size as packet.getTotalSize()
if you are looking for the payload
//opens an offline pcap file
Pcap pcap = Pcap.openOffline(pcapIpFile, errbuf);
//packet object
PcapPacket packet = new PcapPacket(JMemory.POINTER);
Payload pl = new Payload();
pcap.nextEx(packet); // retrieves the next packet from input loop thru until eof
if(packet.hasHeader(pl)) //this will check for and retrieve the payload
pl.data() // this will give you the data in the payload as a byte stream
for data in the different headers (ethernet/ip/tcp) there are other methods available with the implementation

How to download a file from the server using Servlet

I am new to servlet technology, i need to write code to download files from the server at client side.
Can we download files diectly from the server using servlet technology?
Please provide the valuable suggestions.
If I understand you correctly, You can download the file from HTTP servlet via response.sendRedirect() for files available in public location.
Else you need to use the response output stream to bind the file information so that it will prompt you to download for a file:
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(fileToDownload);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
I gues you can handle the exceptions, of course.

Why does Dropbox server not respond when trying to upload a file via its API?

I am using the official Dropbox API for Java.
So far, everything works smoothly. Authentication via oauth works and so do other functions (like directory listings).
Now, I tried to upload a file like this:
InputStream is = getInputStream();
byte[] bytes = is2Bytes(is); // Gets all bytes "behind" the stream
int len = bytes.length;
api.putFileOverwrite(path, is, len, null);
Now, when I do this call, my application hangs for about 15 seconds and then I get an exception thrown that Dropbox server did not respond.
So, first I asked Dropbox support if there was something wrong with their server. There isn't.
Then, I played around with the parameters of the putFileOverwrite method and I found out that if I set len=0 manually, the server responds and creates a 0 byte file with the correct file name.
As another test, I manually entered the value len=100 (the original file has 250KB so that should be ok). Again, the server does NOT respond.
So, what's wrong?
That is not weird at all. Since you use your self-made method is2Bytes, the steam is empty, because you read all the bytes to count them. The proper way of doing this would be either knowing how many bytes you are going to send or using the build-in method for sending a file.
public HttpResponse putFile(String root, String dbPath, File localFile)
Very weird. I was able to work around this by re-creating a new InputStream from the byte array and send that to Dropbox:
InputStream is = getInputStream();
byte[] bytes = is2Bytes(is); // Gets all bytes "behind" the stream
int len = bytes.length;
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
api.putFileOverwrite(path, bis, len, null);

Compressing and decompressing streams

I found this article about simple proxy server implemented in JAVA:
http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm
The code simply gets some stream from the client, after sends it to the server and after it gets stream from the server and sends the response to the client. What I would like to do is to compress this streams before it is sent and decompress after it is received.
I found the class GZIPInputStream but I'm not sure how to use it and what I found on internet didn't help me. I either didn't understand that so much or it was not a good solution for me.
My idea is too that but I'm not sure if its ok:
final InputStream streamFromClient = client.getInputStream();
final OutputStream streamToClient = client.getOutputStream();
final InputStream streamFromServer = server.getInputStream();
final OutputStream streamToServer = server.getOutputStream();
InputStream gzipStream = new GZIPInputStream(streamFromClient );
try
{
while ((bytesRead = gzipStream.read(request)) != -1)
{
streamToServer.write(request, 0, bytesRead);
streamToServer.flush();
}
}
catch (Exception e) {
System.out.println(e);
}
Now the data sent to the server should be compressed before sending (but I'm not sure if it's a correct solution). IS IT?
Now imagine the server sends me the compressed data.
So this stream:
final InputStream streamFromServer = server.getInputStream();
is compressed.
How can I decompress it and write to the
final OutputStream streamToClient = client.getOutputStream();
Thanks for the help, guys!
Read the javadoc of these streams : http://download.oracle.com/javase/6/docs/api/java/util/zip/GZIPInputStream.html and http://download.oracle.com/javase/6/docs/api/java/util/zip/GZIPOutputStream.html.
GZIPOutputStream compresses the bytes you write into it before sending them to the wrapped output stream. GZIPInputStream reads compressed bytes from the wrapped stream and returns uncompressed bytes.
So, if you want to send compressed bytes to anyone, you must write to a GZIPOutputStream. But of course, this will only work if the receiving end knows it and decompresses the bytes it receives.
Similarly, if you want to read compressed bytes, you need to read them from a GZIPInputSTream. But of course, it'll only work if the bytes are indeed compressed using the same algorithm by the sending end.

Categories

Resources