java.io

Class OutputStreamWriter

Implemented Interfaces:
Appendable, Closeable, Flushable
Known Direct Subclasses:
FileWriter

public class OutputStreamWriter
extends Writer

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.

For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example:

 Writer out
   = new BufferedWriter(new OutputStreamWriter(System.out));
 

A surrogate pair is a character represented by a sequence of two char values: A high surrogate in the range '\uD800' to '\uDBFF' followed by a low surrogate in the range '\uDC00' to '\uDFFF'. If the character represented by a surrogate pair cannot be encoded by a given charset then a charset-dependent substitution sequence is written to the output stream.

A malformed surrogate element is a high surrogate that is not followed by a low surrogate or a low surrogate that is not preceded by a high surrogate. It is illegal to attempt to write a character stream containing malformed surrogate elements. The behavior of an instance of this class when a malformed surrogate element is written is not specified.

Since:
JDK1.1
See Also:
BufferedWriter, OutputStream, Charset

Field Summary

Fields inherited from class java.io.Writer

lock

Constructor Summary

OutputStreamWriter(OutputStream out)
Create an OutputStreamWriter that uses the default character encoding.
OutputStreamWriter(OutputStream out, String charsetName)
Create an OutputStreamWriter that uses the named charset.
OutputStreamWriter(OutputStream out, Charset cs)
Create an OutputStreamWriter that uses the given charset.
OutputStreamWriter(OutputStream out, CharsetEncoder enc)
Create an OutputStreamWriter that uses the given charset encoder.

Method Summary

void
close()
Close the stream.
void
flush()
Flush the stream.
String
getEncoding()
Return the name of the character encoding being used by this stream.
void
write(cbuf[] , int off, int len)
Write a portion of an array of characters.
void
write(int c)
Write a single character.
void
write(String str, int off, int len)
Write a portion of a string.

Methods inherited from class java.io.Writer

append, append, append, close, flush, write, write, write, write, write

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

OutputStreamWriter

public OutputStreamWriter(OutputStream out)
Create an OutputStreamWriter that uses the default character encoding.
Parameters:
out - An OutputStream
Usages and Demos :

View More Examples of OutputStreamWriter(OutputStream out)
   1:                     if (enc != null){
   2:                         writer = new OutputStreamWriter((OutputStream)arg1, enc);
   3:                     } else {
   4:         ...
   5:                         writer = new OutputStreamWriter((OutputStream)arg1);
   6:                     }
   7:                 } else {
   8:                     throw new IllegalArgumentException(String.valueOf(arg1));

View Full Code Here
   1:         if (obj instanceof OutputStream) {
   2:             return new OutputStreamWriter((OutputStream)obj);
   3:         } else if (obj instanceof Writer) {
   4:             return (Writer)obj;
   5:         }

View Full Code Here
   1:         if (obj instanceof OutputStream) {
   2:             return new OutputStreamWriter((OutputStream) obj);
   3:         } else if (obj instanceof Writer) {
   4:             return (Writer) obj;
   5:         }

View Full Code Here
   1:         try {
   2:             os = new OutputStreamWriter(SVNFileUtil.openFileForWriting(myTmpFile), "UTF-8");
   3:             for (Iterator commands = myCache.iterator(); commands.hasNext();) {
   4:                 Map command = (Map) commands.next();
   5:                 String name = (String) command.remove("");

View Full Code Here
   1:                               
   2:             _to_server = new PrintWriter ( new OutputStreamWriter( _connector_socket.getOutputStream() ) ) ;
   3: 
   4: 
   5:             String server_pid = _from_server.readLine() ;

View Full Code Here

OutputStreamWriter

public OutputStreamWriter(OutputStream out,
                          String charsetName)
            throws UnsupportedEncodingException
Create an OutputStreamWriter that uses the named charset.
Parameters:
out - An OutputStream
charsetName - The name of a supported charset
Throws:
UnsupportedEncodingException - If the named encoding is not supported
Usages and Demos :

View More Examples of OutputStreamWriter(OutputStream out,String charsetName)
   1:     if (_charset != null)
   2:       _writer = new OutputStreamWriter(os, _charset);
   3:     else
   4:         ...
   5:       _writer = new OutputStreamWriter(os, _encoding);
   6: 
   7:         ...
   8:     if (_charset != null)
   9:       _writer = new OutputStreamWriter(os, _charset);
  10:     else

View Full Code Here
   1:                     ? new OutputStreamWriter(base)
   2:                     : new OutputStreamWriter(base, name);
   3:                 Copying.copyChars(text, s);
   4:                 s.flush();
   5:                 s.close();

View Full Code Here
   1:             _bout = new ByteArrayOutputStream2(2*length);
   2:             _writer = new OutputStreamWriter(_bout,StringUtil.__ISO_8859_1);
   3:         }
   4:         else
   5:             _bout.reset();

View Full Code Here
   1:     if (!buffer)
   2:         out = new BufferedWriter(new OutputStreamWriter(_out, Charset.forName(ENCODING)
   3:             .newEncoder()), BUFFER_SIZE);
   4: 
   5:     indentation_ = 0;

View Full Code Here
   1:         try {
   2:             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out, encoding)), true);
   3:         } catch (UnsupportedEncodingException uee) {
   4:             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out)), true);
   5:         }

View Full Code Here

OutputStreamWriter

public OutputStreamWriter(OutputStream out,
                          Charset cs)
Create an OutputStreamWriter that uses the given charset.
Parameters:
out - An OutputStream
cs - A charset
Since:
1.4
Usages and Demos :

View More Examples of OutputStreamWriter(OutputStream out,Charset cs)
   1:     if (_charset != null)
   2:       _writer = new OutputStreamWriter(os, _charset);
   3:     else
   4:         ...
   5:       _writer = new OutputStreamWriter(os, _encoding);
   6: 
   7:         ...
   8:     if (_charset != null)
   9:       _writer = new OutputStreamWriter(os, _charset);
  10:     else

View Full Code Here
   1:                     ? new OutputStreamWriter(base)
   2:                     : new OutputStreamWriter(base, name);
   3:                 Copying.copyChars(text, s);
   4:                 s.flush();
   5:                 s.close();

View Full Code Here
   1:             _bout = new ByteArrayOutputStream2(2*length);
   2:             _writer = new OutputStreamWriter(_bout,StringUtil.__ISO_8859_1);
   3:         }
   4:         else
   5:             _bout.reset();

View Full Code Here
   1:     if (!buffer)
   2:         out = new BufferedWriter(new OutputStreamWriter(_out, Charset.forName(ENCODING)
   3:             .newEncoder()), BUFFER_SIZE);
   4: 
   5:     indentation_ = 0;

View Full Code Here
   1:         try {
   2:             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out, encoding)), true);
   3:         } catch (UnsupportedEncodingException uee) {
   4:             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out)), true);
   5:         }

View Full Code Here

OutputStreamWriter

public OutputStreamWriter(OutputStream out,
                          CharsetEncoder enc)
Create an OutputStreamWriter that uses the given charset encoder.
Parameters:
out - An OutputStream
enc - A charset encoder
Since:
1.4
Usages and Demos :

View More Examples of OutputStreamWriter(OutputStream out,CharsetEncoder enc)
   1:     if (_charset != null)
   2:       _writer = new OutputStreamWriter(os, _charset);
   3:     else
   4:         ...
   5:       _writer = new OutputStreamWriter(os, _encoding);
   6: 
   7:         ...
   8:     if (_charset != null)
   9:       _writer = new OutputStreamWriter(os, _charset);
  10:     else

View Full Code Here
   1:                     ? new OutputStreamWriter(base)
   2:                     : new OutputStreamWriter(base, name);
   3:                 Copying.copyChars(text, s);
   4:                 s.flush();
   5:                 s.close();

View Full Code Here
   1:             _bout = new ByteArrayOutputStream2(2*length);
   2:             _writer = new OutputStreamWriter(_bout,StringUtil.__ISO_8859_1);
   3:         }
   4:         else
   5:             _bout.reset();

View Full Code Here
   1:     if (!buffer)
   2:         out = new BufferedWriter(new OutputStreamWriter(_out, Charset.forName(ENCODING)
   3:             .newEncoder()), BUFFER_SIZE);
   4: 
   5:     indentation_ = 0;

View Full Code Here
   1:         try {
   2:             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out, encoding)), true);
   3:         } catch (UnsupportedEncodingException uee) {
   4:             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out)), true);
   5:         }

View Full Code Here

Method Details

close

public void close()
            throws IOException
Close the stream.
Specified by:
close in interface Closeable
Overrides:
close in interface Writer
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of close()
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.net.Socket;
   4:         ...
   5:         try {
   6:             OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
   7:             writer.write(SHUTDOWN_MESSAGE);
   8:         ...
   9:             writer.close();
  10:         } catch (UnknownHostException e) {

View Full Code Here
   1: 
   2: import java.io.OutputStreamWriter;
   3: 
   4:         ...
   5:         NewsML newsml = factory.createNewsML(args[0]);
   6:         OutputStreamWriter output = new OutputStreamWriter(System.out);
   7:         newsml.writeXML(output, true);
   8:         ...
   9:         output.close();
  10:     }

View Full Code Here
   1: 
   2: import java.io.OutputStreamWriter;
   3: 
   4:         ...
   5: 
   6:         OutputStreamWriter out = new OutputStreamWriter(System.out);
   7:         Copying.copyChars(output, out);
   8:         ...
   9:         out.flush();
  10:         out.close();
  11:     }

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.io.PrintWriter;
   4:         ...
   5: 
   6:       OutputStreamWriter osw = new OutputStreamWriter(System.out);
   7:       PrintWriter out = new PrintWriter(osw);
   8:         ...
   9: 
  10:       osw.close();
  11:         ...
  12:       out.close();

View Full Code Here
   1:     SwappingBuffer wad = new SwappingBuffer();
   2:     OutputStreamWriter writer = new OutputStreamWriter(wad, "UTF-8");
   3:     writer.write(s);
   4:         ...
   5:     writer.close();
   6:     long n = wad.size();
   7:     if (n > Integer.MAX_VALUE) throw new IllegalArgumentException("KBigStringNowYoureJustBeingSilly");
   8:     dos.writeBoolean(true);

View Full Code Here

flush

public void flush()
            throws IOException
Flush the stream.
Specified by:
flush in interface Flushable
Overrides:
flush in interface Writer
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of flush()
   1:   public static void main(String[] args) {
   2:     OutputStreamWriter osw = null;
   3:     try {
   4:         ...
   5:        osw = new OutputStreamWriter(new FileOutputStream("c:/test"),"Unicode");
   6:     }
   7:         ...
   8:       osw.write("\u00A9\u00BD\u0391\u0392\u0393\u0394\u00f8");
   9:       osw.flush();
  10:     }

View Full Code Here
   1: 
   2: import java.io.OutputStreamWriter;
   3: 
   4:         ...
   5: 
   6:         OutputStreamWriter out = new OutputStreamWriter(System.out);
   7:         Copying.copyChars(output, out);
   8:         ...
   9:         out.flush();
  10:         out.close();

View Full Code Here
   1:     uc.setDoOutput(true);
   2:     OutputStreamWriter out 
   3:         ...
   4:      = new OutputStreamWriter(uc.getOutputStream(), "ASCII");
   5: 
   6:         ...
   7:     out.write("\r\n");
   8:     out.flush();
   9:     out.close();

View Full Code Here
   1:         OutputStream os = con.getOutputStream();
   2:         OutputStreamWriter osw = new OutputStreamWriter(os);
   3:         osw.write(postData);
   4:         ...
   5:         osw.flush();
   6:         osw.close();
   7: 
   8:         InputStream is = con.getInputStream();

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.io.Reader;
   4:         ...
   5:         }
   6:         OutputStreamWriter writer = new OutputStreamWriter(os);
   7:         writer.write(s);
   8:         ...
   9:         writer.flush();
  10:     }

View Full Code Here

getEncoding

public String getEncoding()
Return the name of the character encoding being used by this stream.

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the OutputStreamWriter(OutputStream,String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.

Returns:
The historical name of this encoding, or possibly null if the stream has been closed
See Also:
Charset
Usages and Demos :

View More Examples of getEncoding()
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.io.Writer;
   4:         ...
   5:     public Writer openSource( JPackage pkg, String fileName ) throws IOException {
   6:         final OutputStreamWriter bw = new OutputStreamWriter(openBinary(pkg,fileName));
   7: 
   8:         ...
   9:             return new UnicodeEscapeWriter(bw) {
  10:                 private final CharsetEncoder encoder = EncoderFactory.createEncoder(bw.getEncoding());
  11:                 protected boolean requireEscaping(int ch) {

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.io.Writer;
   4:         ...
   5:         File encodingFinder = new File(getTestDirectory(), "finder.txt");
   6:         OutputStreamWriter out = null;
   7:         try {
   8:         ...
   9:             out = new OutputStreamWriter(new FileOutputStream(encodingFinder));
  10:         ...
  11:             defaultEncoding = out.getEncoding();

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.io.PrintWriter;
   4:         ...
   5:                         {    
   6:                             OutputStreamWriter ow = new OutputStreamWriter(fos, this.enc);
   7:                             this.out = new PrintWriter(ow,true);
   8:         ...
   9: 
  10:                             this.logger.AppliInfo("Ecoding"+ow.getEncoding()+"support "+java.nio.charset.Charset.isSupported(this.enc));
  11:                         }

View Full Code Here
   1:     private ByteBuffer mBuffer;
   2:     private transient OutputStreamWriter mConvertor;
   3: 
   4:         ...
   5:         drain(true);
   6:         mConvertor = new OutputStreamWriter
   7:             (new ByteBufferOutputStream(mBuffer), enc);
   8:         ...
   9:         return (mConvertor == null) ? mDefaultEncoding :
  10:             mConvertor.getEncoding();
  11:     }
  12:         ...
  13: 
  14:     private OutputStreamWriter getConvertor()

View Full Code Here
   1: import java.io.InputStreamReader;
   2: import java.io.OutputStreamWriter;
   3: import java.util.Properties;
   4:         ...
   5:         {
   6:             OutputStreamWriter defaultStream = new OutputStreamWriter
   7:                 ( new ByteArrayOutputStream () );
   8:         ...
   9:             String sysenc = defaultStream.getEncoding();
  10:             try
  11:         ...
  12: 
  13:         String defaultIOEncoding = (new OutputStreamWriter(new ByteArrayOutputStream ())).getEncoding();

View Full Code Here

write

public void write(cbuf[] ,
                  int off,
                  int len)
            throws IOException
Write a portion of an array of characters.
Overrides:
write in interface Writer
Parameters:
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(cbuf[] ,int off,int len)
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.io.UnsupportedEncodingException;
   4:         ...
   5:     private ByteArrayOutputStream byteArray = new ByteArrayOutputStream ();
   6:     private OutputStreamWriter streamWriter;
   7:     private ServletOutputStream existingStream;
   8:         ...
   9:     private OutputStreamWriter existingStreamWriter;
  10:     private String encoding;
  11:         ...
  12:     {
  13:         streamWriter.write(cbuf, 0, cbuf.length);

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.io.UnsupportedEncodingException;
   4:         ...
   5:     private String _encoding;
   6:     private OutputStreamWriter _writer;
   7:     private OutputStreamWithBuffer _os;
   8:         ...
   9:     if (_charset != null)
  10:       _writer = new OutputStreamWriter(os, _charset);
  11:     else
  12:         ...
  13:       
  14:       _writer.write(buf, offset, length);

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.io.Reader;
   4:         ...
   5:    {
   6:       return new OutputStreamWriter(new FileOutputStream(file), charset);
   7:    }
   8:         ...
   9:       {
  10:          outs.write(bytes, 0, r);
  11:          r = ins.read(bytes);
  12:         ...
  13:    {
  14:       OutputStreamWriter writer = new OutputStreamWriter(outs);

View Full Code Here
   1:       = new InputStreamReader(inStream, inEncodingName);
   2:     OutputStreamWriter out
   3:         ...
   4:       = new OutputStreamWriter(outStream, outEncodingName);
   5:     char[] buffer = new char[2048];
   6:         ...
   7:           break;
   8:         out.write(buffer, 0, count);
   9:       }

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.io.Writer;
   4:         ...
   5:     private ByteArrayOutputStream2 _bout=null;
   6:     private OutputStreamWriter _writer=null;
   7:     private boolean _fixed=false;
   8:         ...
   9:     {
  10:         out.write(_buf,0,_size);
  11:     }
  12:         ...
  13:             _bout = new ByteArrayOutputStream2(2*length);
  14:             _writer = new OutputStreamWriter(_bout,StringUtil.__ISO_8859_1);

View Full Code Here

write

public void write(int c)
            throws IOException
Write a single character.
Overrides:
write in interface Writer
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(int c)
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: 
   4:         ...
   5:         OutputStream os = getResponse().getOutputStream();
   6:         OutputStreamWriter writer = new OutputStreamWriter(os);
   7:         getResponse().setBufferSize(1);
   8:         ...
   9:         {
  10:             writer.write("                                                                                ");
  11:         }
  12:         ...
  13:         writer.write("\n");

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: 
   4:         ...
   5:         File outputFile = new File(fileName);
   6:         OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
   7:                     outputFile));
   8:         ...
   9: 
  10:         out.write("<description>");
  11:         ...
  12:         out.write(query.toString());

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.io.IOException;
   4:         ...
   5:     public void write(OutputStream os) throws IOException {
   6:         OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
   7:         osw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
   8:         ...
   9:                     if (nLevel>=0) { writeSpaces(nLevel,osw); }
  10:                     osw.write("<"+node.getNodeName());
  11:                     writeAttributes(node,osw);
  12:         ...
  13:                     osw.write(">");

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: 
   4:         ...
   5:     public void write(OutputStream os) throws IOException {
   6:         OutputStreamWriter osw = new OutputStreamWriter(os,"ASCII");
   7:         osw.write("%% This file was converted to BibTeX by Writer2BibTeX ver. "+Application.getVersion()+".\n");
   8:         ...
   9:         osw.write("\n");
  10:         Enumeration enumeration = entries.elements();
  11:         ...
  12:             BibMark entry = (BibMark) enumeration.nextElement();
  13:             osw.write("@");

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.security.InvalidParameterException;
   4:         ...
   5:             }
   6:             OutputStreamWriter writer = new OutputStreamWriter(out);
   7:         ...
   8:             writer.write("#*******************************************************************************\n");
   9:         ...
  10:             writer.write("# Copyright (c) 2004, 2006 IBM Corporation.\n");

View Full Code Here

write

public void write(String str,
                  int off,
                  int len)
            throws IOException
Write a portion of a string.
Overrides:
write in interface Writer
Parameters:
str - A String
off - Offset from which to start writing characters
len - Number of characters to write
Throws:
IOException - If an I/O error occurs
Usages and Demos :

View More Examples of write(String str,int off,int len)
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.io.UnsupportedEncodingException;
   4:         ...
   5:     private ByteArrayOutputStream byteArray = new ByteArrayOutputStream ();
   6:     private OutputStreamWriter streamWriter;
   7:     private ServletOutputStream existingStream;
   8:         ...
   9:     private OutputStreamWriter existingStreamWriter;
  10:     private String encoding;
  11:         ...
  12:     {
  13:         streamWriter.write(cbuf, 0, cbuf.length);

View Full Code Here
   1: import java.io.IOException;
   2: import java.io.OutputStreamWriter;
   3: import java.io.UnsupportedEncodingException;
   4:         ...
   5:     private String _encoding;
   6:     private OutputStreamWriter _writer;
   7:     private OutputStreamWithBuffer _os;
   8:         ...
   9:     if (_charset != null)
  10:       _writer = new OutputStreamWriter(os, _charset);
  11:     else
  12:         ...
  13:       
  14:       _writer.write(buf, offset, length);

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.io.Reader;
   4:         ...
   5:    {
   6:       return new OutputStreamWriter(new FileOutputStream(file), charset);
   7:    }
   8:         ...
   9:       {
  10:          outs.write(bytes, 0, r);
  11:          r = ins.read(bytes);
  12:         ...
  13:    {
  14:       OutputStreamWriter writer = new OutputStreamWriter(outs);

View Full Code Here
   1:       = new InputStreamReader(inStream, inEncodingName);
   2:     OutputStreamWriter out
   3:         ...
   4:       = new OutputStreamWriter(outStream, outEncodingName);
   5:     char[] buffer = new char[2048];
   6:         ...
   7:           break;
   8:         out.write(buffer, 0, count);
   9:       }

View Full Code Here
   1: import java.io.OutputStream;
   2: import java.io.OutputStreamWriter;
   3: import java.io.Writer;
   4:         ...
   5:     private ByteArrayOutputStream2 _bout=null;
   6:     private OutputStreamWriter _writer=null;
   7:     private boolean _fixed=false;
   8:         ...
   9:     {
  10:         out.write(_buf,0,_size);
  11:     }
  12:         ...
  13:             _bout = new ByteArrayOutputStream2(2*length);
  14:             _writer = new OutputStreamWriter(_bout,StringUtil.__ISO_8859_1);

View Full Code Here