Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/ch/PipeImpl.java
32288 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*/2728package sun.nio.ch;2930import java.io.IOException;31import java.net.InetAddress;32import java.net.InetSocketAddress;33import java.nio.*;34import java.nio.channels.*;35import java.nio.channels.spi.*;36import java.security.AccessController;37import java.security.PrivilegedExceptionAction;38import java.security.PrivilegedActionException;39import java.security.SecureRandom;40import java.util.Random;414243/**44* A simple Pipe implementation based on a socket connection.45*/4647class PipeImpl48extends Pipe49{50// Number of bytes in the secret handshake.51private static final int NUM_SECRET_BYTES = 16;5253// Random object for handshake values54private static final Random RANDOM_NUMBER_GENERATOR = new SecureRandom();5556// Source and sink channels57private SourceChannel source;58private SinkChannel sink;5960private class Initializer61implements PrivilegedExceptionAction<Void>62{6364private final SelectorProvider sp;6566private IOException ioe = null;6768private Initializer(SelectorProvider sp) {69this.sp = sp;70}7172@Override73public Void run() throws IOException {74LoopbackConnector connector = new LoopbackConnector();75connector.run();76if (ioe instanceof ClosedByInterruptException) {77ioe = null;78Thread connThread = new Thread(connector) {79@Override80public void interrupt() {}81};82connThread.start();83for (;;) {84try {85connThread.join();86break;87} catch (InterruptedException ex) {}88}89Thread.currentThread().interrupt();90}9192if (ioe != null)93throw new IOException("Unable to establish loopback connection", ioe);9495return null;96}9798private class LoopbackConnector implements Runnable {99100@Override101public void run() {102ServerSocketChannel ssc = null;103SocketChannel sc1 = null;104SocketChannel sc2 = null;105106try {107// Create secret with a backing array.108ByteBuffer secret = ByteBuffer.allocate(NUM_SECRET_BYTES);109ByteBuffer bb = ByteBuffer.allocate(NUM_SECRET_BYTES);110111// Loopback address112InetAddress lb = InetAddress.getByName("127.0.0.1");113assert(lb.isLoopbackAddress());114InetSocketAddress sa = null;115for(;;) {116// Bind ServerSocketChannel to a port on the loopback117// address118if (ssc == null || !ssc.isOpen()) {119ssc = ServerSocketChannel.open();120ssc.socket().bind(new InetSocketAddress(lb, 0));121sa = new InetSocketAddress(lb, ssc.socket().getLocalPort());122}123124// Establish connection (assume connections are eagerly125// accepted)126sc1 = SocketChannel.open(sa);127RANDOM_NUMBER_GENERATOR.nextBytes(secret.array());128do {129sc1.write(secret);130} while (secret.hasRemaining());131secret.rewind();132133// Get a connection and verify it is legitimate134sc2 = ssc.accept();135do {136sc2.read(bb);137} while (bb.hasRemaining());138bb.rewind();139140if (bb.equals(secret))141break;142143sc2.close();144sc1.close();145}146147// Create source and sink channels148source = new SourceChannelImpl(sp, sc1);149sink = new SinkChannelImpl(sp, sc2);150} catch (IOException e) {151try {152if (sc1 != null)153sc1.close();154if (sc2 != null)155sc2.close();156} catch (IOException e2) {}157ioe = e;158} finally {159try {160if (ssc != null)161ssc.close();162} catch (IOException e2) {}163}164}165}166}167168PipeImpl(final SelectorProvider sp) throws IOException {169try {170AccessController.doPrivileged(new Initializer(sp));171} catch (PrivilegedActionException x) {172throw (IOException)x.getCause();173}174}175176public SourceChannel source() {177return source;178}179180public SinkChannel sink() {181return sink;182}183184}185186187