Path: blob/master/test/jdk/java/io/Writer/WriteFromString.java
41152 views
/*1* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 407176525@summary Bug in the parameter of str.getChars called in write26*/27282930import java.io.*;3132public class WriteFromString {333435public static void main(String argv[]) throws Exception {36LocalStringWriter lsw = new LocalStringWriter();37boolean result = true;3839String testString = "Testing of what gets written";40// Should write out at offset 2 for length 4 i.e."stin"41lsw.write(testString, 1, 4);42String res = lsw.toString();43if (!res.equals("esti")) {44result = false;45System.err.println("Writer.write is incorrect:" + res);46}4748// Same bug in stringwriter as well49StringWriter sw = new StringWriter();50sw.write(testString, 1, 4);51res = sw.toString();52String ss = testString.substring(1,4);53System.out.println("Substring = "+ss);54if (!res.equals("esti")) {55System.err.println("StringWriter.write is incorrect:" + res);56result = false;57}58if (!result) {59throw new Exception("Writer.write method is incorrect.");60}61}6263}6465/**66* A copy of StringWriter to test the write method in Writer67*/6869class LocalStringWriter extends Writer {7071private StringBuffer buf;7273/**74* Create a new string writer, using the default initial string-buffer75* size.76*/77public LocalStringWriter() {78buf = new StringBuffer();79lock = buf;80}8182/**83* Write a portion of an array of characters.84*85* @param cbuf Array of characters86* @param off Offset from which to start writing characters87* @param len Number of characters to write88*/89public void write(char cbuf[], int off, int len) {90if ((off < 0) || (off > cbuf.length) || (len < 0) ||91((off + len) > cbuf.length) || ((off + len) < 0)) {92throw new IndexOutOfBoundsException();93} else if (len == 0) {94return;95}96buf.append(cbuf, off, len);97}9899/**100* Write a string.101*/102public void write(String str) {103buf.append(str);104}105106/**107* Return the buffer's current value as a string.108*/109public String toString() {110return buf.toString();111}112113114public void flush(){ }115116public void close(){ }117118}119120121