Path: blob/master/test/jdk/java/io/Reader/ReadIntoReadOnlyBuffer.java
41149 views
/*1* Copyright (c) 2021, 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*/2223import java.io.IOException;24import java.io.Reader;25import java.io.StringReader;26import java.nio.ByteBuffer;27import java.nio.CharBuffer;28import java.nio.ReadOnlyBufferException;2930/*31* @test32* @bug 826607833* @summary Tests that attempting to read into a read-only CharBuffer34* does not advance the Reader position35* @run main ReadIntoReadOnlyBuffer36*/37public class ReadIntoReadOnlyBuffer {38private static final String THE_STRING = "123";39public static void main(String[] args) throws Exception {40CharBuffer buf = CharBuffer.allocate(8).asReadOnlyBuffer();41StringReader r = new StringReader(THE_STRING);42read(r, buf);43buf = ByteBuffer.allocateDirect(16).asCharBuffer().asReadOnlyBuffer();44r = new StringReader(THE_STRING);45read(r, buf);46}4748private static void read(Reader r, CharBuffer b) throws IOException {49try {50r.read(b);51throw new RuntimeException("ReadOnlyBufferException expected");52} catch (ReadOnlyBufferException expected) {53}5455char[] c = new char[3];56int n = r.read(c);57if (n != c.length) {58throw new RuntimeException("Expected " + c.length + ", got " + n);59}6061String s = new String(c);62if (!s.equals(THE_STRING)) {63throw new RuntimeException("Expected " + THE_STRING + ", got " + s);64}65}66}676869