< prev index next >

src/java.base/share/classes/java/net/Socket.java

Print this page




 453      * @since 1.4
 454      */
 455      void createImpl(boolean stream) throws SocketException {
 456         if (impl == null)
 457             setImpl();
 458         try {
 459             impl.create(stream);
 460             created = true;
 461         } catch (IOException e) {
 462             throw new SocketException(e.getMessage());
 463         }
 464     }
 465 
 466     private void checkOldImpl() {
 467         if (impl == null)
 468             return;
 469         // SocketImpl.connect() is a protected method, therefore we need to use
 470         // getDeclaredMethod, therefore we need permission to access the member
 471 
 472         oldImpl = AccessController.doPrivileged
 473                                 (new PrivilegedAction<Boolean>() {
 474             public Boolean run() {
 475                 Class<?> clazz = impl.getClass();
 476                 while (true) {
 477                     try {
 478                         clazz.getDeclaredMethod("connect", SocketAddress.class, int.class);
 479                         return Boolean.FALSE;
 480                     } catch (NoSuchMethodException e) {
 481                         clazz = clazz.getSuperclass();
 482                         // java.net.SocketImpl class will always have this abstract method.
 483                         // If we have not found it by now in the hierarchy then it does not
 484                         // exist, we are an old style impl.
 485                         if (clazz.equals(java.net.SocketImpl.class)) {
 486                             return Boolean.TRUE;
 487                         }
 488                     }
 489                 }
 490             }
 491         });
 492     }
 493 


 894      * @return     an input stream for reading bytes from this socket.
 895      * @exception  IOException  if an I/O error occurs when creating the
 896      *             input stream, the socket is closed, the socket is
 897      *             not connected, or the socket input has been shutdown
 898      *             using {@link #shutdownInput()}
 899      *
 900      * @revised 1.4
 901      * @spec JSR-51
 902      */
 903     public InputStream getInputStream() throws IOException {
 904         if (isClosed())
 905             throw new SocketException("Socket is closed");
 906         if (!isConnected())
 907             throw new SocketException("Socket is not connected");
 908         if (isInputShutdown())
 909             throw new SocketException("Socket input is shutdown");
 910         final Socket s = this;
 911         InputStream is = null;
 912         try {
 913             is = AccessController.doPrivileged(
 914                 new PrivilegedExceptionAction<InputStream>() {
 915                     public InputStream run() throws IOException {
 916                         return impl.getInputStream();
 917                     }
 918                 });
 919         } catch (java.security.PrivilegedActionException e) {
 920             throw (IOException) e.getException();
 921         }
 922         return is;
 923     }
 924 
 925     /**
 926      * Returns an output stream for this socket.
 927      *
 928      * <p> If this socket has an associated channel then the resulting output
 929      * stream delegates all of its operations to the channel.  If the channel
 930      * is in non-blocking mode then the output stream's {@code write}
 931      * operations will throw an {@link
 932      * java.nio.channels.IllegalBlockingModeException}.
 933      *
 934      * <p> Closing the returned {@link java.io.OutputStream OutputStream}
 935      * will close the associated socket.
 936      *
 937      * @return     an output stream for writing bytes to this socket.
 938      * @exception  IOException  if an I/O error occurs when creating the
 939      *               output stream or if the socket is not connected.
 940      * @revised 1.4
 941      * @spec JSR-51
 942      */
 943     public OutputStream getOutputStream() throws IOException {
 944         if (isClosed())
 945             throw new SocketException("Socket is closed");
 946         if (!isConnected())
 947             throw new SocketException("Socket is not connected");
 948         if (isOutputShutdown())
 949             throw new SocketException("Socket output is shutdown");
 950         final Socket s = this;
 951         OutputStream os = null;
 952         try {
 953             os = AccessController.doPrivileged(
 954                 new PrivilegedExceptionAction<OutputStream>() {
 955                     public OutputStream run() throws IOException {
 956                         return impl.getOutputStream();
 957                     }
 958                 });
 959         } catch (java.security.PrivilegedActionException e) {
 960             throw (IOException) e.getException();
 961         }
 962         return os;
 963     }
 964 
 965     /**
 966      * Enable/disable {@link SocketOptions#TCP_NODELAY TCP_NODELAY}
 967      * (disable/enable Nagle's algorithm).
 968      *
 969      * @param on {@code true} to enable TCP_NODELAY,
 970      * {@code false} to disable.
 971      *
 972      * @exception SocketException if there is an error
 973      * in the underlying protocol, such as a TCP error.
 974      *




 453      * @since 1.4
 454      */
 455      void createImpl(boolean stream) throws SocketException {
 456         if (impl == null)
 457             setImpl();
 458         try {
 459             impl.create(stream);
 460             created = true;
 461         } catch (IOException e) {
 462             throw new SocketException(e.getMessage());
 463         }
 464     }
 465 
 466     private void checkOldImpl() {
 467         if (impl == null)
 468             return;
 469         // SocketImpl.connect() is a protected method, therefore we need to use
 470         // getDeclaredMethod, therefore we need permission to access the member
 471 
 472         oldImpl = AccessController.doPrivileged
 473                                 (new PrivilegedAction<>() {
 474             public Boolean run() {
 475                 Class<?> clazz = impl.getClass();
 476                 while (true) {
 477                     try {
 478                         clazz.getDeclaredMethod("connect", SocketAddress.class, int.class);
 479                         return Boolean.FALSE;
 480                     } catch (NoSuchMethodException e) {
 481                         clazz = clazz.getSuperclass();
 482                         // java.net.SocketImpl class will always have this abstract method.
 483                         // If we have not found it by now in the hierarchy then it does not
 484                         // exist, we are an old style impl.
 485                         if (clazz.equals(java.net.SocketImpl.class)) {
 486                             return Boolean.TRUE;
 487                         }
 488                     }
 489                 }
 490             }
 491         });
 492     }
 493 


 894      * @return     an input stream for reading bytes from this socket.
 895      * @exception  IOException  if an I/O error occurs when creating the
 896      *             input stream, the socket is closed, the socket is
 897      *             not connected, or the socket input has been shutdown
 898      *             using {@link #shutdownInput()}
 899      *
 900      * @revised 1.4
 901      * @spec JSR-51
 902      */
 903     public InputStream getInputStream() throws IOException {
 904         if (isClosed())
 905             throw new SocketException("Socket is closed");
 906         if (!isConnected())
 907             throw new SocketException("Socket is not connected");
 908         if (isInputShutdown())
 909             throw new SocketException("Socket input is shutdown");
 910         final Socket s = this;
 911         InputStream is = null;
 912         try {
 913             is = AccessController.doPrivileged(
 914                 new PrivilegedExceptionAction<>() {
 915                     public InputStream run() throws IOException {
 916                         return impl.getInputStream();
 917                     }
 918                 });
 919         } catch (java.security.PrivilegedActionException e) {
 920             throw (IOException) e.getException();
 921         }
 922         return is;
 923     }
 924 
 925     /**
 926      * Returns an output stream for this socket.
 927      *
 928      * <p> If this socket has an associated channel then the resulting output
 929      * stream delegates all of its operations to the channel.  If the channel
 930      * is in non-blocking mode then the output stream's {@code write}
 931      * operations will throw an {@link
 932      * java.nio.channels.IllegalBlockingModeException}.
 933      *
 934      * <p> Closing the returned {@link java.io.OutputStream OutputStream}
 935      * will close the associated socket.
 936      *
 937      * @return     an output stream for writing bytes to this socket.
 938      * @exception  IOException  if an I/O error occurs when creating the
 939      *               output stream or if the socket is not connected.
 940      * @revised 1.4
 941      * @spec JSR-51
 942      */
 943     public OutputStream getOutputStream() throws IOException {
 944         if (isClosed())
 945             throw new SocketException("Socket is closed");
 946         if (!isConnected())
 947             throw new SocketException("Socket is not connected");
 948         if (isOutputShutdown())
 949             throw new SocketException("Socket output is shutdown");
 950         final Socket s = this;
 951         OutputStream os = null;
 952         try {
 953             os = AccessController.doPrivileged(
 954                 new PrivilegedExceptionAction<>() {
 955                     public OutputStream run() throws IOException {
 956                         return impl.getOutputStream();
 957                     }
 958                 });
 959         } catch (java.security.PrivilegedActionException e) {
 960             throw (IOException) e.getException();
 961         }
 962         return os;
 963     }
 964 
 965     /**
 966      * Enable/disable {@link SocketOptions#TCP_NODELAY TCP_NODELAY}
 967      * (disable/enable Nagle's algorithm).
 968      *
 969      * @param on {@code true} to enable TCP_NODELAY,
 970      * {@code false} to disable.
 971      *
 972      * @exception SocketException if there is an error
 973      * in the underlying protocol, such as a TCP error.
 974      *


< prev index next >