test/jdk/java/nio/channels/Channels/Basic.java
author alanb
Fri, 14 Sep 2018 16:56:09 +0100
changeset 51745 90c1dcdebc64
parent 48252 77b88d8f8380
child 54569 7689e1cc56fe
permissions -rw-r--r--
8208780: (se) test SelectWithConsumer.testReadableAndWriteable(): failure Reviewed-by: bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47494
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. 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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3955
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3955
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3955
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
3955
a585e5ca5e4a 6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents: 1639
diff changeset
    25
 * @bug 4417152 4481572 6248930 6725399 6884800
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test Channels basic functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.*;
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    31
import java.nio.charset.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static String message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static String encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static File blah;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static int ITERATIONS = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        message = "ascii data for a test";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        encoding = "ISO-8859-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        message = "\ucafe\ubabe\ucafe\ubabe\ucafe\ubabe";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        encoding = "UTF-8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    54
    static void failNpeExpected() {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    55
        throw new RuntimeException("Did not get the expected NullPointerException.");
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    56
    }
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static void test() throws Exception {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    59
        //Test if methods of Channels throw NPE with null argument(s)
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    60
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    61
            Channels.newInputStream((ReadableByteChannel)null);
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    62
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    63
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    64
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    65
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    66
            Channels.newOutputStream((WritableByteChannel)null);
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    67
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    68
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    69
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            ReadableByteChannel channel = Channels.newChannel((InputStream)null);
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    72
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    73
        } catch (NullPointerException ne) {}  // OK. As expected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            WritableByteChannel channel = Channels.newChannel((OutputStream)null);
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    77
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    78
        } catch (NullPointerException ne) {}  // OK. As expected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    80
        WritableByteChannel wbc = new WritableByteChannel() {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    81
            public int write(ByteBuffer src) { return 0; }
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    82
            public void close() throws IOException { }
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    83
            public boolean isOpen() { return true; }
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    84
        };
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    85
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    86
        ReadableByteChannel rbc = new ReadableByteChannel() {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    87
            public int read(ByteBuffer dst) { return 0; }
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    88
            public void close() {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    89
            public boolean isOpen() { return true; }
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    90
        };
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    91
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    92
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    93
            Channels.newReader((ReadableByteChannel)null,
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    94
                               Charset.defaultCharset().newDecoder(),
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    95
                               -1);
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    96
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    97
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    98
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
    99
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   100
            Channels.newReader(rbc, (CharsetDecoder)null, -1);
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   101
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   102
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   103
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   104
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   105
            Channels.newReader((ReadableByteChannel)null,
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   106
                               Charset.defaultCharset().name());
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   107
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   108
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   109
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   110
        try {
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   111
            Channels.newReader(rbc, (String)null);
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   112
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   113
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   114
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   115
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   116
        try {
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   117
            Channels.newReader(null, (String)null);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   118
            failNpeExpected();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   119
        } catch (NullPointerException npe) {}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   120
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   121
        try {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   122
            Channels.newReader(rbc, (Charset)null);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   123
            failNpeExpected();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   124
        } catch (NullPointerException npe) {}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   125
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   126
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   127
        try {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   128
            Channels.newReader(null, (Charset)null);
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   129
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   130
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   131
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   132
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   133
            Channels.newWriter((WritableByteChannel)null,
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   134
                               Charset.defaultCharset().newEncoder(),
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   135
                               -1);
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   136
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   137
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   138
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   139
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   140
            Channels.newWriter(null, null, -1);
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   141
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   142
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   143
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   144
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   145
            Channels.newWriter(wbc, null, -1);
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   146
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   147
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   148
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   149
        try {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   150
            Channels.newWriter((WritableByteChannel)null,
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   151
                               Charset.defaultCharset().name());
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   152
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   153
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   154
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   155
        try {
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   156
            Channels.newWriter(wbc, (String)null);
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   157
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   158
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   159
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   160
        try {
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   161
            Channels.newWriter(null, (String)null);
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   162
            failNpeExpected();
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   163
        } catch (NullPointerException npe) {}
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 2
diff changeset
   164
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   165
        try {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   166
            Channels.newWriter(wbc, (Charset)null);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   167
            failNpeExpected();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   168
        } catch (NullPointerException npe) {}
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   169
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   170
        try {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   171
            Channels.newWriter(null, (Charset)null);
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   172
            failNpeExpected();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents: 47494
diff changeset
   173
        } catch (NullPointerException npe) {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            blah = File.createTempFile("blah", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            testNewOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            readAndCheck(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            blah.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            writeOut(blah, ITERATIONS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            testNewInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            blah.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            testNewChannelOut(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            readAndCheck(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            blah.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
47494
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   190
            testNewChannelWriteAfterClose(blah);
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   191
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   192
            testNewChannelReadAfterClose(blah);
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   193
            blah.delete();
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   194
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            writeOut(blah, ITERATIONS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            testNewChannelIn(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            test4481572(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            blah.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            testNewWriter(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            readAndCheck(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            blah.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            writeOut(blah, ITERATIONS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            testNewReader(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            blah.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static void readAndCheck(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        FileInputStream fis = new FileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int messageSize = message.length() * ITERATIONS * 3 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        byte bb[] = new byte[messageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        while (bytesRead != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            totalRead += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            bytesRead = fis.read(bb, totalRead, messageSize - totalRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        String result = new String(bb, 0, totalRead, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int len = message.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            String segment = result.substring(i++ * len, i * len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (!segment.equals(message))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                throw new RuntimeException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private static void writeOut(File blah, int limit) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for (int i=0; i<limit; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            fos.write(message.getBytes(encoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static void testNewOutputStream(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        FileChannel fc = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        WritableByteChannel wbc = (WritableByteChannel)fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        OutputStream os = Channels.newOutputStream(wbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        for (int i=0; i<ITERATIONS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            os.write(message.getBytes(encoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static void testNewInputStream(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        FileInputStream fis = new FileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        FileChannel fc = fis.getChannel();
3955
a585e5ca5e4a 6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents: 1639
diff changeset
   253
        InputStream is = Channels.newInputStream(fc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        int messageSize = message.length() * ITERATIONS * 3 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        byte bb[] = new byte[messageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        while (bytesRead != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            totalRead += bytesRead;
3955
a585e5ca5e4a 6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents: 1639
diff changeset
   261
            long rem = Math.min(fc.size() - totalRead, (long)Integer.MAX_VALUE);
a585e5ca5e4a 6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents: 1639
diff changeset
   262
            if (is.available() != (int)rem)
a585e5ca5e4a 6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents: 1639
diff changeset
   263
                throw new RuntimeException("available not useful or not maximally useful");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            bytesRead = is.read(bb, totalRead, messageSize - totalRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
3955
a585e5ca5e4a 6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents: 1639
diff changeset
   266
        if (is.available() != 0)
a585e5ca5e4a 6884800: (file) Path.newInputStream does not usefully implement available()
alanb
parents: 1639
diff changeset
   267
           throw new RuntimeException("available() should return 0 at EOF");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        String result = new String(bb, 0, totalRead, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int len = message.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            String segment = result.substring(i++ * len, i * len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (!segment.equals(message))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new RuntimeException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private static void testNewChannelOut(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        WritableByteChannel wbc = Channels.newChannel(fos);
47494
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   283
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (int i=0; i<ITERATIONS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        wbc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private static void testNewChannelIn(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        ReadableByteChannel rbc = Channels.newChannel(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int messageSize = message.length() * ITERATIONS * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        byte data[] = new byte[messageSize+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        ByteBuffer bb = ByteBuffer.wrap(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        while (bytesRead != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            totalRead += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            bytesRead = rbc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        String result = new String(data, 0, totalRead, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int len = message.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            String segment = result.substring(i++ * len, i * len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (!segment.equals(message))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                throw new RuntimeException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        rbc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
47494
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   316
    private static void testNewChannelWriteAfterClose(File blah)
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   317
        throws Exception {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   318
        try (ExtendedFileOutputStream fos =
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   319
            new ExtendedFileOutputStream(blah)) {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   320
            WritableByteChannel wbc = Channels.newChannel(fos);
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   321
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   322
            wbc.close();
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   323
            try {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   324
                wbc.write(ByteBuffer.allocate(0));
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   325
                throw new RuntimeException
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   326
                    ("No ClosedChannelException on WritableByteChannel::write");
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   327
            } catch (ClosedChannelException expected) {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   328
            }
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   329
        }
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   330
    }
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   331
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   332
    private static void testNewChannelReadAfterClose(File blah)
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   333
        throws Exception {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   334
        try (ExtendedFileInputStream fis = new ExtendedFileInputStream(blah)) {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   335
            ReadableByteChannel rbc = Channels.newChannel(fis);
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   336
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   337
            rbc.close();
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   338
            try {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   339
                rbc.read(ByteBuffer.allocate(0));
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   340
                throw new RuntimeException
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   341
                    ("No ClosedChannelException on ReadableByteChannel::read");
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   342
            } catch (ClosedChannelException expected) {
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   343
            }
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   344
        }
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   345
    }
24e43fd1ad69 8190500: (ch) ReadableByteChannelImpl::read and WritableByteChannelImpl::write might not throw ClosedChannelException as specified
bpb
parents: 47216
diff changeset
   346
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    // Causes BufferOverflowException if bug 4481572 is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private static void test4481572(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        ReadableByteChannel rbc = Channels.newChannel(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        byte data[] = new byte[9000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        ByteBuffer bb = ByteBuffer.wrap(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int bytesRead = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        while (bytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            totalRead += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            bytesRead = rbc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        rbc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private static void testNewWriter(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        WritableByteChannel wbc = (WritableByteChannel)fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        Writer w = Channels.newWriter(wbc, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        char data[] = new char[40];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        message.getChars(0, message.length(), data, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        for (int i=0; i<ITERATIONS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            w.write(data, 0, message.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        w.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        w.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private static void testNewReader(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        FileInputStream fis = new FileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        ReadableByteChannel rbc = (ReadableByteChannel)fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        Reader r = Channels.newReader(rbc, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        int messageSize = message.length() * ITERATIONS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        char data[] = new char[messageSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        int charsRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        while (totalRead < messageSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            totalRead += charsRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            charsRead = r.read(data, totalRead, messageSize - totalRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        String result = new String(data, 0, totalRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int len = message.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        for (int i=0; i<ITERATIONS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            String segment = result.substring(i++ * len, i * len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (!segment.equals(message))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                throw new RuntimeException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
class ExtendedFileInputStream extends java.io.FileInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    ExtendedFileInputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        super(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
class ExtendedFileOutputStream extends java.io.FileOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    ExtendedFileOutputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        super(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}