jdk/src/windows/classes/sun/nio/ch/PipeImpl.java
author xdono
Thu, 02 Oct 2008 19:58:32 -0700
changeset 1247 b4c26443dee5
parent 895 67f1dc69ad10
child 5506 202f599c92aa
permissions -rw-r--r--
6754988: Update copyright year Summary: Update for files that have been modified starting July 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 895
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A simple Pipe implementation based on a socket connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class PipeImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    extends Pipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // Source and sink channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private SourceChannel source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private SinkChannel sink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Random object for handshake values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final Random rnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        byte[] someBytes = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        boolean resultOK = IOUtil.randomBytes(someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (resultOK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            rnd = new Random(ByteBuffer.wrap(someBytes).getLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private class Initializer
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    70
        implements PrivilegedExceptionAction<Void>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        private final SelectorProvider sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        private Initializer(SelectorProvider sp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            this.sp = sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
895
67f1dc69ad10 6726309: Compiler warnings in nio code
alanb
parents: 2
diff changeset
    79
        public Void run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            ServerSocketChannel ssc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            SocketChannel sc1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            SocketChannel sc2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // loopback address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                InetAddress lb = InetAddress.getByName("127.0.0.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                assert(lb.isLoopbackAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                // bind ServerSocketChannel to a port on the loopback address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                ssc.socket().bind(new InetSocketAddress(lb, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                // Establish connection (assumes connections are eagerly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                // accepted)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                InetSocketAddress sa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    = new InetSocketAddress(lb, ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                sc1 = SocketChannel.open(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                ByteBuffer bb = ByteBuffer.allocate(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                long secret = rnd.nextLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                bb.putLong(secret).flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                sc1.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                // Get a connection and verify it is legitimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    sc2 = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    sc2.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    bb.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    if (bb.getLong() == secret)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    sc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // Create source and sink channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                source = new SourceChannelImpl(sp, sc1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                sink = new SinkChannelImpl(sp, sc2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    if (sc1 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        sc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    if (sc2 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        sc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                } catch (IOException e2) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                IOException x = new IOException("Unable to establish"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                + " loopback connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                x.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    if (ssc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                } catch (IOException e2) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    PipeImpl(final SelectorProvider sp) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            AccessController.doPrivileged(new Initializer(sp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (PrivilegedActionException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw (IOException)x.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public SourceChannel source() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public SinkChannel sink() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return sink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}