package java.io;

import java.lang.String;
import kaffe.io.CharToByteConverter;

/*
 * Java core library component.
 *
 * Copyright (c) 1997, 1998
 *      Transvirtual Technologies, Inc.  All rights reserved.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file.
 */
public class OutputStreamWriter
  extends Writer
{
	final private static int BUFDEFAULT = 1024;
	private OutputStream strm;
	private CharToByteConverter encoding;
	private byte[] outbuf = new byte[BUFDEFAULT];

public OutputStreamWriter(OutputStream out)
	{
	strm = out;
	encoding = CharToByteConverter.getDefault();
}

public OutputStreamWriter(OutputStream out, String enc) throws UnsupportedEncodingException
{
	strm = out;
	encoding = CharToByteConverter.getConverter(enc);
}

public void close() throws IOException
{
	flush();
	strm.close();
}

public void flush() throws IOException
{
	strm.flush();
}

public String getEncoding()
	{
	return (encoding.toString());
}

public void write(String str, int off, int len) throws IOException
{
	write(str.toCharArray(), off, len);
}

public void write ( char cbuf[], int off, int len ) throws IOException
{
	int outlen;

	synchronized(lock) {
		while ( len > 0 ) {
			outlen = encoding.convert( cbuf, off, len, outbuf, 0, outbuf.length);
			strm.write( outbuf, 0, outlen);
			off += outlen;
			len -= outlen;
		}
	}
}

public void write(int c) throws IOException
{
	char ch[] = new char[1];
	ch[0] = (char)c;
	write (ch, 0, 1);
}
}

@@ -358,22 +358,16 @@
 %0A%7B%0A%09
-final 
 private 
 stat
@@ -362,16 +362,22 @@
 private 
+final 
 static i
@@ -398,16 +398,58 @@
 = 1024;%0A
+%09private final static int MINMARGIN = 32;%0A
 %09private
@@ -553,16 +553,37 @@
 EFAULT%5D;
+%0A%09private int buflen;
 %0A%0Apublic
@@ -620,17 +620,16 @@
 am out)%0A
-%09
 %7B%0A%09strm 
@@ -951,24 +951,123 @@
 Exception%0A%7B%0A
+%09synchronized (lock) %7B%0A%09%09if (buflen %3E 0) %7B%0A%09%09%09strm.write(outbuf, 0, buflen);%0A%09%09%09buflen = 0;%0A%09%09%7D%0A%09%7D%0A
 %09strm.flush(
@@ -1100,17 +1100,16 @@
 oding()%0A
-%09
 %7B%0A%09retur
@@ -1262,19 +1262,17 @@
 id write
- ( 
+(
 char cbu
@@ -1292,17 +1292,16 @@
  int len
- 
 ) throws
@@ -1319,22 +1319,8 @@
 n%0A%7B%0A
-%09int outlen;%0A%0A
 %09syn
@@ -1328,16 +1328,17 @@
 hronized
+ 
 (lock) %7B
@@ -1351,24 +1351,26 @@
 le (
- 
 len %3E 0
- 
 ) %7B%0A%09%09%09
+int 
 outl
@@ -1391,17 +1391,16 @@
 convert(
- 
 cbuf, of
@@ -1406,25 +1406,29 @@
 ff, len,
- 
+%0A%09%09%09%09
 outbuf, 
 0, outbu
@@ -1415,25 +1415,30 @@
 %09%09%09%09outbuf, 
-0
+buflen
 , outbuf.len
@@ -1444,43 +1444,109 @@
 ngth
-);%0A%09%09%09strm.write( outbuf, 0, outlen
+ - buflen);%0A%09%09%09buflen += outlen;%0A%09%09%09if (outlen == 0 %7C%7C outbuf.length - buflen %3C MINMARGIN)%0A%09%09%09%09flush(
 );%0A%09
@@ -1641,49 +1641,35 @@
 %0A%7B%0A%09
-char ch%5B%5D = new char%5B1%5D;%0A%09ch%5B0%5D =
+write (new char%5B%5D %7B
  (char)c
 ;%0A%09w
@@ -1668,20 +1668,10 @@
 ar)c
-;%0A%09write (ch
+ %7D
 , 0,

Use the buffer we have to actually buffer the converted output.