本文整理汇总了Java中java.nio.channels.AsynchronousSocketChannel.write方法的典型用法代码示例。如果您正苦于以下问题:Java AsynchronousSocketChannel.write方法的具体用法?Java AsynchronousSocketChannel.write怎么用?Java AsynchronousSocketChannel.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.AsynchronousSocketChannel
的用法示例。
在下文中一共展示了AsynchronousSocketChannel.write方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendByteBuffer
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
/**
*
* @param byteBuffer
* @param packetCount
* @param packets
*
* @author: tanyaowu
* @创建时间: 2017年2月4日 下午9:10:01
*
*/
public void sendByteBuffer(ByteBuffer byteBuffer, Integer packetCount, Object packets)
{
if (byteBuffer == null)
{
log.error("{},byteBuffer is null", channelContext);
return;
}
if (!AioUtils.checkBeforeIO(channelContext))
{
return;
}
byteBuffer.flip();
AsynchronousSocketChannel asynchronousSocketChannel = channelContext.getAsynchronousSocketChannel();
WriteCompletionHandler<SessionContext, P, R> writeCompletionHandler = channelContext.getWriteCompletionHandler();
try
{
writeCompletionHandler.getWriteSemaphore().acquire();
} catch (InterruptedException e)
{
log.error(e.toString(), e);
}
asynchronousSocketChannel.write(byteBuffer, packets, writeCompletionHandler);
channelContext.getStat().setLatestTimeOfSentPacket(SystemTimer.currentTimeMillis());
}
示例2: onOpen
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public static void onOpen(AsynchronousSocketChannel asynchronousSocketChannel) throws InterruptedException, ExecutionException, TimeoutException, NoSuchAlgorithmException {
String WebSocketsMagicString = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
Properties properties = new Properties();
StringBuilder sb = new StringBuilder();
while (inputBuffer.hasRemaining()) {
sb.append((char) (inputBuffer.get() & 0xff));
}
String[] lines = sb.toString().split("\\n");
for (String line : lines) {
String[] keyVal = line.split(":");
if (keyVal.length == 2) {
properties.put(keyVal[0].trim(), keyVal[1].trim());
}
}
String message
= "HTTP/1.1 101 Switching Protocols\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Accept: " + Base64.encodeBytes(MessageDigest.getInstance("SHA1").digest((properties.getProperty("Sec-WebSocket-Key") + WebSocketsMagicString).getBytes())) + "\r\n"
+ "Upgrade: websocket\r\n"
+ "\r\n";
outputBuffer = ByteBuffer.allocate(message.getBytes().length);
outputBuffer.put(message.getBytes());
outputBuffer.flip();
while (outputBuffer.hasRemaining()) {
asynchronousSocketChannel.write(outputBuffer);
}
outputBuffer = encodeUnmaskedFrame(1, "Connection Established");
outputBuffer.flip();
outputBuffer.rewind();
while (outputBuffer.hasRemaining()) {
asynchronousSocketChannel.write(outputBuffer);
}
}
示例3: onMessage
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public static void onMessage(AsynchronousSocketChannel asynchronousSocketChannel) throws Exception {
inputBuffer.flip();
inputBuffer.rewind();
String message = decodeMaskedFrame(inputBuffer);
outputBuffer = encodeUnmaskedFrame(1, message);
outputBuffer.flip();
outputBuffer.rewind();
while (outputBuffer.hasRemaining()) {
asynchronousSocketChannel.write(outputBuffer);
}
}
示例4: onClose
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public static void onClose(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
outputBuffer = encodeUnmaskedFrame(1, "Connection Closed");
outputBuffer.flip();
outputBuffer.rewind();
while (outputBuffer.hasRemaining()) {
asynchronousSocketChannel.write(outputBuffer);
}
ByteBuffer closeBuffer = encodeUnmaskedFrame(8, "");
closeBuffer.flip();
while (closeBuffer.hasRemaining()) {
asynchronousSocketChannel.write(closeBuffer);
}
connectionOpen = false;
asynchronousSocketChannel.close();
}
示例5: write
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
private static void write(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
asynchronousSocketChannel.write(outputBuffer);
outputBuffer.clear();
CharBuffer charBuffer = CharBuffer.allocate(1024);
for (;;) {
try {
charBuffer.put("HTTP/1.0 ").put("200 OK").put("\r\n");
charBuffer.put("Server: niossl/0.1").put("\r\n");
charBuffer.put("Content-type: ").put("text/html; charset=iso-8859-1").put("\r\n");
charBuffer.put("Content-length: ").put("31").put("\r\n");
charBuffer.put("\r\n");
charBuffer.put(request);
charBuffer.put("<html><head><title>HttpsServer</title></head><body><h3>HelloWorld!</h3></body></html>");
break;
} catch (BufferOverflowException x) {
charBuffer = CharBuffer.allocate(charBuffer.capacity() * 2);
}
}
charBuffer.flip();
SSLEngineResult sslEngineResult = sslEngine.wrap(ascii.encode(charBuffer), outputBuffer);
outputBuffer.flip();
switch (sslEngineResult.getStatus()) {
case OK:
if (sslEngineResult.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_TASK) {
Runnable runnable;
while ((runnable = sslEngine.getDelegatedTask()) != null) {
runnable.run();
}
}
break;
}
if (outputBuffer.hasRemaining()) {
asynchronousSocketChannel.write(outputBuffer);
}
}
示例6: main
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
AsynchronousSocketChannel sc = AsynchronousSocketChannel.open();
System.out.println(sc.getClass());
Future connected = sc.connect(new InetSocketAddress("localhost", 9999));
// later ensure we are connected.
connected.get();
ByteBuffer bb = ByteBuffer.allocateDirect(4096);
// populate bb
Future<Integer> writeFuture = sc.write(bb);
}
示例7: writeStuffThreadAsync
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
private void writeStuffThreadAsync(
AsynchronousServerSocketChannel socket, AsynchronousSocketChannel ch, int byteCount, int countDown) {
ByteBuffer buf = ByteBuffer.wrap("Fountain water!\n".getBytes());
ch.write(buf, 0, writeCompletionHandler(socket, ch, byteCount, buf, countDown));
}