1/*
2 * Copyright (c) 1998, 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 4071765
26   @summary Bug in the parameter of str.getChars called in write
27*/
28
29
30
31import java.io.*;
32
33public class WriteFromString {
34
35
36    public static void main(String argv[]) throws Exception {
37        LocalStringWriter lsw = new LocalStringWriter();
38        boolean result = true;
39
40        String testString = "Testing of what gets written";
41        // Should write out at offset 2 for length 4 i.e."stin"
42        lsw.write(testString, 1, 4);
43        String res = lsw.toString();
44        if (!res.equals("esti")) {
45            result = false;
46            System.err.println("Writer.write is incorrect:" + res);
47        }
48
49        // Same bug in stringwriter as well
50        StringWriter sw = new StringWriter();
51        sw.write(testString, 1, 4);
52        res = sw.toString();
53        String ss = testString.substring(1,4);
54        System.out.println("Substring = "+ss);
55        if (!res.equals("esti")) {
56            System.err.println("StringWriter.write is incorrect:" + res);
57            result = false;
58        }
59        if (!result) {
60            throw new Exception("Writer.write method is incorrect.");
61        }
62    }
63
64}
65
66/**
67 * A copy of StringWriter to test the write method in Writer
68 */
69
70class LocalStringWriter extends Writer {
71
72    private StringBuffer buf;
73
74    /**
75     * Create a new string writer, using the default initial string-buffer
76     * size.
77     */
78    public LocalStringWriter() {
79        buf = new StringBuffer();
80        lock = buf;
81    }
82
83    /**
84     * Write a portion of an array of characters.
85     *
86     * @param  cbuf  Array of characters
87     * @param  off   Offset from which to start writing characters
88     * @param  len   Number of characters to write
89     */
90    public void write(char cbuf[], int off, int len) {
91        if ((off < 0) || (off > cbuf.length) || (len < 0) ||
92            ((off + len) > cbuf.length) || ((off + len) < 0)) {
93            throw new IndexOutOfBoundsException();
94        } else if (len == 0) {
95            return;
96        }
97        buf.append(cbuf, off, len);
98    }
99
100    /**
101     * Write a string.
102     */
103    public void write(String str) {
104        buf.append(str);
105    }
106
107    /**
108     * Return the buffer's current value as a string.
109     */
110    public String toString() {
111        return buf.toString();
112    }
113
114
115    public void flush(){ }
116
117    public void close(){ }
118
119}
120