--- old/src/share/classes/sun/nio/cs/StreamEncoder.java 2012-02-08 17:38:46.000000000 -0800 +++ new/src/share/classes/sun/nio/cs/StreamEncoder.java 2012-02-08 17:38:46.000000000 -0800 @@ -213,16 +213,16 @@ assert (pos <= lim); int rem = (pos <= lim ? lim - pos : 0); - if (rem > 0) { - if (ch != null) { - if (ch.write(bb) != rem) - assert false : rem; - } else { - out.write(bb.array(), bb.arrayOffset() + pos, rem); - } + if (rem > 0) { + if (ch != null) { + if (ch.write(bb) != rem) + assert false : rem; + } else { + out.write(bb.array(), bb.arrayOffset() + pos, rem); + } } bb.clear(); - } + } private void flushLeftoverChar(CharBuffer cb, boolean endOfInput) throws IOException @@ -259,59 +259,71 @@ haveLeftoverChar = false; } + private void flushEncoder() throws IOException { + for (;;) { + CoderResult cr = encoder.flush(bb); + if (cr.isUnderflow()) + break; + if (cr.isOverflow()) { + assert bb.position() > 0; + writeBytes(); + continue; + } + cr.throwException(); + } + } + void implWrite(char cbuf[], int off, int len) throws IOException { CharBuffer cb = CharBuffer.wrap(cbuf, off, len); if (haveLeftoverChar) - flushLeftoverChar(cb, false); + flushLeftoverChar(cb, false); while (cb.hasRemaining()) { - CoderResult cr = encoder.encode(cb, bb, false); - if (cr.isUnderflow()) { - assert (cb.remaining() <= 1) : cb.remaining(); - if (cb.remaining() == 1) { - haveLeftoverChar = true; - leftoverChar = cb.get(); + CoderResult cr = encoder.encode(cb, bb, false); + if (cr.isUnderflow()) { + assert (cb.remaining() <= 1) : cb.remaining(); + if (cb.remaining() == 1) { + haveLeftoverChar = true; + leftoverChar = cb.get(); + } + break; } - break; - } - if (cr.isOverflow()) { - assert bb.position() > 0; - writeBytes(); - continue; - } - cr.throwException(); + if (cr.isOverflow()) { + assert bb.position() > 0; + writeBytes(); + continue; + } + cr.throwException(); } } void implFlushBuffer() throws IOException { + try { + // flush underlying encoder + flushLeftoverChar(null, true); + flushEncoder(); + encoder.reset(); + } catch (IOException x) { + encoder.reset(); + throw x; + } if (bb.position() > 0) - writeBytes(); + writeBytes(); } void implFlush() throws IOException { implFlushBuffer(); if (out != null) - out.flush(); + out.flush(); } void implClose() throws IOException { flushLeftoverChar(null, true); try { - for (;;) { - CoderResult cr = encoder.flush(bb); - if (cr.isUnderflow()) - break; - if (cr.isOverflow()) { - assert bb.position() > 0; - writeBytes(); - continue; - } - cr.throwException(); - } - + flushEncoder(); if (bb.position() > 0) writeBytes(); if (ch != null) --- /dev/null 2011-11-08 10:18:28.885878122 -0800 +++ new/test/java/io/OutputStreamWriter/Flush.java 2012-02-08 17:38:47.000000000 -0800 @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 6995537 + * @summary check OutputStreamWriter and PrintStream flush the underlying + * charset encoder correctly + */ + +import java.io.*; +import java.util.*; + +public class Flush { + public static void main(String args[]) throws Exception { + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + String str = "\u6700"; + byte[] bytes = new byte[] { (byte)0x1b, (byte)0x24, (byte)0x42, + (byte)0x3a, (byte)0x47, + (byte)0x1b, (byte)0x28, (byte)0x42 }; + + OutputStreamWriter osw = new OutputStreamWriter(bos, "iso-2022-jp"); + osw.write(str, 0, str.length()); + osw.flush(); + check(bos.toByteArray(), bytes); + + bos.reset(); + PrintStream ps = new PrintStream(bos, false, "iso-2022-jp"); + ps.print(str); + check(bos.toByteArray(), bytes); + + } + + private static void check(byte[] result, byte[] expected) + throws Exception + { + if (!Arrays.equals(result, expected)) { + System.out.println("Expected:"); + for (int i = 0; i < expected.length; i++) + System.out.printf("0x%x ", expected[i] & 0xff); + System.out.println(); + System.out.println("Result::"); + for (int i = 0; i < result.length; i++) + System.out.printf("0x%x ", result[i] & 0xff); + System.out.println(); + throw new RuntimeException("Test failed"); + } + + } + + +}