src/java.base/share/classes/java/net/SocketOutputStream.java
changeset 54689 b28b7f631301
parent 52499 768b1c612100
child 58242 94bb65cb37d3
equal deleted inserted replaced
54688:96ad739cfc39 54689:b28b7f631301
     1 /*
     1 /*
     2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    43         init();
    43         init();
    44     }
    44     }
    45 
    45 
    46     private AbstractPlainSocketImpl impl = null;
    46     private AbstractPlainSocketImpl impl = null;
    47     private byte temp[] = new byte[1];
    47     private byte temp[] = new byte[1];
    48     private Socket socket = null;
       
    49 
    48 
    50     /**
    49     /**
    51      * Creates a new SocketOutputStream. Can only be called
    50      * Creates a new SocketOutputStream. Can only be called
    52      * by a Socket. This method needs to hang on to the owner Socket so
    51      * by a Socket. This method needs to hang on to the owner Socket so
    53      * that the fd will not be closed.
    52      * that the fd will not be closed.
    54      * @param impl the socket output stream implemented
    53      * @param impl the socket output stream implemented
    55      */
    54      */
    56     SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException {
    55     SocketOutputStream(AbstractPlainSocketImpl impl) throws IOException {
    57         super(impl.getFileDescriptor());
    56         super(impl.getFileDescriptor());
    58         this.impl = impl;
    57         this.impl = impl;
    59         socket = impl.getSocket();
       
    60     }
    58     }
    61 
    59 
    62     /**
    60     /**
    63      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
    61      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
    64      * object associated with this file output stream. </p>
    62      * object associated with this file output stream. </p>
   148      */
   146      */
   149     public void write(byte b[], int off, int len) throws IOException {
   147     public void write(byte b[], int off, int len) throws IOException {
   150         socketWrite(b, off, len);
   148         socketWrite(b, off, len);
   151     }
   149     }
   152 
   150 
   153     /**
       
   154      * Closes the stream.
       
   155      */
       
   156     private boolean closing = false;
       
   157     public void close() throws IOException {
   151     public void close() throws IOException {
   158         // Prevent recursion. See BugId 4484411
   152         // No longer used. Socket.getOutputStream returns an
   159         if (closing)
   153         // OutputStream which calls Socket.close directly
   160             return;
   154         assert false;
   161         closing = true;
       
   162         if (socket != null) {
       
   163             if (!socket.isClosed())
       
   164                 socket.close();
       
   165         } else
       
   166             impl.close();
       
   167         closing = false;
       
   168     }
   155     }
   169 
   156 
   170     /**
   157     /**
   171      * Overrides finalize, the fd is closed by the Socket.
   158      * Overrides finalize, the fd is closed by the Socket.
   172      */
   159      */