--- 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)