Code example for DatagramChannel

Methods: close, register

0
 
	private SelectionKey obtainSelectorKey(final Selector selector)
	{ 
		try 
		{ 
			return channel.register(selector, 0);
		} 
		catch (final ClosedChannelException e)
		{ 
			closeSelector(selector, true);
			closeChannel(true); 
			throw new CanNeverHappenException(e);
		} 
	} 
 
	private void closeSelector(final Selector selector, final boolean inResponseToAnEarlierException)
	{ 
		try 
		{ 
			selector.close();
		} 
		catch (IOException e)
		{ 
			if (inResponseToAnEarlierException)
			{ 
				return; 
			} 
			throw new IllegalStateException(e);
		} 
	} 
 
	private DatagramChannel openChannel()
	{ 
		try 
		{ 
			return DatagramChannel.open();
		} 
		catch (final IOException exception)
		{ 
			throw new CanNeverHappenException(exception);
		} 
	} 
 
	private Selector openSelector()
	{ 
		try 
		{ 
			return Selector.open();
		} 
		catch(IOException exception)
		{ 
			closeChannel(true); 
			throw new CanNeverHappenException(exception);
		} 
	} 
 
	private void closeChannel(final boolean inResponseToAnEarlierException)
	{ 
		try 
		{ 
			channel.close();
		} 
		catch (IOException e)
		{ 
			if (inResponseToAnEarlierException)
			{