简体   繁体   English

java套接字发送压缩对象

[英]java socket sending compressed object

I want to compress my serializable objects and send them through java sockets. 我想压缩我的可序列化对象,并通过java套接字发送它们。 serializable object: 可序列化的对象:

public class MyObject implements Serializable {

private int id;
private String name;
//getter,setter and equal, hashCode, toString methods

}



public class Sender {

private Socket clientSocket;

public void send(MyObject obj){
GZIPOutputStream gzipOut = new GZIPOutputStream(clientSocket.getOutputStream());
ObjectOutputStream objectOut = new ObjectOutputStream(gzipOut);
objectOut.writeObject(obj);
}
}



public class Receiver {

private Socket serverSocket;

public MyObject receive(){
GZIPInputStream gzipIn = new GZIPInputStream(serverSocket.getInputStream());
ObjectInputStream objectIn = new ObjectInputStream(gzipIn);
return (MyObject) objectIn.readObject();
}

}

this throws ZipException: Not in GZIP format at receiver side. 这会引发ZipException:在接收方不是GZIP格式。

I won't close streams and sockets, because need to send multiple objects using the same stream and socket (performance wise) 我不会关闭流和套接字,因为需要使用相同的流和套接字发送多个对象(性能明智)

ObjectStream is one of the most verbose serialisation formats available. ObjectStream是可用的最详细的序列化格式之一。 Just about any other Serialization would be better for size. 几乎任何其他序列化都会对大小更好。 ObjectStreams have to flushed to send all the data and you can't send more than one stream in a connection, making it inefficient to just send one object per connection. 必须刷新ObjectStream才能发送所有数据,并且在一个连接中不能发送多个流,这使得每个连接仅发送一个对象的效率很低。

Compressed streams work best for large amounts of data, when you try to apply them to a few bytes, the result is larger, not smaller. 压缩流最适用于大量数据,当您尝试将其应用到几个字节时,结果将更大,而不是更小。 Compressed Streams must be flushed to be read. 必须刷新压缩流才能读取。

need to send multiple objects using the same stream and socket (performance wise) 需要使用相同的流和套接字发送多个对象(性能明智)

This means you have to retain your GZIP/Object streams. 这意味着您必须保留GZIP /对象流。 You can't use more than one per stream. 每个视频流最多只能使用一个。

public class ObjectSocket implements Closeable {
    private final Socket socket;
    private final ObjectOutputStream output;
    private final ObjectInputStream input;

    public ObjectSocket(Socket socket) throws IOException {
        this.socket = socket;
        this.output = new ObjectOutputStream(new DeflaterOutputStream(socket.getOutputStream()));
        this.input = new ObjectInputStream(new InflaterInputStream(socket.getInputStream()));
    }

    public void send(Serializable obj) throws IOException {
        output.writeObject(obj);
        output.reset();
        output.flush();
    }

    public <T extends Serializable> T receive() throws IOException, ClassNotFoundException {
        return (T) input.readObject();
    }

    @Override
    public void close() throws IOException {
        output.close();
        input.close();
        socket.close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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