Java Code Examples for java.nio.channels.ByteChannel#write()

The following examples show how to use java.nio.channels.ByteChannel#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SocketChannelIOHelper.java    From Slyther with MIT License 5 votes vote down vote up
/** 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;
}
 
Example 2
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final ByteBuffer value) throws IOException {
	
	write(channel, value.remaining());
	channel.write(value);
}
 
Example 3
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final double[] value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final int n = value.length;
	write(channel, n, work);
	final ByteBuffer buf = checkBuffer(work, n * 8);
	for (int i = 0; i < n; i++) {
		buf.putDouble(value[i]);
	}
	buf.flip();
	channel.write(buf);
}
 
Example 4
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final float[] value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final int n = value.length;
	write(channel, n, work);
	final ByteBuffer buf = checkBuffer(work, n * 4);
	for (int i = 0; i < n; i++) {
		buf.putFloat(value[i]);
	}
	buf.flip();
	channel.write(buf);
}
 
Example 5
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final long[] value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final int n = value.length;
	write(channel, n, work);
	final ByteBuffer buf = checkBuffer(work, n * 8);
	for (int i = 0; i < n; i++) {
		buf.putLong(value[i]);
	}
	buf.flip();
	channel.write(buf);
}
 
Example 6
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final int[] value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final int n = value.length;
	write(channel, n, work);
	final ByteBuffer buf = checkBuffer(work, n * 4);
	for (int i = 0; i < n; i++) {
		buf.putInt(value[i]);
	}
	buf.flip();
	channel.write(buf);
}
 
Example 7
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final short[] value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final int n = value.length;
	write(channel, n, work);
	final ByteBuffer buf = checkBuffer(work, n * 2);
	for (int i = 0; i < n; i++) {
		buf.putShort(value[i]);
	}
	buf.flip();
	channel.write(buf);
}
 
Example 8
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final char[] value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final int n = value.length;
	write(channel, n, work);
	final ByteBuffer buf = checkBuffer(work, n * 2);
	for (int i = 0; i < n; i++) {
		buf.putChar(value[i]);
	}
	buf.flip();
	channel.write(buf);
}
 
Example 9
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	@NonNull final byte[] value) throws IOException {
	
	write(channel, value.length);
	channel.write(ByteBuffer.wrap(value));
}
 
Example 10
Source File: SocketChannelIOHelper.java    From clevertap-android-sdk with MIT License 5 votes vote down vote up
/** 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 {
	if (ws == null) {
		return false;
	}
	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() != null && ws.getDraft().getRole() != null && ws.getDraft().getRole() == Role.SERVER ) {//
		ws.closeConnection();
	}
	return c == null || !((WrappedByteChannel) sockchannel).isNeedWrite();
}
 
Example 11
Source File: SocketChannelIOHelper.java    From ans-android-sdk with GNU General Public License v3.0 5 votes vote down vote up
/** 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();
}
 
Example 12
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	final double value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 8);
	buf.putDouble(value);
	buf.flip();
	channel.write(buf);
}
 
Example 13
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	final float value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 4);
	buf.putFloat(value);
	buf.flip();
	channel.write(buf);
}
 
Example 14
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	final long value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 8);
	buf.putLong(value);
	buf.flip();
	channel.write(buf);
}
 
Example 15
Source File: SocketChannelIOHelper.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
/** 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();
}
 
Example 16
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	final short value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 2);
	buf.putShort(value);
	buf.flip();
	channel.write(buf);
}
 
Example 17
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	final char value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 2);
	buf.putChar(value);
	buf.flip();
	channel.write(buf);
}
 
Example 18
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * ByteChannelへ書き込む
 * @param channel
 * @param value
 * @param work
 * @throws IOException
 */
public static void write(@NonNull final ByteChannel channel,
	final byte value,
	@Nullable final ByteBuffer work) throws IOException {
	
	final ByteBuffer buf = checkBuffer(work, 1);
	buf.put(value);
	buf.flip();
	channel.write(buf);
}
 
Example 19
Source File: ChannelHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: SocketChannelIOHelper.java    From alipay-sdk-java-all with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the whole outQueue has been flushed
 *
 * @param ws          The WebSocketImpl associated with the channels
 * @param sockchannel The channel to write to
 * @return returns Whether there is more data to write
 * @throws IOException May be thrown by {@link WrappedByteChannel#writeMore()}
 */
public static boolean batch(WebSocketImpl ws, ByteChannel sockchannel) throws IOException {
    if (ws == null) {
        return false;
    }
    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() != null && ws.getDraft().getRole() != null
            && ws.getDraft().getRole() == Role.SERVER) {//
        ws.closeConnection();
    }
    return c == null || !((WrappedByteChannel) sockchannel).isNeedWrite();
}