src/java.base/share/classes/java/io/Console.java

Print this page




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.util.*;
  29 import java.nio.charset.Charset;


  30 import sun.nio.cs.StreamDecoder;
  31 import sun.nio.cs.StreamEncoder;
  32 
  33 /**
  34  * Methods to access the character-based console device, if any, associated
  35  * with the current Java virtual machine.
  36  *
  37  * <p> Whether a virtual machine has a console is dependent upon the
  38  * underlying platform and also upon the manner in which the virtual
  39  * machine is invoked.  If the virtual machine is started from an
  40  * interactive command line without redirecting the standard input and
  41  * output streams then its console will exist and will typically be
  42  * connected to the keyboard and display from which the virtual machine
  43  * was launched.  If the virtual machine is started automatically, for
  44  * example by a background job scheduler, then it will typically not
  45  * have a console.
  46  * <p>
  47  * If this virtual machine has a console then it is represented by a
  48  * unique instance of this class which can be obtained by invoking the
  49  * {@link java.lang.System#console()} method.  If no console device is


 502                            if (cbuf == rcb) {
 503                                 cbuf = grow();
 504                                 end = cbuf.length;
 505                            } else {
 506                                return off - offset;
 507                            }
 508                         }
 509                     }
 510                     if (eof)
 511                         return off - offset;
 512                 }
 513             }
 514         }
 515     }
 516 
 517     // Set up JavaIOAccess in SharedSecrets
 518     static {
 519         try {
 520             // Add a shutdown hook to restore console's echo state should
 521             // it be necessary.
 522             sun.misc.SharedSecrets.getJavaLangAccess()
 523                 .registerShutdownHook(0 /* shutdown hook invocation order */,
 524                     false /* only register if shutdown is not in progress */,
 525                     new Runnable() {
 526                         public void run() {
 527                             try {
 528                                 if (echoOff) {
 529                                     echo(true);
 530                                 }
 531                             } catch (IOException x) { }
 532                         }
 533                     });
 534         } catch (IllegalStateException e) {
 535             // shutdown is already in progress and console is first used
 536             // by a shutdown hook
 537         }
 538 
 539         sun.misc.SharedSecrets.setJavaIOAccess(new sun.misc.JavaIOAccess() {
 540             public Console console() {
 541                 if (istty()) {
 542                     if (cons == null)
 543                         cons = new Console();
 544                     return cons;
 545                 }
 546                 return null;
 547             }
 548 
 549             public Charset charset() {
 550                 // This method is called in sun.security.util.Password,
 551                 // cons already exists when this method is called
 552                 return cons.cs;
 553             }
 554         });
 555     }
 556     private static Console cons;
 557     private native static boolean istty();
 558     private Console() {
 559         readLock = new Object();




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.util.*;
  29 import java.nio.charset.Charset;
  30 import jdk.internal.misc.JavaIOAccess;
  31 import jdk.internal.misc.SharedSecrets;
  32 import sun.nio.cs.StreamDecoder;
  33 import sun.nio.cs.StreamEncoder;
  34 
  35 /**
  36  * Methods to access the character-based console device, if any, associated
  37  * with the current Java virtual machine.
  38  *
  39  * <p> Whether a virtual machine has a console is dependent upon the
  40  * underlying platform and also upon the manner in which the virtual
  41  * machine is invoked.  If the virtual machine is started from an
  42  * interactive command line without redirecting the standard input and
  43  * output streams then its console will exist and will typically be
  44  * connected to the keyboard and display from which the virtual machine
  45  * was launched.  If the virtual machine is started automatically, for
  46  * example by a background job scheduler, then it will typically not
  47  * have a console.
  48  * <p>
  49  * If this virtual machine has a console then it is represented by a
  50  * unique instance of this class which can be obtained by invoking the
  51  * {@link java.lang.System#console()} method.  If no console device is


 504                            if (cbuf == rcb) {
 505                                 cbuf = grow();
 506                                 end = cbuf.length;
 507                            } else {
 508                                return off - offset;
 509                            }
 510                         }
 511                     }
 512                     if (eof)
 513                         return off - offset;
 514                 }
 515             }
 516         }
 517     }
 518 
 519     // Set up JavaIOAccess in SharedSecrets
 520     static {
 521         try {
 522             // Add a shutdown hook to restore console's echo state should
 523             // it be necessary.
 524             SharedSecrets.getJavaLangAccess()
 525                 .registerShutdownHook(0 /* shutdown hook invocation order */,
 526                     false /* only register if shutdown is not in progress */,
 527                     new Runnable() {
 528                         public void run() {
 529                             try {
 530                                 if (echoOff) {
 531                                     echo(true);
 532                                 }
 533                             } catch (IOException x) { }
 534                         }
 535                     });
 536         } catch (IllegalStateException e) {
 537             // shutdown is already in progress and console is first used
 538             // by a shutdown hook
 539         }
 540 
 541         SharedSecrets.setJavaIOAccess(new JavaIOAccess() {
 542             public Console console() {
 543                 if (istty()) {
 544                     if (cons == null)
 545                         cons = new Console();
 546                     return cons;
 547                 }
 548                 return null;
 549             }
 550 
 551             public Charset charset() {
 552                 // This method is called in sun.security.util.Password,
 553                 // cons already exists when this method is called
 554                 return cons.cs;
 555             }
 556         });
 557     }
 558     private static Console cons;
 559     private native static boolean istty();
 560     private Console() {
 561         readLock = new Object();