1321 * may be valid for some options. 1322 * 1323 * @return this DatagramSocket 1324 * 1325 * @throws UnsupportedOperationException if the datagram socket 1326 * does not support the option. 1327 * 1328 * @throws IllegalArgumentException if the value is not valid for 1329 * the option. 1330 * 1331 * @throws IOException if an I/O error occurs, or if the socket is closed. 1332 * 1333 * @throws SecurityException if a security manager is set and if the socket 1334 * option requires a security permission and if the caller does 1335 * not have the required permission. 1336 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1337 * do not require any security permission. 1338 * 1339 * @throws NullPointerException if name is {@code null} 1340 * 1341 * @since 1.9 1342 */ 1343 public <T> DatagramSocket setOption(SocketOption<T> name, T value) 1344 throws IOException 1345 { 1346 getImpl().setOption(name, value); 1347 return this; 1348 } 1349 1350 /** 1351 * Returns the value of a socket option. 1352 * 1353 * @param <T> The type of the socket option value 1354 * @param name The socket option 1355 * 1356 * @return The value of the socket option. 1357 * 1358 * @throws UnsupportedOperationException if the datagram socket 1359 * does not support the option. 1360 * 1361 * @throws IOException if an I/O error occurs, or if the socket is closed. 1362 * 1363 * @throws NullPointerException if name is {@code null} 1364 * 1365 * @throws SecurityException if a security manager is set and if the socket 1366 * option requires a security permission and if the caller does 1367 * not have the required permission. 1368 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1369 * do not require any security permission. 1370 * 1371 * @since 1.9 1372 */ 1373 public <T> T getOption(SocketOption<T> name) throws IOException { 1374 return getImpl().getOption(name); 1375 } 1376 1377 private static Set<SocketOption<?>> options; 1378 private static boolean optionsSet = false; 1379 1380 /** 1381 * Returns a set of the socket options supported by this socket. 1382 * 1383 * This method will continue to return the set of options even after 1384 * the socket has been closed. 1385 * 1386 * @return A set of the socket options supported by this socket. This set 1387 * may be empty if the socket's DatagramSocketImpl cannot be created. 1388 * 1389 * @since 1.9 1390 */ 1391 public Set<SocketOption<?>> supportedOptions() { 1392 synchronized(DatagramSocket.class) { 1393 if (optionsSet) { 1394 return options; 1395 } 1396 try { 1397 DatagramSocketImpl impl = getImpl(); 1398 options = Collections.unmodifiableSet(impl.supportedOptions()); 1399 } catch (IOException e) { 1400 options = Collections.emptySet(); 1401 } 1402 optionsSet = true; 1403 return options; 1404 } 1405 } 1406 } | 1321 * may be valid for some options. 1322 * 1323 * @return this DatagramSocket 1324 * 1325 * @throws UnsupportedOperationException if the datagram socket 1326 * does not support the option. 1327 * 1328 * @throws IllegalArgumentException if the value is not valid for 1329 * the option. 1330 * 1331 * @throws IOException if an I/O error occurs, or if the socket is closed. 1332 * 1333 * @throws SecurityException if a security manager is set and if the socket 1334 * option requires a security permission and if the caller does 1335 * not have the required permission. 1336 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1337 * do not require any security permission. 1338 * 1339 * @throws NullPointerException if name is {@code null} 1340 * 1341 * @since 9 1342 */ 1343 public <T> DatagramSocket setOption(SocketOption<T> name, T value) 1344 throws IOException 1345 { 1346 getImpl().setOption(name, value); 1347 return this; 1348 } 1349 1350 /** 1351 * Returns the value of a socket option. 1352 * 1353 * @param <T> The type of the socket option value 1354 * @param name The socket option 1355 * 1356 * @return The value of the socket option. 1357 * 1358 * @throws UnsupportedOperationException if the datagram socket 1359 * does not support the option. 1360 * 1361 * @throws IOException if an I/O error occurs, or if the socket is closed. 1362 * 1363 * @throws NullPointerException if name is {@code null} 1364 * 1365 * @throws SecurityException if a security manager is set and if the socket 1366 * option requires a security permission and if the caller does 1367 * not have the required permission. 1368 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1369 * do not require any security permission. 1370 * 1371 * @since 9 1372 */ 1373 public <T> T getOption(SocketOption<T> name) throws IOException { 1374 return getImpl().getOption(name); 1375 } 1376 1377 private static Set<SocketOption<?>> options; 1378 private static boolean optionsSet = false; 1379 1380 /** 1381 * Returns a set of the socket options supported by this socket. 1382 * 1383 * This method will continue to return the set of options even after 1384 * the socket has been closed. 1385 * 1386 * @return A set of the socket options supported by this socket. This set 1387 * may be empty if the socket's DatagramSocketImpl cannot be created. 1388 * 1389 * @since 9 1390 */ 1391 public Set<SocketOption<?>> supportedOptions() { 1392 synchronized(DatagramSocket.class) { 1393 if (optionsSet) { 1394 return options; 1395 } 1396 try { 1397 DatagramSocketImpl impl = getImpl(); 1398 options = Collections.unmodifiableSet(impl.supportedOptions()); 1399 } catch (IOException e) { 1400 options = Collections.emptySet(); 1401 } 1402 optionsSet = true; 1403 return options; 1404 } 1405 } 1406 } |