Path: blob/master/test/jdk/java/nio/charset/CharsetEncoder/Flush.java
41153 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* @modules jdk.charsets27* @author Martin Buchholz28*/2930import java.util.*;31import java.io.*;32import java.nio.*;33import java.nio.charset.*;3435public class Flush {36private static byte[] contents(ByteBuffer bb) {37byte[] contents = new byte[bb.position()];38bb.duplicate().flip().get(contents);39return contents;40}4142private static ByteBuffer extend(ByteBuffer bb) {43ByteBuffer x = ByteBuffer.allocate(2*bb.capacity()+10);44bb.flip();45x.put(bb);46return x;47}4849private static void realMain(String[] args) throws Throwable {50// A japanese character should decode as a 3-byte51// switch-to-japanese escape sequence, followed by a 2-byte52// encoding of the char itself, followed by a 3-byte return to53// ASCII escape sequence.54char[] jis0208 = {'\u3001'};55CharBuffer cb = CharBuffer.wrap(jis0208);56ByteBuffer bb = ByteBuffer.allocate(6);57CharsetEncoder enc = Charset.forName("ISO-2022-JP").newEncoder();5859check(enc.encode(cb, bb, true).isUnderflow());6061System.out.println(Arrays.toString(contents(bb)));62check(! cb.hasRemaining());63equal(contents(bb).length, 3 + 2);64equal(bb.get(0), (byte)0x1b);6566//----------------------------------------------------------------67// We must be able to recover if flush() returns OVERFLOW68//----------------------------------------------------------------69check(enc.flush(bb).isOverflow());70check(enc.flush(bb).isOverflow());71equal(contents(bb).length, 3 + 2);7273bb = extend(bb);7475check(enc.flush(bb).isUnderflow());76equal(bb.get(3 + 2), (byte)0x1b);77System.out.println(Arrays.toString(contents(bb)));78equal(contents(bb).length, 3 + 2 + 3);7980//----------------------------------------------------------------81// A final redundant flush() is a no-op82//----------------------------------------------------------------83check(enc.flush(bb).isUnderflow());84check(enc.flush(bb).isUnderflow());85equal(contents(bb).length, 3 + 2 + 3);8687//----------------------------------------------------------------88// CharsetEncoder.encode(ByteBuffer) must call flush(ByteBuffer)89//----------------------------------------------------------------90bb = enc.encode(CharBuffer.wrap(jis0208));91byte[] expected = "\u001b$B!\"\u001b(B".getBytes("ASCII");92byte[] contents = new byte[bb.limit()]; bb.get(contents);93check(Arrays.equals(contents, expected));94}9596//--------------------- Infrastructure ---------------------------97static volatile int passed = 0, failed = 0;98static void pass() { passed++; }99static void fail() { failed++; Thread.dumpStack(); }100static void fail(String msg) { System.out.println(msg); fail(); }101static void unexpected(Throwable t) { failed++; t.printStackTrace(); }102static void check(boolean cond) { if (cond) pass(); else fail(); }103static void equal(Object x, Object y) {104if (x == null ? y == null : x.equals(y)) pass();105else {System.out.println(x + " not equal to " + y); fail(); }}106107public static void main(String[] args) throws Throwable {108try { realMain(args); } catch (Throwable t) { unexpected(t); }109110System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);111if (failed > 0) throw new Exception("Some tests failed");112}113}114115116