简体   繁体   中英

Extend SocketChannel to add custom read and write

I'm trying to send serialized object over channels. The idea is to extend SocketChannel with method constructor that save Object to send, and add method writeObj() in order to send serialized object size followed by serialized object.

Using private field, I'm trying to manage all procedure in non-blocking mode, so on constructor save object to serialized into ByteBuffer src field and writeObj() write object using super.write(ByteBuffer src) . (respective Object readObj() that return null if Object is not completely received or Object if super.read() returns -1)

The problem is that SocketChannel is abstract class, so super.read() and super.write(..) is not allowed.

Should I extend SocketChannelImpl or there are other right method to do this?

Classic XY problem.

Extend SocketChannel

You can't.

to add custom read and write

You can't.

I'm trying to send serialized object over channels. The idea is to extend SocketChannel with method constructor that save Object to send, and add method writeObj() in order to send serialized object size followed by serialized object.

You can't. You don't need to. You can serialize any Serializable object to a ByteArrayOutputStream , get its bytes, and send those bytes via the same techniques you would use to send any other bytes.

Using private field, I'm trying to manage all procedure in non-blocking mode, so on constructor save object to serialized into ByteBuffer src field and writeObj() write object using super.write(ByteBuffer src).

I don't understand what 'using private field' would have to do with any of this, given that you can't in practice extend SocketChannel in the first place: nor do I understand why a private field would be necessary, or why you would think so.

(respective Object readObj() that return null if Object is not completely received or Object if super.read() returns -1)

This is already two major design mistakes. If the object isn't completely received, you should keep receiving, and if EOS occurs before the object is completely received you should throw an exception rather than returning anything.

The problem is that SocketChannel is abstract class, so super.read() and super.write(..) is not allowed.

The problem is that (a) your design approach and (b) your design details are both (c) fundamentally flawed and (d) completely unnecessary. You don't need any extra code beyond what it would take to receive any application-protocol message in non-blocking mode. It's complex, as you have to deal with partial reads and premature ends of stream at the receiver, and partial writes at the sender, but it doesn't require extending SocketChannel or the other measures you mention.

Should I extend SocketChannelImpl

No.

or there are other right method to do this?

There are lots of posts here about how to handle both conditions.

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