2013-02-21 70 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