Path: blob/master/test/jdk/java/io/Writer/Bug6856817.java
41149 views
/*1* Copyright (c) 2015, 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/*24* @test25* @bug 685681726* @summary optimize the Writer.append(CharSequence) method27*/28import java.io.File;29import java.io.FileOutputStream;30import java.io.FileReader;31import java.io.IOException;32import java.io.OutputStream;33import java.io.OutputStreamWriter;34import java.nio.ByteBuffer;35import java.nio.CharBuffer;3637/**38*39* @author [email protected]40*/41public class Bug6856817 {4243private static final String str = "This is just a test string that i am using it to test the CharBuffer.append(CharSequence csq) for little bit performance improvement.";44private static final int BUF_SIZE = 1024;4546/**47*48* @param args49*/50public static void main(String args[]) {51CharBuffer charBuffer = CharBuffer.allocate(BUF_SIZE);52File file = new File("temp.txt");53file.deleteOnExit();5455charBuffer.put(str);56charBuffer.flip();57checkFileContent(charBuffer, file, str);58charBuffer.position(10);59checkFileContent(charBuffer, file, str.substring(10));60charBuffer.position(charBuffer.limit());61checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));6263char arr[] = new char[BUF_SIZE];64charBuffer = CharBuffer.wrap(arr);65charBuffer.put(str);66charBuffer.flip();67checkFileContent(charBuffer, file, str);68charBuffer.position(10);69checkFileContent(charBuffer, file, str.substring(10));70charBuffer.position(charBuffer.limit());71checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));7273char secArr[] = new char[BUF_SIZE];74charBuffer = CharBuffer.wrap(secArr);75charBuffer.put(str);76charBuffer.position(5);77charBuffer.limit(str.length() - 7);78charBuffer = charBuffer.slice();79checkFileContent(charBuffer, file, str.substring(5, (str.length() - 7)));80charBuffer.position(10);81checkFileContent(charBuffer, file, str.substring(15, (str.length() - 7)));82charBuffer.position(charBuffer.limit());83checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));8485charBuffer = ByteBuffer.allocate(BUF_SIZE).asCharBuffer();86charBuffer.put(str);87charBuffer.flip();88checkFileContent(charBuffer, file, str);89charBuffer.position(10);90checkFileContent(charBuffer, file, str.substring(10));91charBuffer.position(charBuffer.limit());92checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));9394charBuffer = ByteBuffer.allocateDirect(1024).asCharBuffer();95charBuffer.put(str);96charBuffer.flip();97checkFileContent(charBuffer, file, str);98charBuffer.position(10);99checkFileContent(charBuffer, file, str.substring(10));100charBuffer.position(charBuffer.limit());101checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));102}103104private static void checkFileContent(CharBuffer charBuffer, File file, String expectedValue) {105OutputStreamWriter writer = null;106FileReader reader = null;107int position, limit;108position = charBuffer.position();109limit = charBuffer.limit();110try {111OutputStream outputStream = new FileOutputStream(file);112writer = new OutputStreamWriter(outputStream);113writer.append(charBuffer);114writer.close();115if (!isEqual(position, charBuffer.position())) {116System.out.println(": failed");117throw new RuntimeException("buffer position before write: " + position + " and position after write: " + charBuffer.position());118}119if (!isEqual(limit, charBuffer.limit())) {120System.out.println(": failed");121throw new RuntimeException("buffer limit before write: " + limit + " and limit after write: " + charBuffer.limit());122}123reader = new FileReader(file);124char arr[] = new char[BUF_SIZE];125int byteRead = reader.read(arr);126if (byteRead != -1) {127String stringRead = new String(arr, 0, byteRead);128if (expectedValue.equals(stringRead)) {129System.out.println(": passed");130} else {131System.out.println(": failed");132throw new RuntimeException("expected :" + expectedValue + " and got:" + stringRead);133}134}135} catch (IOException ex) {136ex.printStackTrace();137throw new RuntimeException(ex);138} finally {139try {140if (writer != null) {141writer.close();142}143if (reader != null) {144reader.close();145}146} catch (IOException ex) {147throw new RuntimeException(ex);148}149}150}151152private static boolean isEqual(final int first, final int second) {153return (first == second);154}155}156157158