1/*
2 * Copyright (c) 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 6448457
26 * @summary Test Channels.newOutputStream returns OutputStream that handles
27 *     short writes from the underlying channel
28 * @key randomness
29 */
30
31import java.io.OutputStream;
32import java.io.IOException;
33import java.nio.ByteBuffer;
34import java.nio.channels.*;
35import java.util.Random;
36
37public class ShortWrite {
38
39    static Random rand = new Random();
40    static int bytesWritten = 0;
41
42    public static void main(String[] args) throws IOException {
43
44        WritableByteChannel wbc = new WritableByteChannel() {
45            public int write(ByteBuffer src) {
46                int rem = src.remaining();
47                if (rem > 0) {
48                    // short write
49                    int n = rand.nextInt(rem) + 1;
50                    src.position(src.position() + n);
51                    bytesWritten += n;
52                    return n;
53                } else {
54                    return 0;
55                }
56            }
57            public void close() throws IOException {
58                throw new RuntimeException("not implemented");
59            }
60            public boolean isOpen() {
61                throw new RuntimeException("not implemented");
62            }
63        };
64
65        // wrap Channel with OutputStream
66        OutputStream out = Channels.newOutputStream(wbc);
67
68
69        // write 100, 99, 98, ... 1
70        // and check that the expected number of bytes is written
71        int expected = 0;
72        byte[] buf = new byte[100];
73        for (int i=0; i<buf.length; i++) {
74            int len = buf.length-i;
75            out.write(buf, i, len);
76            expected += len;
77        }
78        System.out.format("Bytes written: %d, expected: %d\n", bytesWritten,
79            expected);
80        if (bytesWritten != expected)
81            throw new RuntimeException("incorrect number of bytes written");
82
83    }
84}
85