压缩和解压缩流 [英] Compressing and decompressing streams
问题描述
我发现这篇关于在JAVA中实现的简单代理服务器的文章:
http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm
代码只需从客户端获取一些流,将其发送到服务器之后,并从服务器获取流并将响应发送到客户端。
我发现类 GZIPInputStream
我的想法太太,但我不知道如果它的确定: / p>
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);
}
现在发送到服务器的数据应该在发送前压缩不知道是否是一个正确的解决方案)。 IS IT?
现在假设服务器向我发送压缩数据。
所以这个流:
final InputStream streamFromServer = server.getInputStream
已压缩。
解压缩它并写入
final OutputStream streamToClient = client.getOutputStream感谢您的帮助,
解决方案读取这些流的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读取它们。但是,它只会工作,如果字节确实使用相同的算法由发送端压缩。
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.
这篇关于压缩和解压缩流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!