All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.craft.atom.nio.NioUdpByteChannel Maven / Gradle / Ivy

There is a newer version: 3.1.2
Show newest version
package io.craft.atom.nio;

import io.craft.atom.nio.spi.NioBufferSizePredictor;
import io.craft.atom.nio.spi.NioChannelEventDispatcher;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;

import lombok.ToString;


/**
 * A nio channel for datagram-oriented sockets.
 * 
 * @author mindwind
 * @version 1.0, Feb 22, 2013
 */
@ToString(callSuper = true)
public class NioUdpByteChannel extends NioByteChannel {
	
	
	private DatagramChannel datagramChannel; 
	

	public NioUdpByteChannel(DatagramChannel datagramChannel, NioConfig config, NioBufferSizePredictor predictor, NioChannelEventDispatcher dispatcher) {
		super(config, predictor, dispatcher);
		
		if (datagramChannel == null) {
			throw new IllegalArgumentException("DatagramChannel can not be null.");
		}
		
		this.datagramChannel = datagramChannel;
		this.localAddress = datagramChannel.socket().getLocalSocketAddress();
	}

	@Override
	protected SocketAddress readUdp(ByteBuffer buf) throws IOException {
		return datagramChannel.receive(buf);
	}

	@Override
	protected int writeUdp(ByteBuffer buf, SocketAddress target) throws IOException {
		return datagramChannel.send(buf, target);
	}

	@Override
	protected SelectableChannel innerChannel() {
		return datagramChannel;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy