压缩和解压缩流
我发现这篇关于用JAVA实现的简单代理服务器的文章:
http://www.java2s.com/ Code/Java/Network-Protocol/Asimpleproxyserver.htm
该代码只是从客户端获取一些流,然后将其发送到服务器,然后从服务器获取流并将响应发送到客户端。我想做的是在发送该流之前对其进行压缩,并在接收后对其进行解压缩。
我找到了类 GZIPInputStream
但我不知道如何使用它,而且我在互联网上找到的内容对我没有帮助。我要么不太明白,要么这对我来说不是一个好的解决方案。
我的想法也是这样,但我不确定是否可以:
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);
}
现在发送到服务器的数据应该在发送之前进行压缩(但我不确定这是否是正确的解决方案)。是吗?
现在想象服务器向我发送压缩数据。 所以这个流:
final InputStream streamFromServer = server.getInputStream();
被压缩了。
我怎样才能解压它并写信给
final OutputStream streamToClient = client.getOutputStream();
谢谢你们的帮助,伙计们!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读这些流的 javadoc : http:// download.oracle.com/javase/6/docs/api/java/util/zip/GZIPInputStream.html 和 http://download.oracle.com/javase/6 /docs/api/java/util/zip/GZIPOutputStream.html。
GZIPOutputStream 在将写入的字节发送到包装的输出流之前对其进行压缩。 GZIPInputStream 从包装流中读取压缩字节并返回未压缩字节。
因此,如果您想向任何人发送压缩字节,则必须写入 GZIPOutputStream。但当然,只有当接收端知道并解压缩它接收到的字节时,这才有效。
同样,如果要读取压缩字节,则需要从 GZIPInputSTream 读取它们。但当然,只有发送端确实使用相同的算法压缩字节时,它才会起作用。
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.