简体   繁体   中英

Java: Read/Write compressed objects over TCP

I have to transfer some records over TCP though sockets. I used ObjectInputStream and ObjectOutputStream and it worked fine. One critical think is that the socket is opened once and has to remain open through the whole communication but each side reads and writes more than once (so it more like a persistent connection).

I tried to compress the Objects before writing them in order to increase the overall performance and the results were quite encouraging but, since I used GZIPOutputStream and ByteArrayOutputStream , the memory overhead is too large and in some case I get OutOfMemory error.

I tried DeflaterOutputStream but it didn't seem apropriate for writting objects. Is there any way to solve this problem?

Java serialization is convenient, but rather ineffective both size-wise and in CPU usage. If you want high performance, suggest you use a completely different communication protocol, for example based on Protocol Buffers or something else light-weight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM