Basic.java revision 724:3d110bb4dc19
1/*
2 * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4417152 4481572 6248930 6725399
26 * @summary Test Channels basic functionality
27 */
28
29import java.io.*;
30import java.nio.*;
31import java.nio.charset.*;
32import java.nio.channels.*;
33
34
35public class Basic {
36
37    static String message;
38
39    static String encoding;
40
41    static File blah;
42
43    static int ITERATIONS = 500;
44
45    public static void main(String[] args) throws Exception {
46        message = "ascii data for a test";
47        encoding = "ISO-8859-1";
48        test();
49        message = "\ucafe\ubabe\ucafe\ubabe\ucafe\ubabe";
50        encoding = "UTF-8";
51        test();
52    }
53
54    static void failNpeExpected() {
55        throw new RuntimeException("Did not get the expected NullPointerException.");
56    }
57
58    private static void test() throws Exception {
59        //Test if methods of Channels throw NPE with null argument(s)
60        try {
61            Channels.newInputStream((ReadableByteChannel)null);
62            failNpeExpected();
63        } catch (NullPointerException npe) {}
64
65        try {
66            Channels.newOutputStream((WritableByteChannel)null);
67            failNpeExpected();
68        } catch (NullPointerException npe) {}
69
70        try {
71            ReadableByteChannel channel = Channels.newChannel((InputStream)null);
72            failNpeExpected();
73        } catch (NullPointerException ne) {}  // OK. As expected.
74
75        try {
76            WritableByteChannel channel = Channels.newChannel((OutputStream)null);
77            failNpeExpected();
78        } catch (NullPointerException ne) {}  // OK. As expected.
79
80        WritableByteChannel wbc = new WritableByteChannel() {
81            public int write(ByteBuffer src) { return 0; }
82            public void close() throws IOException { }
83            public boolean isOpen() { return true; }
84        };
85
86        ReadableByteChannel rbc = new ReadableByteChannel() {
87            public int read(ByteBuffer dst) { return 0; }
88            public void close() {}
89            public boolean isOpen() { return true; }
90        };
91
92        try {
93            Channels.newReader((ReadableByteChannel)null,
94                               Charset.defaultCharset().newDecoder(),
95                               -1);
96            failNpeExpected();
97        } catch (NullPointerException npe) {}
98
99        try {
100            Channels.newReader(rbc, (CharsetDecoder)null, -1);
101            failNpeExpected();
102        } catch (NullPointerException npe) {}
103
104        try {
105            Channels.newReader((ReadableByteChannel)null,
106                               Charset.defaultCharset().name());
107            failNpeExpected();
108        } catch (NullPointerException npe) {}
109
110        try {
111            Channels.newReader(rbc, null);
112            failNpeExpected();
113        } catch (NullPointerException npe) {}
114
115
116        try {
117            Channels.newReader(null, null);
118            failNpeExpected();
119        } catch (NullPointerException npe) {}
120
121        try {
122            Channels.newWriter((WritableByteChannel)null,
123                               Charset.defaultCharset().newEncoder(),
124                               -1);
125            failNpeExpected();
126        } catch (NullPointerException npe) {}
127
128        try {
129            Channels.newWriter(null, null, -1);
130            failNpeExpected();
131        } catch (NullPointerException npe) {}
132
133        try {
134            Channels.newWriter(wbc, null, -1);
135            failNpeExpected();
136        } catch (NullPointerException npe) {}
137
138        try {
139            Channels.newWriter((WritableByteChannel)null,
140                               Charset.defaultCharset().name());
141            failNpeExpected();
142        } catch (NullPointerException npe) {}
143
144        try {
145            Channels.newWriter(wbc, null);
146            failNpeExpected();
147        } catch (NullPointerException npe) {}
148
149        try {
150            Channels.newWriter(null, null);
151            failNpeExpected();
152        } catch (NullPointerException npe) {}
153
154
155        try {
156            blah = File.createTempFile("blah", null);
157
158            testNewOutputStream(blah);
159            readAndCheck(blah);
160            blah.delete();
161
162            writeOut(blah, ITERATIONS);
163            testNewInputStream(blah);
164            blah.delete();
165
166            testNewChannelOut(blah);
167            readAndCheck(blah);
168            blah.delete();
169
170            writeOut(blah, ITERATIONS);
171            testNewChannelIn(blah);
172            test4481572(blah);
173            blah.delete();
174
175            testNewWriter(blah);
176            readAndCheck(blah);
177            blah.delete();
178
179            writeOut(blah, ITERATIONS);
180            testNewReader(blah);
181
182        } finally {
183            blah.delete();
184        }
185    }
186
187    private static void readAndCheck(File blah) throws Exception {
188        FileInputStream fis = new FileInputStream(blah);
189        int messageSize = message.length() * ITERATIONS * 3 + 1;
190        byte bb[] = new byte[messageSize];
191        int bytesRead = 0;
192        int totalRead = 0;
193        while (bytesRead != -1) {
194            totalRead += bytesRead;
195            bytesRead = fis.read(bb, totalRead, messageSize - totalRead);
196        }
197        String result = new String(bb, 0, totalRead, encoding);
198        int len = message.length();
199        for (int i=0; i<ITERATIONS; i++) {
200            String segment = result.substring(i++ * len, i * len);
201            if (!segment.equals(message))
202                throw new RuntimeException("Test failed");
203        }
204        fis.close();
205    }
206
207    private static void writeOut(File blah, int limit) throws Exception {
208        FileOutputStream fos = new FileOutputStream(blah);
209        for (int i=0; i<limit; i++)
210            fos.write(message.getBytes(encoding));
211        fos.close();
212    }
213
214    private static void testNewOutputStream(File blah) throws Exception {
215        FileOutputStream fos = new FileOutputStream(blah);
216        FileChannel fc = fos.getChannel();
217        WritableByteChannel wbc = (WritableByteChannel)fc;
218        OutputStream os = Channels.newOutputStream(wbc);
219        for (int i=0; i<ITERATIONS; i++)
220            os.write(message.getBytes(encoding));
221        os.close();
222        fos.close();
223    }
224
225    private static void testNewInputStream(File blah) throws Exception {
226        FileInputStream fis = new FileInputStream(blah);
227        FileChannel fc = fis.getChannel();
228        ReadableByteChannel rbc = (ReadableByteChannel)fc;
229        InputStream is = Channels.newInputStream(rbc);
230        int messageSize = message.length() * ITERATIONS * 3 + 1;
231        byte bb[] = new byte[messageSize];
232
233        int bytesRead = 0;
234        int totalRead = 0;
235        while (bytesRead != -1) {
236            totalRead += bytesRead;
237            bytesRead = is.read(bb, totalRead, messageSize - totalRead);
238        }
239
240        String result = new String(bb, 0, totalRead, encoding);
241        int len = message.length();
242        for (int i=0; i<ITERATIONS; i++) {
243            String segment = result.substring(i++ * len, i * len);
244            if (!segment.equals(message))
245                throw new RuntimeException("Test failed");
246        }
247        is.close();
248        fis.close();
249    }
250
251    private static void testNewChannelOut(File blah) throws Exception {
252        ExtendedFileOutputStream fos = new ExtendedFileOutputStream(blah);
253        WritableByteChannel wbc = Channels.newChannel(fos);
254        for (int i=0; i<ITERATIONS; i++)
255            wbc.write(ByteBuffer.wrap(message.getBytes(encoding)));
256        wbc.close();
257        fos.close();
258    }
259
260    private static void testNewChannelIn(File blah) throws Exception {
261        ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
262        ReadableByteChannel rbc = Channels.newChannel(fis);
263
264        int messageSize = message.length() * ITERATIONS * 3;
265        byte data[] = new byte[messageSize+1];
266        ByteBuffer bb = ByteBuffer.wrap(data);
267
268        int bytesRead = 0;
269        int totalRead = 0;
270        while (bytesRead != -1) {
271            totalRead += bytesRead;
272            bytesRead = rbc.read(bb);
273        }
274
275        String result = new String(data, 0, totalRead, encoding);
276        int len = message.length();
277        for (int i=0; i<ITERATIONS; i++) {
278            String segment = result.substring(i++ * len, i * len);
279            if (!segment.equals(message))
280                throw new RuntimeException("Test failed");
281        }
282        rbc.close();
283        fis.close();
284    }
285
286    // Causes BufferOverflowException if bug 4481572 is present.
287    private static void test4481572(File blah) throws Exception {
288        ExtendedFileInputStream fis = new ExtendedFileInputStream(blah);
289        ReadableByteChannel rbc = Channels.newChannel(fis);
290
291        byte data[] = new byte[9000];
292        ByteBuffer bb = ByteBuffer.wrap(data);
293
294        int bytesRead = 1;
295        int totalRead = 0;
296        while (bytesRead > 0) {
297            totalRead += bytesRead;
298            bytesRead = rbc.read(bb);
299        }
300        rbc.close();
301        fis.close();
302    }
303
304    private static void testNewWriter(File blah) throws Exception {
305        FileOutputStream fos = new FileOutputStream(blah);
306        WritableByteChannel wbc = (WritableByteChannel)fos.getChannel();
307        Writer w = Channels.newWriter(wbc, encoding);
308        char data[] = new char[40];
309        message.getChars(0, message.length(), data, 0);
310        for (int i=0; i<ITERATIONS; i++)
311            w.write(data, 0, message.length());
312        w.flush();
313        w.close();
314        fos.close();
315    }
316
317    private static void testNewReader(File blah) throws Exception {
318        FileInputStream fis = new FileInputStream(blah);
319        ReadableByteChannel rbc = (ReadableByteChannel)fis.getChannel();
320        Reader r = Channels.newReader(rbc, encoding);
321
322        int messageSize = message.length() * ITERATIONS;
323        char data[] = new char[messageSize];
324
325        int totalRead = 0;
326        int charsRead = 0;
327        while (totalRead < messageSize) {
328            totalRead += charsRead;
329            charsRead = r.read(data, totalRead, messageSize - totalRead);
330        }
331        String result = new String(data, 0, totalRead);
332        int len = message.length();
333        for (int i=0; i<ITERATIONS; i++) {
334            String segment = result.substring(i++ * len, i * len);
335            if (!segment.equals(message))
336                throw new RuntimeException("Test failed");
337        }
338        r.close();
339        fis.close();
340    }
341}
342
343class ExtendedFileInputStream extends java.io.FileInputStream {
344    ExtendedFileInputStream(File file) throws FileNotFoundException {
345        super(file);
346    }
347}
348
349class ExtendedFileOutputStream extends java.io.FileOutputStream {
350    ExtendedFileOutputStream(File file) throws FileNotFoundException {
351        super(file);
352    }
353}
354