### Question:
Utf8OutputStreamWriter extends Writer { @Override public void write(char[] buf, int offset, int count) { if (outputStream == null) throw new IOException("Can't call write on a Utf8OutputStreamWriter that's already closed"); int srcPosition = offset; int srcEnd = offset + count; while (srcPosition < srcEnd) { if (destBuffer.length - destPosition < 256) { assert outputStream != null : "@SuppressWarnings(nullness)"; outputStream.write(destBuffer, 0, destPosition); destPosition = 0; } int srcCurrEnd = srcPosition + (destBuffer.length - destPosition) / 3; if (srcCurrEnd > srcEnd) srcCurrEnd = srcEnd; while (srcPosition < srcCurrEnd) { char c = buf[srcPosition++]; if (queuedLeadSurrogate != -1) { int trailSurrogate = ((int) c) & 0xFFFF; if (trailSurrogate < 0xDC00 || trailSurrogate > 0xDFFF) throw new InvalidFormatException("Character " + trailSurrogate + " unexpected; only a valid trail surrogate should come after a lead surrogate"); int n = (queuedLeadSurrogate << 10) + trailSurrogate + -56613888 ; destBuffer[destPosition++] = (byte) (0xF0 + ((n >> 18) & 0x07)); destBuffer[destPosition++] = (byte) (0x80 + ((n >> 12) & 0x3F)); destBuffer[destPosition++] = (byte) (0x80 + ((n >> 6) & 0x3F)); destBuffer[destPosition++] = (byte) (0x80 + (n & 0x3F)); queuedLeadSurrogate = -1; } else if (c <= 0x7F) { destBuffer[destPosition++] = (byte) c; } else if (c <= 0x7FF) { destBuffer[destPosition++] = (byte) (0xC0 + ((c >> 6) & 0x1F)); destBuffer[destPosition++] = (byte) (0x80 + (c & 0x3F)); } else if (c >= 0xD800 && c <= 0xDFFF) { int firstSurrogate = ((int) c) & 0xFFFF; if (firstSurrogate > 0xDBFF) throw new InvalidFormatException("Character " + firstSurrogate + " unexpected; surrogate pair must start with a lead surrogate"); queuedLeadSurrogate = firstSurrogate; } else { destBuffer[destPosition++] = (byte) (0xE0 + ((c >> 12) & 0x0F)); destBuffer[destPosition++] = (byte) (0x80 + ((c >> 6) & 0x3F)); destBuffer[destPosition++] = (byte) (0x80 + (c & 0x3F)); } } } } Utf8OutputStreamWriter(OutputStream outputStream);  Utf8OutputStreamWriter(OutputStream outputStream, boolean closeOuterStream); @Override void close(); @Override void flush(); @Override void write(char[] buf, int offset, int count);  }### Answer:
@Test public void testWrite() { testRoundTripping("a"); testRoundTripping("abcdef"); StringBuilder uni = new StringBuilder(); for (char c = '\u0000'; c <= '\ud7ff'; ++c) uni.append(c); for (char c = '\ue000'; c < '\uffff'; ++c) uni.append(c); uni.append('\uffff'); for (char c = '\ud7ff'; c > 0; --c) uni.append(c); uni.append('\0'); for (int codePlane = 1; codePlane <= 16; ++codePlane) appendCodePoints(codePlane, uni); testRoundTripping(uni.toString()); }