2013-02-21 66 views
1

我正在制作一个程序,需要将加密的数据从一台PC发送到另一台。我不想每次都明确地加密/解密数据,然后通过使用AsynchronousSocketChannel发送数据,我想知道我是否可以以某种方式扩展AsynchronousSocketChannel.write和AsynchronousSocketChannel.read的功能来为我做到这一点。但似乎AsynchronousSocketChannel.write是最后一种方法。有没有什么方法可以制作我自己的AsynchronousSocketChannel,还是这反直觉?制作我自己的AsynchronousSocketChannel

非常感谢提前。

回答

1

您可以在自己的类包起来:

import java.nio.ByteBuffer; 
import java.nio.channels.AsynchronousSocketChannel; 
import java.util.concurrent.Future; 

public class EncryptedAsynchronousSocketChannel { 
    private AsynchronousSocketChannel channel; 
    public Future<Integer> read(ByteBuffer dst){ 
     return channel.read(dst); 
    } 
    public Future<Integer> write(ByteBuffer src){ 
     return channel.write(src); 
    } 
} 
+0

呀,但我真的不能采取在AsynchronousSocketChannel已经编码方法的优势。像这样的构图是(容易)做到这一点的唯一方式? – Alex 2013-02-21 02:20:16

+0

只是调用在私有实例中调用同名方法的方法,如果需要修改参数。在java中,这是我知道的唯一方法 – Navin 2013-02-21 02:21:38

+0

当人们尝试改进最终的String类时,这是一个相对常见的问题。另一种方法是提取rt.jar并修改该类以使其虚拟化,但我假设您不想修改Java库。 – Navin 2013-02-21 02:24:22