Encontrei esta mensagem no FAQ do JGuru///////////////////////////////////////////////////// How can I compress my data to save bandwidth when sending across a socket? Location: http://www.jguru.com/faq/view.jsp?EID=3823 Created: Dec 31, 1999 Modified: 2002-03-25 09:34:57.661 Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7) The GZIPInputStream and GZIPOutputStream classes found in the java.util.zip package compress data with the GZIP file format, as defined by RFC 1952 (ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html). See also http://cnrit.tamu.edu/sir_anodos/ for classes to use compression over a network stream. [User seems to have left university. Anyone know where it went?] Comments and alternative answers For more info, see How can I compress/uncompress a... Author: John Zukowski (http://www.jguru.com/guru/viewbio.jsp?EID=7), Oct 1, 2000 For more info, see How can I compress/uncompress a long string or a file (of any type)?. Doesn't work so well with live Socket communications. Author: Andy DePue (http://www.jguru.com/guru/viewbio.jsp?EID=24220), Mar 28, 2001 The short answer above doesn't really help those people who want to add a "compression layer" to their socket streams. The reality is, if you wrap your Socket streams in these compressed streams, you will find that your client or server will seem to hang. Why? Say you write a byte to a GZip stream... does that mean a byte (or less than a byte) comes out on the other end right when you write it? Nope. The GZip streams do not write to the underlying stream until there are enough bytes on the incoming side to send compressed bytes to the outgoing side. Here is an example: Client A connects to Server B. They both use the GZip streams to compress their socket streams. Server B sends a HELLO message to Client A, but Client A never receives it because the GZip output stream on Server B doesn't have enough data to compress to output anything. Meanwhile, Server B is waiting for an ACK from Client A, and never gets it because Client A never got its HELLO message. Nothing else happens. Again, this is because Server B never sent all the HELLO bytes out of its Socket to Client A, because the GZip streams hold on to the outgoing data until they have enough to compress. And calling flush() doesn't help at all. An alternative is not to wrap the Socket streams in the GZIP streams, but instead individually compress the pieces you send (in other words, put a GZIP output stream around a ByteArrayOutputStream, write out your data, close the stream, and then send the resulting byte array from the ByteArrayOutputStream out the socket). This, of course, is not very effecient CPU, memory, or compression wise. Anyone else have any ideas on how to wrap a Socket stream in a GZIP stream and have it actually work? Re: Doesn't work so well with live Socket communications. Author: Mark Phillips (http://www.jguru.com/guru/viewbio.jsp?EID=134288), Jul 12, 2002 About a year ago I wrestled with the same problem Andy outlines and failed to solve it. Nowadays I expect the task is fairly easily accomplished with NIO and ByteBuffers. You compress your data before loading it into your ByteBuffer for transmission. Steams be damned, LOL. --Mark ///////////////////////////////////////////////////// Fernando Henrique da Silva wrote: > Ol� amigos! Tudo bem? > > Estou utilizando uma estrutura cliente-servidor, e estou utilizando > strings para o di�logo entre o cliente e o servidor. Abaixo disso > utilizo a classe GZIPOutputStream e a GZIPInputStream. Ou seja: > > GZIPOutputStream gzipout = new > GZIPOutputStream(socket.getOutputStream()); > PrintWriter out = new PrintWriter( gzipout ); > > Teoricamente, ap�s eu escrever a mensagem de um para o outro, eu > devo utilizar a fun��o finish() para finalizar a escrita. > Segundo a documenta��o, a fun��o finish "Finishes writing compressed > data to the output stream without closing the underlying stream" e > dispara exce��es. > O problema � que sempre ela dispara exce��es, e depois disso eu n�o > consigo mais nem enviar nem receber strings pelos sockets, embora a > conex�o n�o caia. > O que est� acontecendo? > > Se poss�vel, enviar a resposta tamb�m para o email > [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > > Obrigado desde j�! > > > Rael Gugelmin Cunha > graduando > em c.computa��o > ------------------------------ LISTA SOUJAVA ---------------------------- http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP d�vidas mais comuns: http://www.soujava.org.br/faq.htm regras da lista: http://www.soujava.org.br/regras.htm historico: http://www.mail-archive.com/java-list%40soujava.org.br para sair da lista: envie email para [EMAIL PROTECTED] -------------------------------------------------------------------------
