< prev index next >

src/java.base/share/classes/java/lang/Process.java

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 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.lang;
 27 

 28 import jdk.internal.util.StaticProperty;
 29 
 30 import java.io.*;
 31 import java.lang.ProcessBuilder.Redirect;
 32 import java.nio.charset.Charset;
 33 import java.nio.charset.UnsupportedCharsetException;
 34 import java.util.Objects;
 35 import java.util.concurrent.CompletableFuture;
 36 import java.util.concurrent.ForkJoinPool;
 37 import java.util.concurrent.TimeUnit;
 38 import java.util.stream.Stream;
 39 
 40 /**
 41  * {@code Process} provides control of native processes started by
 42  * ProcessBuilder.start and Runtime.exec.
 43  * The class provides methods for performing input from the process, performing
 44  * output to the process, waiting for the process to complete,
 45  * checking the exit status of the process, and destroying (killing)
 46  * the process.
 47  * The {@link ProcessBuilder#start()} and

822         public long skip(long n) throws IOException {
823             long remaining = n;
824             int nr;
825 
826             if (n <= 0) {
827                 return 0;
828             }
829 
830             int size = (int)Math.min(2048, remaining);
831             byte[] skipBuffer = new byte[size];
832             while (remaining > 0) {
833                 nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
834                 if (nr < 0) {
835                     break;
836                 }
837                 remaining -= nr;
838             }
839 
840             return n - remaining;
841         }





































































842     }
843 
844     /**
845      * A nested class to delay looking up the Charset for the native encoding.
846      */
847     private static class CharsetHolder {
848         private static final Charset nativeCharset;
849         static {
850             Charset cs;
851             try {
852                 cs = Charset.forName(StaticProperty.nativeEncoding());
853             } catch (UnsupportedCharsetException uce) {
854                 cs = Charset.defaultCharset();
855             }
856             nativeCharset = cs;
857         }
858 
859         /**
860          * Charset for the native encoding or {@link Charset#defaultCharset().
861          */

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 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.lang;
 27 
 28 import jdk.internal.misc.Blocker;
 29 import jdk.internal.util.StaticProperty;
 30 
 31 import java.io.*;
 32 import java.lang.ProcessBuilder.Redirect;
 33 import java.nio.charset.Charset;
 34 import java.nio.charset.UnsupportedCharsetException;
 35 import java.util.Objects;
 36 import java.util.concurrent.CompletableFuture;
 37 import java.util.concurrent.ForkJoinPool;
 38 import java.util.concurrent.TimeUnit;
 39 import java.util.stream.Stream;
 40 
 41 /**
 42  * {@code Process} provides control of native processes started by
 43  * ProcessBuilder.start and Runtime.exec.
 44  * The class provides methods for performing input from the process, performing
 45  * output to the process, waiting for the process to complete,
 46  * checking the exit status of the process, and destroying (killing)
 47  * the process.
 48  * The {@link ProcessBuilder#start()} and

823         public long skip(long n) throws IOException {
824             long remaining = n;
825             int nr;
826 
827             if (n <= 0) {
828                 return 0;
829             }
830 
831             int size = (int)Math.min(2048, remaining);
832             byte[] skipBuffer = new byte[size];
833             while (remaining > 0) {
834                 nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
835                 if (nr < 0) {
836                     break;
837                 }
838                 remaining -= nr;
839             }
840 
841             return n - remaining;
842         }
843 
844         @Override
845         public int read() throws IOException {
846             boolean attempted = Blocker.begin();
847             try {
848                 return super.read();
849             } finally {
850                 Blocker.end(attempted);
851             }
852         }
853 
854         @Override
855         public int read(byte[] b) throws IOException {
856             boolean attempted = Blocker.begin();
857             try {
858                 return super.read(b);
859             } finally {
860                 Blocker.end(attempted);
861             }
862         }
863 
864         @Override
865         public int read(byte[] b, int off, int len) throws IOException {
866             boolean attempted = Blocker.begin();
867             try {
868                 return super.read(b, off, len);
869             } finally {
870                 Blocker.end(attempted);
871             }
872         }
873     }
874 
875     /**
876      * An output stream for a subprocess pipe.
877      */
878     static class PipeOutputStream extends FileOutputStream {
879         PipeOutputStream(FileDescriptor fd) {
880             super(fd);
881         }
882 
883         @Override
884         public void write(int b) throws IOException {
885             boolean attempted = Blocker.begin();
886             try {
887                 super.write(b);
888             } finally {
889                 Blocker.end(attempted);
890             }
891         }
892 
893         @Override
894         public void write(byte[] b) throws IOException {
895             boolean attempted = Blocker.begin();
896             try {
897                 super.write(b);
898             } finally {
899                 Blocker.end(attempted);
900             }
901         }
902 
903         @Override
904         public void write(byte[] b, int off, int len) throws IOException {
905             boolean attempted = Blocker.begin();
906             try {
907                 super.write(b, off, len);
908             } finally {
909                 Blocker.end(attempted);
910             }
911         }
912     }
913 
914     /**
915      * A nested class to delay looking up the Charset for the native encoding.
916      */
917     private static class CharsetHolder {
918         private static final Charset nativeCharset;
919         static {
920             Charset cs;
921             try {
922                 cs = Charset.forName(StaticProperty.nativeEncoding());
923             } catch (UnsupportedCharsetException uce) {
924                 cs = Charset.defaultCharset();
925             }
926             nativeCharset = cs;
927         }
928 
929         /**
930          * Charset for the native encoding or {@link Charset#defaultCharset().
931          */
< prev index next >