jdk/test/java/rmi/testlibrary/TestSocketFactory.java
author rriggs
Thu, 16 Mar 2017 16:16:31 -0400
changeset 45984 75fef64e21fa
child 46056 46c8ef3d3c25
permissions -rw-r--r--
8163958: Improved garbage collection Reviewed-by: smarks, chegar, skoivu, rhalade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45984
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     1
/*
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     4
 *
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     8
 *
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    13
 * accompanied this code).
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    14
 *
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    18
 *
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    21
 * questions.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    22
 */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    23
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    25
import java.io.FilterInputStream;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    26
import java.io.IOException;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    27
import java.io.InputStream;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    28
import java.io.OutputStream;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    29
import java.io.Serializable;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    30
import java.net.InetAddress;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    31
import java.net.ServerSocket;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    32
import java.net.Socket;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    33
import java.net.SocketAddress;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    34
import java.net.SocketException;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    35
import java.net.SocketOption;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    36
import java.nio.channels.ServerSocketChannel;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    37
import java.nio.channels.SocketChannel;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    38
import java.rmi.server.RMIClientSocketFactory;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    39
import java.rmi.server.RMIServerSocketFactory;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    40
import java.rmi.server.RMISocketFactory;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    41
import java.util.ArrayList;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    42
import java.util.Arrays;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    43
import java.util.List;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    44
import java.util.Objects;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    45
import java.util.Set;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    46
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    47
import org.testng.Assert;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    48
import org.testng.TestNG;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    49
import org.testng.annotations.Test;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    50
import org.testng.annotations.DataProvider;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    51
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    52
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    53
/**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    54
 * A RMISocketFactory utility factory to log RMI stream contents and to
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    55
 * match and replace output stream contents to simulate failures.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    56
 */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    57
public class TestSocketFactory extends RMISocketFactory
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    58
        implements RMIClientSocketFactory, RMIServerSocketFactory, Serializable {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    59
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    60
    private static final long serialVersionUID = 1L;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    61
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    62
    private volatile transient byte[] matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    63
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    64
    private volatile transient byte[] replaceBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    65
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    66
    private transient final List<InterposeSocket> sockets = new ArrayList<>();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    67
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    68
    private transient final List<InterposeServerSocket> serverSockets = new ArrayList<>();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    69
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    70
    public static final boolean DEBUG = false;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    71
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    72
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    73
     * Debugging output can be synchronized with logging of RMI actions.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    74
     *
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    75
     * @param format a printf format
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    76
     * @param args   any args
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    77
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    78
    private static void DEBUG(String format, Object... args) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    79
        if (DEBUG) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    80
            System.err.printf(format, args);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    81
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    82
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    83
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    84
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    85
     * Create a socket factory that creates InputStreams that log
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    86
     * and OutputStreams that log .
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    87
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    88
    public TestSocketFactory() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    89
        this.matchBytes = new byte[0];
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    90
        this.replaceBytes = this.matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    91
        System.out.printf("Creating TestSocketFactory()%n");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    92
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    93
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    94
    public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    95
        this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    96
        this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    97
        sockets.forEach( s -> s.setMatchReplaceBytes(matchBytes, replaceBytes));
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    98
        serverSockets.forEach( s -> s.setMatchReplaceBytes(matchBytes, replaceBytes));
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
    99
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   100
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   101
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   102
    @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   103
    public Socket createSocket(String host, int port) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   104
        Socket socket = RMISocketFactory.getDefaultSocketFactory()
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   105
                .createSocket(host, port);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   106
        InterposeSocket s = new InterposeSocket(socket, matchBytes, replaceBytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   107
        sockets.add(s);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   108
        return s;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   109
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   110
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   111
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   112
     * Return the current list of sockets.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   113
     * @return Return a snapshot of the current list of sockets
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   114
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   115
    public List<InterposeSocket> getSockets() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   116
        List<InterposeSocket> snap = new ArrayList<>(sockets);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   117
        return snap;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   118
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   119
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   120
    @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   121
    public ServerSocket createServerSocket(int port) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   122
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   123
        ServerSocket serverSocket = RMISocketFactory.getDefaultSocketFactory()
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   124
                .createServerSocket(port);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   125
        InterposeServerSocket ss = new InterposeServerSocket(serverSocket, matchBytes, replaceBytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   126
        serverSockets.add(ss);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   127
        return ss;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   128
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   129
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   130
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   131
     * Return the current list of server sockets.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   132
     * @return Return a snapshot of the current list of server sockets
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   133
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   134
    public List<InterposeServerSocket> getServerSockets() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   135
        List<InterposeServerSocket> snap = new ArrayList<>(serverSockets);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   136
        return snap;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   137
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   138
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   139
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   140
     * An InterposeSocket wraps a socket that produces InputStreams
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   141
     * and OutputStreams that log the traffic.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   142
     * The OutputStreams it produces match an array of bytes and replace them.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   143
     * Useful for injecting protocol and content errors.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   144
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   145
    public static class InterposeSocket extends Socket {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   146
        private final Socket socket;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   147
        private InputStream in;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   148
        private MatchReplaceOutputStream out;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   149
        private volatile byte[] matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   150
        private volatile byte[] replaceBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   151
        private final ByteArrayOutputStream inLogStream;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   152
        private final ByteArrayOutputStream outLogStream;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   153
        private final String name;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   154
        private static volatile int num = 0;    // index for created InterposeSockets
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   155
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   156
        public InterposeSocket(Socket socket, byte[] matchBytes, byte[] replaceBytes) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   157
            this.socket = socket;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   158
            this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   159
            this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   160
            this.inLogStream = new ByteArrayOutputStream();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   161
            this.outLogStream = new ByteArrayOutputStream();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   162
            this.name = "IS" + ++num + "::"
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   163
                    + Thread.currentThread().getName() + ": "
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   164
                    + socket.getLocalPort() + " <  " + socket.getPort();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   165
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   166
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   167
        public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   168
            this.matchBytes = matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   169
            this.replaceBytes = replaceBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   170
            out.setMatchReplaceBytes(matchBytes, replaceBytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   171
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   172
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   173
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   174
        public void connect(SocketAddress endpoint) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   175
            socket.connect(endpoint);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   176
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   177
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   178
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   179
        public void connect(SocketAddress endpoint, int timeout) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   180
            socket.connect(endpoint, timeout);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   181
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   182
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   183
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   184
        public void bind(SocketAddress bindpoint) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   185
            socket.bind(bindpoint);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   186
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   187
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   188
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   189
        public InetAddress getInetAddress() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   190
            return socket.getInetAddress();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   191
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   192
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   193
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   194
        public InetAddress getLocalAddress() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   195
            return socket.getLocalAddress();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   196
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   197
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   198
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   199
        public int getPort() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   200
            return socket.getPort();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   201
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   202
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   203
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   204
        public int getLocalPort() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   205
            return socket.getLocalPort();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   206
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   207
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   208
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   209
        public SocketAddress getRemoteSocketAddress() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   210
            return socket.getRemoteSocketAddress();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   211
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   212
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   213
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   214
        public SocketAddress getLocalSocketAddress() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   215
            return socket.getLocalSocketAddress();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   216
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   217
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   218
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   219
        public SocketChannel getChannel() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   220
            return socket.getChannel();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   221
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   222
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   223
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   224
        public synchronized void close() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   225
            socket.close();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   226
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   227
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   228
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   229
        public String toString() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   230
            return "InterposeSocket " + name + ": " + socket.toString();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   231
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   232
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   233
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   234
        public boolean isConnected() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   235
            return socket.isConnected();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   236
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   237
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   238
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   239
        public boolean isBound() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   240
            return socket.isBound();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   241
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   242
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   243
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   244
        public boolean isClosed() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   245
            return socket.isClosed();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   246
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   247
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   248
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   249
        public <T> Socket setOption(SocketOption<T> name, T value) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   250
            return socket.setOption(name, value);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   251
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   252
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   253
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   254
        public <T> T getOption(SocketOption<T> name) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   255
            return socket.getOption(name);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   256
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   257
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   258
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   259
        public Set<SocketOption<?>> supportedOptions() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   260
            return socket.supportedOptions();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   261
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   262
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   263
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   264
        public synchronized InputStream getInputStream() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   265
            if (in == null) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   266
                in = socket.getInputStream();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   267
                String name = Thread.currentThread().getName() + ": "
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   268
                        + socket.getLocalPort() + " <  " + socket.getPort();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   269
                in = new LoggingInputStream(in, name, inLogStream);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   270
                DEBUG("Created new InterposeInputStream: %s%n", name);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   271
            }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   272
            return in;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   273
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   274
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   275
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   276
        public synchronized OutputStream getOutputStream() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   277
            if (out == null) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   278
                OutputStream o = socket.getOutputStream();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   279
                String name = Thread.currentThread().getName() + ": "
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   280
                        + socket.getLocalPort() + "  > " + socket.getPort();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   281
                out = new MatchReplaceOutputStream(o, name, outLogStream, matchBytes, replaceBytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   282
                DEBUG("Created new MatchReplaceOutputStream: %s%n", name);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   283
            }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   284
            return out;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   285
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   286
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   287
        /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   288
         * Return the bytes logged from the input stream.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   289
         * @return Return the bytes logged from the input stream.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   290
         */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   291
        public byte[] getInLogBytes() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   292
            return inLogStream.toByteArray();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   293
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   294
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   295
        /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   296
         * Return the bytes logged from the output stream.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   297
         * @return Return the bytes logged from the output stream.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   298
         */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   299
        public byte[] getOutLogBytes() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   300
            return outLogStream.toByteArray();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   301
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   302
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   303
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   304
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   305
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   306
     * InterposeServerSocket is a ServerSocket that wraps each Socket it accepts
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   307
     * with an InterposeSocket so that its input and output streams can be monitored.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   308
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   309
    public static class InterposeServerSocket extends ServerSocket {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   310
        private final ServerSocket socket;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   311
        private volatile byte[] matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   312
        private volatile byte[] replaceBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   313
        private final List<InterposeSocket> sockets = new ArrayList<>();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   314
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   315
        public InterposeServerSocket(ServerSocket socket, byte[] matchBytes, byte[] replaceBytes) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   316
            this.socket = socket;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   317
            this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   318
            this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   319
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   320
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   321
        public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   322
            this.matchBytes = matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   323
            this.replaceBytes = replaceBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   324
            sockets.forEach(s -> s.setMatchReplaceBytes(matchBytes, replaceBytes));
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   325
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   326
        /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   327
         * Return a snapshot of the current list of sockets created from this server socket.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   328
         * @return Return a snapshot of the current list of sockets
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   329
         */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   330
        public List<InterposeSocket> getSockets() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   331
            List<InterposeSocket> snap = new ArrayList<>(sockets);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   332
            return snap;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   333
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   334
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   335
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   336
        public void bind(SocketAddress endpoint) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   337
            socket.bind(endpoint);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   338
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   339
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   340
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   341
        public void bind(SocketAddress endpoint, int backlog) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   342
            socket.bind(endpoint, backlog);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   343
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   344
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   345
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   346
        public InetAddress getInetAddress() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   347
            return socket.getInetAddress();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   348
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   349
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   350
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   351
        public int getLocalPort() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   352
            return socket.getLocalPort();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   353
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   354
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   355
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   356
        public SocketAddress getLocalSocketAddress() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   357
            return socket.getLocalSocketAddress();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   358
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   359
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   360
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   361
        public Socket accept() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   362
            Socket s = socket.accept();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   363
            InterposeSocket socket = new InterposeSocket(s, matchBytes, replaceBytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   364
            sockets.add(socket);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   365
            return socket;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   366
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   367
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   368
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   369
        public void close() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   370
            socket.close();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   371
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   372
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   373
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   374
        public ServerSocketChannel getChannel() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   375
            return socket.getChannel();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   376
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   377
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   378
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   379
        public boolean isClosed() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   380
            return socket.isClosed();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   381
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   382
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   383
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   384
        public String toString() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   385
            return socket.toString();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   386
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   387
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   388
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   389
        public <T> ServerSocket setOption(SocketOption<T> name, T value) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   390
            return socket.setOption(name, value);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   391
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   392
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   393
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   394
        public <T> T getOption(SocketOption<T> name) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   395
            return socket.getOption(name);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   396
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   397
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   398
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   399
        public Set<SocketOption<?>> supportedOptions() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   400
            return socket.supportedOptions();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   401
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   402
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   403
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   404
        public synchronized void setSoTimeout(int timeout) throws SocketException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   405
            socket.setSoTimeout(timeout);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   406
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   407
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   408
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   409
        public synchronized int getSoTimeout() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   410
            return socket.getSoTimeout();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   411
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   412
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   413
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   414
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   415
     * LoggingInputStream is a stream and logs all bytes read to it.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   416
     * For identification it is given a name.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   417
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   418
    public static class LoggingInputStream extends FilterInputStream {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   419
        private int bytesIn = 0;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   420
        private final String name;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   421
        private final OutputStream log;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   422
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   423
        public LoggingInputStream(InputStream in, String name, OutputStream log) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   424
            super(in);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   425
            this.name = name;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   426
            this.log = log;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   427
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   428
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   429
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   430
        public int read() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   431
            int b = super.read();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   432
            if (b >= 0) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   433
                log.write(b);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   434
                bytesIn++;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   435
            }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   436
            return b;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   437
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   438
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   439
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   440
        public int read(byte[] b, int off, int len) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   441
            int bytes = super.read(b, off, len);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   442
            if (bytes > 0) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   443
                log.write(b, off, bytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   444
                bytesIn += bytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   445
            }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   446
            return bytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   447
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   448
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   449
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   450
        public int read(byte[] b) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   451
            return read(b, 0, b.length);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   452
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   453
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   454
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   455
        public void close() throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   456
            super.close();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   457
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   458
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   459
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   460
        public String toString() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   461
            return String.format("%s: In: (%d)", name, bytesIn);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   462
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   463
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   464
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   465
    /**
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   466
     * An OutputStream that replaces one string of bytes with another.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   467
     * If any range matches, the match starts after the partial match.
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   468
     */
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   469
    static class MatchReplaceOutputStream extends OutputStream {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   470
        private final OutputStream out;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   471
        private final String name;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   472
        private volatile byte[] matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   473
        private volatile byte[] replaceBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   474
        int matchIndex;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   475
        private int bytesOut = 0;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   476
        private final OutputStream log;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   477
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   478
        MatchReplaceOutputStream(OutputStream out, String name, OutputStream log,
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   479
                                 byte[] matchBytes, byte[] replaceBytes) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   480
            this.out = out;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   481
            this.name = name;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   482
            this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   483
            this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   484
            matchIndex = 0;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   485
            this.log = log;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   486
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   487
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   488
        public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   489
            this.matchBytes = matchBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   490
            this.replaceBytes = replaceBytes;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   491
            matchIndex = 0;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   492
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   493
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   494
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   495
        public void write(int b) throws IOException {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   496
            b = b & 0xff;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   497
            if (matchBytes.length == 0) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   498
                out.write(b);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   499
                log.write(b);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   500
                bytesOut++;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   501
                return;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   502
            }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   503
            if (b == (matchBytes[matchIndex] & 0xff)) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   504
                if (++matchIndex >= matchBytes.length) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   505
                    matchIndex = 0;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   506
                    DEBUG( "TestSocketFactory MatchReplace %s replaced %d bytes at offset: %d (x%04x)%n",
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   507
                            name, replaceBytes.length, bytesOut, bytesOut);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   508
                    out.write(replaceBytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   509
                    log.write(replaceBytes);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   510
                    bytesOut += replaceBytes.length;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   511
                }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   512
            } else {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   513
                if (matchIndex > 0) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   514
                    // mismatch, write out any that matched already
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   515
                    if (matchIndex > 0) // Only non-trivial matches
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   516
                        DEBUG( "Partial match %s matched %d bytes at offset: %d (0x%04x), expected: x%02x, actual: x%02x%n",
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   517
                                name, matchIndex, bytesOut, bytesOut,  matchBytes[matchIndex], b);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   518
                    out.write(matchBytes, 0, matchIndex);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   519
                    log.write(matchBytes, 0, matchIndex);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   520
                    bytesOut += matchIndex;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   521
                    matchIndex = 0;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   522
                }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   523
                if (b == (matchBytes[matchIndex] & 0xff)) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   524
                    matchIndex++;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   525
                } else {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   526
                    out.write(b);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   527
                    log.write(b);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   528
                    bytesOut++;
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   529
                }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   530
            }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   531
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   532
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   533
        @Override
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   534
        public String toString() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   535
            return String.format("%s: Out: (%d)", name, bytesOut);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   536
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   537
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   538
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   539
    private static byte[] orig = new byte[]{
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   540
            (byte) 0x80, 0x05,
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   541
            0x73, 0x72, 0x00, 0x12, // TC_OBJECT, TC_CLASSDESC, length = 18
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   542
            0x6A, 0x61, 0x76, 0x61, 0x2E, 0x72, 0x6D, 0x69, 0x2E, // "java.rmi."
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   543
            0x64, 0x67, 0x63, 0x2E, 0x4C, 0x65, 0x61, 0x73, 0x65  // "dgc.Lease"
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   544
    };
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   545
    private static byte[] repl = new byte[]{
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   546
            (byte) 0x80, 0x05,
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   547
            0x73, 0x72, 0x00, 0x12, // TC_OBJECT, TC_CLASSDESC, length = 18
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   548
            0x6A, 0x61, 0x76, 0x61, 0x2E, (byte) 'l', (byte) 'a', (byte) 'n', (byte) 'g',
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   549
            0x2E, (byte) 'R', (byte) 'u', (byte) 'n', (byte) 'n', (byte) 'a', (byte) 'b', (byte) 'l',
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   550
            (byte) 'e'
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   551
    };
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   552
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   553
    @DataProvider(name = "MatchReplaceData")
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   554
    static Object[][] matchReplaceData() {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   555
        byte[] empty = new byte[0];
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   556
        byte[] byte1 = new byte[]{1, 2, 3, 4, 5, 6};
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   557
        byte[] bytes2 = new byte[]{1, 2, 4, 3, 5, 6};
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   558
        byte[] bytes3 = new byte[]{6, 5, 4, 3, 2, 1};
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   559
        byte[] bytes4 = new byte[]{1, 2, 0x10, 0x20, 0x30, 0x40, 5, 6};
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   560
        byte[] bytes4a = new byte[]{1, 2, 0x10, 0x20, 0x30, 0x40, 5, 7};  // mostly matches bytes4
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   561
        byte[] bytes5 = new byte[]{0x30, 0x40, 5, 6};
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   562
        byte[] bytes6 = new byte[]{1, 2, 0x10, 0x20, 0x30};
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   563
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   564
        return new Object[][]{
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   565
                {new byte[]{}, new byte[]{}, empty, empty},
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   566
                {new byte[]{}, new byte[]{}, byte1, byte1},
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   567
                {new byte[]{3, 4}, new byte[]{4, 3}, byte1, bytes2}, //swap bytes
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   568
                {new byte[]{3, 4}, new byte[]{0x10, 0x20, 0x30, 0x40}, byte1, bytes4}, // insert
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   569
                {new byte[]{1, 2, 0x10, 0x20}, new byte[]{}, bytes4, bytes5}, // delete head
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   570
                {new byte[]{0x40, 5, 6}, new byte[]{}, bytes4, bytes6},   // delete tail
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   571
                {new byte[]{0x40, 0x50}, new byte[]{0x60, 0x50}, bytes4, bytes4}, // partial match, replace nothing
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   572
                {bytes4a, bytes3, bytes4, bytes4}, // long partial match, not replaced
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   573
                {orig, repl, orig, repl},
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   574
        };
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   575
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   576
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   577
    @Test(enabled = true, dataProvider = "MatchReplaceData")
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   578
    static void test3(byte[] match, byte[] replace,
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   579
                      byte[] input, byte[] expected) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   580
        System.out.printf("match: %s, replace: %s%n", Arrays.toString(match), Arrays.toString(replace));
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   581
        try (ByteArrayOutputStream output = new ByteArrayOutputStream();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   582
        ByteArrayOutputStream log = new ByteArrayOutputStream();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   583
             OutputStream out = new MatchReplaceOutputStream(output, "test3",
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   584
                     log, match, replace)) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   585
            out.write(input);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   586
            byte[] actual = output.toByteArray();
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   587
            long index = Arrays.mismatch(actual, expected);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   588
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   589
            if (index >= 0) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   590
                System.out.printf("array mismatch, offset: %d%n", index);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   591
                System.out.printf("actual: %s%n", Arrays.toString(actual));
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   592
                System.out.printf("expected: %s%n", Arrays.toString(expected));
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   593
            }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   594
            Assert.assertEquals(actual, expected, "match/replace fail");
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   595
        } catch (IOException ioe) {
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   596
            Assert.fail("unexpected exception", ioe);
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   597
        }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   598
    }
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   599
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   600
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   601
75fef64e21fa 8163958: Improved garbage collection
rriggs
parents:
diff changeset
   602
}