1739 * @param value The value of the socket option. A value of {@code null} 1740 * may be valid for some options. 1741 * @return this Socket 1742 * 1743 * @throws UnsupportedOperationException if the socket does not support 1744 * the option. 1745 * 1746 * @throws IllegalArgumentException if the value is not valid for 1747 * the option. 1748 * 1749 * @throws IOException if an I/O error occurs, or if the socket is closed. 1750 * 1751 * @throws NullPointerException if name is {@code null} 1752 * 1753 * @throws SecurityException if a security manager is set and if the socket 1754 * option requires a security permission and if the caller does 1755 * not have the required permission. 1756 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1757 * do not require any security permission. 1758 * 1759 * @since 1.9 1760 */ 1761 public <T> Socket setOption(SocketOption<T> name, T value) throws IOException { 1762 getImpl().setOption(name, value); 1763 return this; 1764 } 1765 1766 /** 1767 * Returns the value of a socket option. 1768 * 1769 * @param <T> The type of the socket option value 1770 * @param name The socket option 1771 * 1772 * @return The value of the socket option. 1773 * 1774 * @throws UnsupportedOperationException if the socket does not support 1775 * the option. 1776 * 1777 * @throws IOException if an I/O error occurs, or if the socket is closed. 1778 * 1779 * @throws NullPointerException if name is {@code null} 1780 * 1781 * @throws SecurityException if a security manager is set and if the socket 1782 * option requires a security permission and if the caller does 1783 * not have the required permission. 1784 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1785 * do not require any security permission. 1786 * 1787 * @since 1.9 1788 */ 1789 @SuppressWarnings("unchecked") 1790 public <T> T getOption(SocketOption<T> name) throws IOException { 1791 return getImpl().getOption(name); 1792 } 1793 1794 private static Set<SocketOption<?>> options; 1795 private static boolean optionsSet = false; 1796 1797 /** 1798 * Returns a set of the socket options supported by this socket. 1799 * 1800 * This method will continue to return the set of options even after 1801 * the socket has been closed. 1802 * 1803 * @return A set of the socket options supported by this socket. This set 1804 * may be empty if the socket's SocketImpl cannot be created. 1805 * 1806 * @since 1.9 1807 */ 1808 public Set<SocketOption<?>> supportedOptions() { 1809 synchronized (Socket.class) { 1810 if (optionsSet) { 1811 return options; 1812 } 1813 try { 1814 SocketImpl impl = getImpl(); 1815 options = Collections.unmodifiableSet(impl.supportedOptions()); 1816 } catch (IOException e) { 1817 options = Collections.emptySet(); 1818 } 1819 optionsSet = true; 1820 return options; 1821 } 1822 } 1823 } | 1739 * @param value The value of the socket option. A value of {@code null} 1740 * may be valid for some options. 1741 * @return this Socket 1742 * 1743 * @throws UnsupportedOperationException if the socket does not support 1744 * the option. 1745 * 1746 * @throws IllegalArgumentException if the value is not valid for 1747 * the option. 1748 * 1749 * @throws IOException if an I/O error occurs, or if the socket is closed. 1750 * 1751 * @throws NullPointerException if name is {@code null} 1752 * 1753 * @throws SecurityException if a security manager is set and if the socket 1754 * option requires a security permission and if the caller does 1755 * not have the required permission. 1756 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1757 * do not require any security permission. 1758 * 1759 * @since 9 1760 */ 1761 public <T> Socket setOption(SocketOption<T> name, T value) throws IOException { 1762 getImpl().setOption(name, value); 1763 return this; 1764 } 1765 1766 /** 1767 * Returns the value of a socket option. 1768 * 1769 * @param <T> The type of the socket option value 1770 * @param name The socket option 1771 * 1772 * @return The value of the socket option. 1773 * 1774 * @throws UnsupportedOperationException if the socket does not support 1775 * the option. 1776 * 1777 * @throws IOException if an I/O error occurs, or if the socket is closed. 1778 * 1779 * @throws NullPointerException if name is {@code null} 1780 * 1781 * @throws SecurityException if a security manager is set and if the socket 1782 * option requires a security permission and if the caller does 1783 * not have the required permission. 1784 * {@link java.net.StandardSocketOptions StandardSocketOptions} 1785 * do not require any security permission. 1786 * 1787 * @since 9 1788 */ 1789 @SuppressWarnings("unchecked") 1790 public <T> T getOption(SocketOption<T> name) throws IOException { 1791 return getImpl().getOption(name); 1792 } 1793 1794 private static Set<SocketOption<?>> options; 1795 private static boolean optionsSet = false; 1796 1797 /** 1798 * Returns a set of the socket options supported by this socket. 1799 * 1800 * This method will continue to return the set of options even after 1801 * the socket has been closed. 1802 * 1803 * @return A set of the socket options supported by this socket. This set 1804 * may be empty if the socket's SocketImpl cannot be created. 1805 * 1806 * @since 9 1807 */ 1808 public Set<SocketOption<?>> supportedOptions() { 1809 synchronized (Socket.class) { 1810 if (optionsSet) { 1811 return options; 1812 } 1813 try { 1814 SocketImpl impl = getImpl(); 1815 options = Collections.unmodifiableSet(impl.supportedOptions()); 1816 } catch (IOException e) { 1817 options = Collections.emptySet(); 1818 } 1819 optionsSet = true; 1820 return options; 1821 } 1822 } 1823 } |