Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/charset/CharsetEncoder/Flush.java
38828 views
/*1* Copyright (c) 2010, 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 622760825* @summary Test proper handling of flush()26* @author Martin Buchholz27*/2829import java.util.*;30import java.io.*;31import java.nio.*;32import java.nio.charset.*;3334public class Flush {35private static byte[] contents(ByteBuffer bb) {36byte[] contents = new byte[bb.position()];37((ByteBuffer)(bb.duplicate().flip())).get(contents);38return contents;39}4041private static ByteBuffer extend(ByteBuffer bb) {42ByteBuffer x = ByteBuffer.allocate(2*bb.capacity()+10);43bb.flip();44x.put(bb);45return x;46}4748private static void realMain(String[] args) throws Throwable {49// A japanese character should decode as a 3-byte50// switch-to-japanese escape sequence, followed by a 2-byte51// encoding of the char itself, followed by a 3-byte return to52// ASCII escape sequence.53char[] jis0208 = {'\u3001'};54CharBuffer cb = CharBuffer.wrap(jis0208);55ByteBuffer bb = ByteBuffer.allocate(6);56CharsetEncoder enc = Charset.forName("ISO-2022-JP").newEncoder();5758check(enc.encode(cb, bb, true).isUnderflow());5960System.out.println(Arrays.toString(contents(bb)));61check(! cb.hasRemaining());62equal(contents(bb).length, 3 + 2);63equal(bb.get(0), (byte)0x1b);6465//----------------------------------------------------------------66// We must be able to recover if flush() returns OVERFLOW67//----------------------------------------------------------------68check(enc.flush(bb).isOverflow());69check(enc.flush(bb).isOverflow());70equal(contents(bb).length, 3 + 2);7172bb = extend(bb);7374check(enc.flush(bb).isUnderflow());75equal(bb.get(3 + 2), (byte)0x1b);76System.out.println(Arrays.toString(contents(bb)));77equal(contents(bb).length, 3 + 2 + 3);7879//----------------------------------------------------------------80// A final redundant flush() is a no-op81//----------------------------------------------------------------82check(enc.flush(bb).isUnderflow());83check(enc.flush(bb).isUnderflow());84equal(contents(bb).length, 3 + 2 + 3);8586//----------------------------------------------------------------87// CharsetEncoder.encode(ByteBuffer) must call flush(ByteBuffer)88//----------------------------------------------------------------89bb = enc.encode(CharBuffer.wrap(jis0208));90byte[] expected = "\u001b$B!\"\u001b(B".getBytes("ASCII");91byte[] contents = new byte[bb.limit()]; bb.get(contents);92check(Arrays.equals(contents, expected));93}9495//--------------------- Infrastructure ---------------------------96static volatile int passed = 0, failed = 0;97static void pass() { passed++; }98static void fail() { failed++; Thread.dumpStack(); }99static void fail(String msg) { System.out.println(msg); fail(); }100static void unexpected(Throwable t) { failed++; t.printStackTrace(); }101static void check(boolean cond) { if (cond) pass(); else fail(); }102static void equal(Object x, Object y) {103if (x == null ? y == null : x.equals(y)) pass();104else {System.out.println(x + " not equal to " + y); fail(); }}105106public static void main(String[] args) throws Throwable {107try { realMain(args); } catch (Throwable t) { unexpected(t); }108109System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);110if (failed > 0) throw new Exception("Some tests failed");111}112}113114115