本文整理汇总了Java中java.nio.channels.ByteChannel.write方法的典型用法代码示例。如果您正苦于以下问题:Java ByteChannel.write方法的具体用法?Java ByteChannel.write怎么用?Java ByteChannel.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.ByteChannel
的用法示例。
在下文中一共展示了ByteChannel.write方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeGetRequestTo
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
private void writeGetRequestTo(ByteChannel channel) throws IOException {
// @formatter:off
String request = new StringBuilder("GET").append(SPACE)
.append(this.url.getPath()).append(SPACE).append("HTTP/1.1")
.append(CRLF)
.append("User-Agent: NIOHttp/0.1 Java")
.append(CRLF)
.append("Host:").append(SPACE).append(url.getHost())
.append(CRLF)
.append("Accept-Language: en-us").append(CRLF)
.append("Connection: close").append(CRLF).append(CRLF)
.toString();
// @formatter:on
ByteBuffer sending = ByteBuffer.allocate(request.getBytes().length);
sending.put(request.getBytes());
sending.flip();
channel.write(sending);
}
示例2: batch
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/** Returns whether the whole outQueue has been flushed */
public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws IOException {
ByteBuffer buffer = ws.outQueue.peek();
WrappedByteChannel c = null;
if( buffer == null ) {
if( sockchannel instanceof WrappedByteChannel ) {
c = (WrappedByteChannel) sockchannel;
if( c.isNeedWrite() ) {
c.writeMore();
}
}
} else {
do {// FIXME writing as much as possible is unfair!!
/*int written = */sockchannel.write( buffer );
if( buffer.remaining() > 0 ) {
return false;
} else {
ws.outQueue.poll(); // Buffer finished. Remove it.
buffer = ws.outQueue.peek();
}
} while ( buffer != null );
}
if( ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft().getRole() == Role.SERVER ) {//
synchronized ( ws ) {
ws.closeConnection();
}
}
return c != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
}
示例3: batch
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
* Returns whether the whole outQueue has been flushed
*/
public static boolean batch(WebSocketImpl ws, ByteChannel sockchannel) throws IOException {
ByteBuffer buffer = ws.outQueue.peek();
WrappedByteChannel c = null;
if (buffer == null) {
if (sockchannel instanceof WrappedByteChannel) {
c = (WrappedByteChannel) sockchannel;
if (c.isNeedWrite()) {
c.writeMore();
}
}
} else {
do {// FIXME writing as much as possible is unfair!!
/*int written = */
sockchannel.write(buffer);
if (buffer.remaining() > 0) {
return false;
} else {
ws.outQueue.poll(); // Buffer finished. Remove it.
buffer = ws.outQueue.peek();
}
} while (buffer != null);
}
if (ws != null && ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft() != null && ws.getDraft().getRole() != null && ws.getDraft().getRole() == Role.SERVER) {//
synchronized (ws) {
ws.closeConnection();
}
}
return c != null ? !((WrappedByteChannel) sockchannel).isNeedWrite() : true;
}
示例4: writeBlocking
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
public static void writeBlocking(WebSocketImpl ws, ByteChannel channel) throws InterruptedException, IOException {
if (!$assertionsDisabled && (channel instanceof AbstractSelectableChannel) && !((AbstractSelectableChannel) channel).isBlocking()) {
throw new AssertionError();
} else if ($assertionsDisabled || !(channel instanceof WrappedByteChannel) || ((WrappedByteChannel) channel).isBlocking()) {
ByteBuffer buf = (ByteBuffer) ws.outQueue.take();
while (buf.hasRemaining()) {
channel.write(buf);
}
} else {
throw new AssertionError();
}
}
示例5: writeGetRequestToChannel
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
private void writeGetRequestToChannel(ByteChannel channel) throws IOException {
Request request = new HttpRequestBuilder(this.url).closeConnection()
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1").httpGet();
sysout(request.getBytes(), "HTTP Request");
ByteBuffer sending = ByteBuffer.allocate(request.getBytes().length);
sending.put(request.getBytes());
sending.flip();
channel.write(sending);
}
示例6: batch
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/** Returns whether the whole outQueue has been flushed
* @param ws The WebSocketImpl associated with the channels
* @param sockchannel The channel to write to
* @throws IOException May be thrown by {@link WrappedByteChannel#writeMore()}
* @return returns Whether there is more data to write
*/
public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws IOException {
ByteBuffer buffer = ws.outQueue.peek();
WrappedByteChannel c = null;
if( buffer == null ) {
if( sockchannel instanceof WrappedByteChannel ) {
c = (WrappedByteChannel) sockchannel;
if( c.isNeedWrite() ) {
c.writeMore();
}
}
} else {
do {// FIXME writing as much as possible is unfair!!
/*int written = */sockchannel.write( buffer );
if( buffer.remaining() > 0 ) {
return false;
} else {
ws.outQueue.poll(); // Buffer finished. Remove it.
buffer = ws.outQueue.peek();
}
} while ( buffer != null );
}
if( ws != null && ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft() != null && ws.getDraft().getRole() != null && ws.getDraft().getRole() == Role.SERVER ) {//
synchronized ( ws ) {
ws.closeConnection();
}
}
return c == null || !((WrappedByteChannel) sockchannel).isNeedWrite();
}
示例7: write
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/**
* ByteChannelへ書き込む
* @param channel
* @param value
* @param work
* @throws IOException
*/
public static void write(@NonNull final ByteChannel channel,
final boolean value,
@Nullable final ByteBuffer work) throws IOException {
final ByteBuffer buf = checkBuffer(work, 1);
buf.put((byte)(value ? 1 : 0));
buf.flip();
channel.write(buf);
}
示例8: writeData
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
@Test
public void writeData() throws Exception {
this.server.expect("hello".getBytes());
this.server.start();
ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT);
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer);
this.server.closeAndVerify();
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:10,代码来源:SocketTargetServerConnectionTests.java
示例9: batch
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/** Returns whether the whole outQueue has been flushed */
public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws IOException {
ByteBuffer buffer = ws.outQueue.peek();
WrappedByteChannel c = null;
if( buffer == null ) {
if( sockchannel instanceof WrappedByteChannel ) {
c = (WrappedByteChannel) sockchannel;
if( c.isNeedWrite() ) {
c.writeMore();
}
}
} else {
do {// FIXME writing as much as possible is unfair!!
/*int written = */sockchannel.write( buffer );
if( buffer.remaining() > 0 ) {
return false;
} else {
ws.outQueue.poll(); // Buffer finished. Remove it.
buffer = ws.outQueue.peek();
}
} while ( buffer != null );
}
if( ws != null && ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft() != null && ws.getDraft().getRole() != null && ws.getDraft().getRole() == Role.SERVER ) {//
synchronized ( ws ) {
ws.closeConnection();
}
}
return c != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
}
示例10: batch
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
public static boolean batch(WebSocketImplementation ws, ByteChannel sockchannel) throws IOException {
ByteBuffer buffer = ws.outQueue.peek();
WrappedByteChannel c = null;
if(buffer == null) {
if(sockchannel instanceof WrappedByteChannel) {
c = (WrappedByteChannel) sockchannel;
if(c.isNeedWrite()) {
c.writeMore();
}
}
} else {
do {
sockchannel.write(buffer);
if(buffer.remaining() > 0) {
return false;
} else {
ws.outQueue.poll();
buffer = ws.outQueue.peek();
}
} while (buffer != null);
}
if(ws != null && ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft() != null && ws.getDraft().getRole() != null && ws.getDraft().getRole() == Role.SERVER) {//
synchronized (ws) {
ws.closeConnection();
}
}
return c != null ? !((WrappedByteChannel) sockchannel).isNeedWrite() : true;
}
示例11: batch
import java.nio.channels.ByteChannel; //导入方法依赖的package包/类
/** Returns whether the whole outQueue has been flushed */
public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws IOException {
ByteBuffer buffer = ws.outQueue.peek();
if( buffer == null ) {
if( sockchannel instanceof WrappedByteChannel ) {
WrappedByteChannel c = (WrappedByteChannel) sockchannel;
if( c.isNeedWrite() ) {
c.writeMore();
return !c.isNeedWrite();
}
return true;
}
} else {
do {// FIXME writing as much as possible is unfair!!
/*int written = */sockchannel.write( buffer );
if( buffer.remaining() > 0 ) {
return false;
} else {
ws.outQueue.poll(); // Buffer finished. Remove it.
buffer = ws.outQueue.peek();
}
} while ( buffer != null );
}
if( ws.isClosed() ) {
synchronized ( ws ) {
sockchannel.close();
}
}
return sockchannel instanceof WrappedByteChannel == true ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
}